springcloud实战之10 分布式配置中心(config)介绍了如何用spring cloud config和git仓库读取配置文件,当配置文件里的属性发生变化,按照以往的惯例,必须重启服务才能够读取到新的配置属性。但springcloud config为我们提供了springcloud bus(消息总线)能够实现动态刷新配置文件。
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath />
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Edgware.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class SpringCloudServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudServerConfigApplication.class,args) ;
}
}
server:
port: 8001
management:
security:
enabled: false
spring:
application:
name: springcloud-server-config
cloud:
config:
server:
git:
uri: https://github.com/shiyuan2he/springcloud
search-paths: springcloud-repo-config
username:
password:
force-pull: true
label: master
rabbitmq:
host: 192.168.175.128
port: 5672
username: admin
password: admin
eureka:
client:
service-url:
defaultZone: http://peer1:8080/eureka/
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath />
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Edgware.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudServiceConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudServiceConfigApplication.class,args) ;
}
}
@RestController
@RefreshScope
@RequestMapping(value = "/api/rest")
public class RestfulController {
@Value("${user.name}")
String name;
@Value("${user.age}")
String age ;
@GetMapping("/name")
public String getName(){
return name +":"+ age;
}
}
spring.application.name=springcloud-service-config
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8001/
server.port=8003
eureka.client.serviceUrl.defaultZone=http://peer1:8080/eureka/
## 从配置中心读取文件
spring.cloud.config.discovery.enabled=true
## 配置中心的servieId,即服务名。
spring.cloud.config.discovery.serviceId=springcloud-server-config
spring.cloud.config.fail-fast=true
spring.rabbitmq.host=192.168.175.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
management.security.enabled=false
1.启动服务注册中心集群(springcloud-server-eureka)
2.启动服务配置中心(springcloud-server-config)
3.启动springcloud-service-config集群
启动后的服务注册图如下:
分别调用两个读取配置文件得集群:
去github仓库修改配置文件,并访问之前的集群服务,还没有立马生效
调用http://localhost:8001/bus/refresh 接口刷新配置中心服务
再次查看集群服务,如下图
springcloud-server-eureka
springcloud-server-config
springcloud-service-config
springcloud实战之1 技术选型:dubbo还是springcloud
springcloud实战之2 服务注册与发现(eureka实现)
springcloud实战之3 高可用服务注册中心(eureka集群)
springcloud实战之5 服务消费者(ribbon)
springcloud实战之6 服务消费者(Feign)
springcloud实战之7 断路器(Hystrix)
springcloud实战之8 断路器-仪表盘-单例监控(Hystrix)
springcloud实战之9断路器-集群监控(turbine)
springcloud实战之10 分布式配置中心(config)
springcloud实战之11路由网关(zuul)
springcloud实战之12 路由网关拦截器
springcloud实战之13 rabbitmq消息总线(bus)