分布式配置:配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。
在生产环境,由于配置繁多,造成运维人员或开发人员配置上的不便,springcloud提供了分布式配置中心组建,支持从远程配置仓库(git)中读取配置。
读取配置流程如下图:
config-server从git中读取远程配置,config-client在从config-server中读取配置。
新建项目config-server,pom文件中加入config-server依赖,完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId> <artifactId>springcloud_config_server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud_config_server</name> <description>springcloud_config_server</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类加入@EnableConfigServer注解,表明这是一个config-server并开启配置服务器功能。代码如下:
package com.dalaoyang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class SpringcloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConfigServerApplication.class, args); } }
application.properties配置如下:
##端口号 server.port=9000 ##服务名称 spring.application.name=config_server ##注册中心地址 eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/ ##远程仓库配置 ##git仓库地址 spring.cloud.config.server.git.uri=https://gitee.com/dalaoyang/SpringCloudConfig.git ##git仓库配置路径 spring.cloud.config.server.git.searchPaths=config ##git仓库分支 spring.cloud.config.label=master ##如果为公开仓库,用户名密码可不填 ##git仓库用户名 ##spring.cloud.config.server.git.username= ##git仓库密码 ##spring.cloud.config.server.git.password=
config-server到这里构建完成,然后新建一个config-client服务。pom文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId> <artifactId>springcloud_config_client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud_config_client</name> <description>springcloud_config_client</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
然后新建一个配置文件,这里需要说明一下,config-client默认会去找8888端口的配置中心,如果配置中心使用8888端口,config-client可以使用application.properties配置文件。如果配置中心没有使用8888端口,那么config-client要使用bootstrap.properties,原因是bootstrap.* 命名的文件会被优先读取和配置。由于这个例子的项目使用端口为9000,所以新建一个bootstrap.properties,代码如下:
spring.application.name=config-client spring.cloud.config.label=master spring.cloud.config.profile=test spring.cloud.config.uri= http://localhost:9000/ ##注册中心地址 eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
然后做一个测试类,修改一下application,代码如下:
package com.dalaoyang; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class SpringcloudConfigClientApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConfigClientApplication.class, args); } @Value("${title}") String title; @RequestMapping("/getTitle") public String getTitle(){ return title; } }
到这里就全部构建完成了,依次启动config-server和config-client。
先访问一下http://eureka.dalaoyang.cn/
可以看到服务构建成功,然后访问http://localhost:8080/getTitle,可以看到:
源码下载 :大老杨码云
共同学习,写下你的评论
评论加载中...
作者其他优质文章