SpringCloud体系有分布式配置中心和配置客户端组件,开发者可将开发、测试、生产不同环境下的配置文件,包括微服务配置文件发布到仓库,配置中心对外提供访问接口用于读取配置信息,配置中心可作为微服务注册到服务中心,配置客户端用于从配置中心读取配置文件的特定信息。
配置中心所需依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动类添加**@EnableConfigServer**注解
application.properties配置文件
spring.application.name=config-server
server.port=8000
spring.cloud.config.server.git.uri=https://gitee.com/lindl_dev/blockchain-plat-config/
spring.cloud.config.server.git.searchPaths=service-config
spring.cloud.config.label=master
#公开仓库,可以不填写用户名和密码
spring.cloud.config.server.git.username=xx
spring.cloud.config.server.git.password=xx
这里以8000端口开启配置中心服务,配置文件发布到码云代码仓库
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
以 应用/环境/分支 的方式访问仓库里的配置文件
开发配置客户端引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
bootstrap.properties配置以8001端口开启config-client关联的应用服务,则此客户端访问配置文件名也应该包含config-client
用于测试简单放了一个配置文件
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#dev开发环境配置文件
#test测试环境
#pro正式环境
spring.cloud.config.uri= http://localhost:8000/
server.port=8001
启动类
@RestController
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${blockchain}")
String blockchain;
@RequestMapping(value = "/test")
public String test(){
return blockchain;
}
}
test接口读取blockchain键对应的属性值
高可用服务配置中心
接下来将分布式配置中心和配置客户端改造成微服务注册到服务中心。配置中心通过微服务集群部署,其他微服务请求以服务调用的方式获取配置信息,ribbon的负载均衡处理可使配置服务达到高可用。
config-server工程添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--消息总线 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
application.yml添加
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#配置rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
management.security.enabled=false
ConfigServerApplication启动类添加**@EnableEurekaClient**注解
分布式配置客户端config-client添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.properties将
spring.cloud.config.uri= http://localhost:8000/
替换为
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#从配置中心读取文件
spring.cloud.config.discovery.enabled=true
#配置中心的serviceId
spring.cloud.config.discovery.serviceId=config-server
management.security.enabled=false
management.endpoints.web.exposure.include=*
ConfigClientApplication启动类添加**@RefreshScope**注解
升级
<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>
引用版本为Dalston.SR1,Dalston.RC1版本调用配置中心的/bus/refresh配置客户端获取的旧版本的数据。
配置中心启动的是8000端口,通过http://localhost:8000/bus/refresh刷新所有服务配置信息,客户端就能读到仓库最新的配置信息。
在Git的配置栏下的WebHooks填写工程push调用url地址为配置中心的/bus/refresh请求可实现项目配置信息实时读取。