简介:我们之前创建的微服务实例的配置文件都是配置在应用程序里面的,如Application.properties等,这么做的第一个缺点是一旦修改配置文件,必须要修改应用且必须重启实例才能使配置生效,第二个缺点是加入我们要部署多个微服务实例的时候,假如使用的是同一个配置文件,那么有多少个节点我们就要修改且重新部署多少次,容易遗漏出错,效率不高。此时,Spring Cloud Config应运而生,他是一个独立部署的微服务,可以实现微服务配置文件的动态获取及刷新,完美的解决了上述两个问题,下面让我们来实现一个简单的Spring Cloud Config实例:
1、Config服务端:
第一步:引入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.spring.cloud</groupId>
<artifactId>config-server</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<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>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
第二步:创建启动主类,并添加@EnableConfigServer注解:
package controller;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
第三步:增加配置文件application.properties:
spring.application.name=config-server
server.port=7001
#git远程仓库地址,具体到项目
spring.cloud.config.server.git.uri=http://xxxxx/xxxxx/test.git
#配置仓库下的相对搜索路径,可以配置多个
spring.cloud.config.server.git.searchPaths=config
#配置git仓库的用户名
spring.cloud.config.server.git.username=username
#配置git仓库的密码
spring.cloud.config.server.git.password=password
到这里,使用一个通过Spring Cloud Config实现,并使用git管理配置内容的分布式配置中心就完成了。
我们可以通过浏览器、Postman、CURL工具直接访问配置内容,访问配置信息的URL和配置文件的映射关系如下所示:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
lable:对应不同的分支,默认是master
config-server是先从git上clone一份配置文件存在本地,然后读取这些内容并返回给微服务应用进行加载,这样做的好处是当无法连接git仓库时,能直接从config-server本地获取配置文件返回给客户端;
2、客户端配置:
第一步:引入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.spring.cloud</groupId>
<artifactId>config-client</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
第二步:创建应用主类:
package controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
第三步:resource目录下面创建bootstrap.properties启动配置文件:
spring.application.name=didispace//对应配置文件规则中的{application}部分
server.port=7002
spring.cloud.config.profile=dev//对应配置文件规则中的{profile}部分
spring.cloud.config.label=master//对应配置文件规则中的{label}部分
spring.cloud.config.uri=http://localhost:7001//配置中心config-server的地址
第四步:测试,新增一个接口返回配置文件中的某个属性:
package controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class TestController {
@Value("${client.param}")
private String form;
@RequestMapping(value = "/form")
public String form() {
return this.form;
}
}