学习笔记:微服务-22 spring cloud config+kafka+spring cloud bus 消息总线实现配置自动更新

上节配置了spring cloud config server,集中管理配置文件,当配置文件发生变化时,客户端通过

curl -X POST http://客户端地址:端口/actuator/refresh 可以刷新客户端配置,但是如果微服务多了,一个微服务有多个实例在运行,需要一个个去刷新,采用bus消息总线,可以实现一键刷新所有实例的配置。

原理图:

学习笔记:微服务-22 spring cloud config+kafka+spring cloud bus 消息总线实现配置自动更新_第1张图片

一、启动kafka (安装参看我的其他文章)

cd  kafka_2.12-2.1.0

启动自带的zookeeper

bin/zookeeper-server-start.sh config/zookeeper.properties &     

启动kafka

bin/kafka-server-start.sh config/server.properties &

查看topic

bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --list

二、spring cloud config  server 配置

1. pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	com.linbsoft
	microservice-config-server-8401
	0.0.1-SNAPSHOT
	microservice-config-server-8401
	Demo project for Spring Boot microservice-microservice-config-server-8401

	
		UTF-8
		UTF-8
		1.8
		Greenwich.M3
	
	
	        
            org.springframework.cloud
            spring-cloud-config-server
        
		
			org.springframework.boot
			spring-boot-starter-web
		
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
       
        org.springframework.cloud
        spring-cloud-starter-config
       
 
    org.springframework.cloud
    spring-cloud-starter-bus-kafka

    
            org.springframework.cloud
            spring-cloud-starter-stream-kafka
   
		
            org.springframework.cloud
            spring-cloud-bus
        

    org.springframework.boot
    spring-boot-actuator

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	
		
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

       其中: spring-cloud-starter-bus-kafka  ,spring-cloud-starter-stream-kafka,spring-cloud-bus是对kafka bus总线的支持

2.  application.properties文件

server.port=8401
spring.application.name=MicroserviceConfigServer8401
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8101/eureka/,http://admin:[email protected]:8102/eureka/
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config

#以下是gitub获取配置文件

#spring.cloud.config.server.git.uri=https://github.com/XXX/spring-cloud-config
#spring.cloud.config.label=master
#spring.cloud.config.server.git.searchPaths=config-repo
#spring.cloud.config.server.git.username=XXX
#spring.cloud.config.server.git.password=***

spring.cloud.bus.enabled=true
spring.kafka.bootstrap-servers=centos7.linbsoft.com:9092
management.endpoints.web.exposure.include=*
spring.cloud.bus.refresh.enabled=true

3. 启动类加注解

@EnableConfigServer

三、spring cloud config client

自动配置客户端设置

1. pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	com.linbsoft
	microservice-serverA-8801
	0.0.1-SNAPSHOT
	microservice-serverA-8801
	Demo project for Spring Boot microservice-microservice-serverA-8801

	
		UTF-8
		UTF-8
		1.8
		Greenwich.M3
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
       
        org.springframework.cloud
        spring-cloud-starter-config
       
  
    org.springframework.cloud
    spring-cloud-starter-bus-kafka

         
            org.springframework.cloud
            spring-cloud-starter-stream-kafka
         
        
        
            org.springframework.cloud
            spring-cloud-bus
        
             
            org.springframework.boot
            spring-boot-actuator-autoconfigure
        

    org.springframework.boot
    spring-boot-starter-actuator

    
    de.codecentric
    spring-boot-admin-starter-client
    2.1.1
  
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	
		
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

像server一样,加入了spring-cloud-starter-bus-kafka  ,spring-cloud-starter-stream-kafka,spring-cloud-bus依赖。

2. bootstrap.properties 这个先于从配置服务器取得的配置文件

spring.application.name=MicroserviceServerA8801
server.port=8801
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8101/eureka/,http://admin:[email protected]:8102/eureka/
#spring.cloud.config.uri=http://centos7.linbsoft.com:8401
spring.cloud.config.enabled=true
spring.cloud.config.profile=dev
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=MicroserviceConfigServer8401
management.endpoints.web.exposure.include=*
management.security.enabled=false
spring.cloud.stream.default-binder=kafka
spring.cloud.bus.enabled=true
spring.kafka.bootstrap-servers=centos7.linbsoft.com:9092
spring.cloud.bus.trace.enabled=true
spring.cloud.bus.refresh.enabled=true

3. 在启动类加入注解

@EnableDiscoveryClient

4. 增加测试类,就是显示姓名,年龄,这两个值是写在配置中心的配置文件中的

@RestController
@RefreshScope
public class TestController {
    
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;

    @RequestMapping("/test")
    public String test(){
        return this.name+this.age;
    }
}

5.配置中心的配置文件

/microservice-config-server-8401/src/main/resources/config/MicroserviceServerA8801-dev.properties

内容简单两行:

name=linbin
age=88

四.测试

1.配置修改前的config server中配置文件

学习笔记:微服务-22 spring cloud config+kafka+spring cloud bus 消息总线实现配置自动更新_第2张图片

2.配置修改前测试客户端读取的配置文件

3. 修改配置文件

4. 保存后查看config server已变化

学习笔记:微服务-22 spring cloud config+kafka+spring cloud bus 消息总线实现配置自动更新_第3张图片

5. 启动刷新,spring cloud config server自动向kafka总线发出消息

(1)为了检测消息,先启动一个消费者进程监控消息

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic springCloudBusOutput --from-beginning

(2)开启另外一个shell,向spring cloud config server发出刷新配置的post请求

curl -X POST http://centos7.linbsoft.com:8401/actuator/bus-refresh

(3)在(1)的监控shell可以看到向kafka发出的消息

学习笔记:微服务-22 spring cloud config+kafka+spring cloud bus 消息总线实现配置自动更新_第4张图片

(5)查看客户端是否更新类配置文件

可以看见客户端已更新了配置文件

 

你可能感兴趣的:(系统集成,java,spring,cloud)