Spring Cloud Config + Spring Cloud Bus + kafka实现配置中心配置动态更新


一、架构图

当在 Git 仓库中某个应用配置文件中的参数更新后,只需要通过 POST方法访问 config Server 的 /actuator/bus-refresh 接口,就可以让所以的微服务节点更新配置。 在我们的 DEMO 中有一个 Config Server 和 两个 Config Client。

二、下载和部署 kafka

到 kafka 官网下载 kafka 2.12-2 解压到 E:\软件3\JAVA\消息中间件。

cd E:\软件3\JAVA\消息中间件\kafka_2.12-2.0.0

-- 启动

bin\windows\zookeeper-server-start.bat config\zookeeper.properties

-- 启动 kaffka

bin\windows\kafka-server-start.bat config/server.properties

-- 创建 Topic (可理解为创建消息队列)

bin\windows\kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

-- 生成消息

bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test

-- 消费消息

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning

三、Config Server

1. pom.xml

<?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.0modelVersion>
 
    <groupId>com.sandegroupId>
    <artifactId>config-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
 
    <name>config-servername>
    <description>Demo project for Spring Bootdescription>
 
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
         
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-serverartifactId>
        dependency>
        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bus-kafkaartifactId>
            <version>2.0.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-stream-kafkaartifactId>
            <version>2.0.1.RELEASEversion>
         dependency>
        
        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-busartifactId>
            <version>2.0.0.RELEASEversion>
        dependency>
         
        <dependency>
            <groupId>io.micrometergroupId>
            <artifactId>micrometer-coreartifactId>
        dependency>
        
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
            <version>1.4.4.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        
    dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
 
 
project>

spring-cloud-starter-bus-kafka、spring-cloud-starter-stream-kafka、spring-cloud-bus 这个几个依赖配置是实现配置中心配置动态刷新需要的。

注意:

spring-boot-starter-amqp、spring-cloud-starter-bus-amqp、spring-cloud-starter-stream-rabbit 这三项依赖是集成RabbitMQ使用的。Kafka 和 RabbitMQ 只能配置一个,如果 kafka 和 RabbitMQ 依赖包同时存在,启动时会报错,所以这些依赖必须注释掉。错误如下:

Failed to start bean 'outputBindingLifecycle'; nested exception is java.lang.IllegalStateException: A default binder has been requested, but there is more than one binder available for 'org.springframework.integration.channel.DirectChannel' : kafka,rabbit

2. src/main/resources/application.properties

server.port=7001
spring.application.name=config-server
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka/,http://localhost:1111/eureka/
spring.cloud.config.server.git.uri=https://gitee.com/lixiaxin200319/config-repo
spring.cloud.config.server.git.username=lixiaxin200319
spring.cloud.config.server.git.password=HUI2011kai0421
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.label=master
#spring.cloud.config.server.git.repos.dev.pattern=dev/*
#spring.cloud.config.server.git.repos.dev=https://gitee.com/lixiaxin200319/config-repo/dev
spring.cloud.config.server.git.basedir=E:\\JAVA\\Spring\\config-server\\src\\main\\resources\\repos

3. src/main/resources/bootstap.properties

encrypt.key-store.location=file:///E:/JAVA/Spring/config-server/src/main/resources/shared/config-server.keystore3
encrypt.key-store.alias=config-server3
encrypt.key-store.password=111111
#encrypt.key-store.secret=222222
#encrypt.key=didispace
#spring.rabbitmq.host=localhost
#spring.rabbitmq.port=5672
#spring.rabbitmq.username=springcloud
#spring.rabbitmq.password=123456
#spring.cloud.stream.kafka.binder.brokers=localhost
#spring.cloud.stream.kafka.binder.defaultBrokerPort=9092
#spring.cloud.stream.kafka.binder.zkNodes=localhost
#spring.cloud.stream.kafka.binder.defaultZkPort=2181
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=test
management.endpoints.web.exposure.include=*
spring.cloud.bus.refresh.enabled=true

下面的几个配置项是集成 kafka 需要的:

spring.kafka.bootstrap-servers=localhost:9092 // 配置 kafka 服务器的地址和端口
spring.kafka.consumer.group-id=test // 配置kafka 服务器的Topic (可理解为创建消息队列)
management.endpoints.web.exposure.include=*
spring.cloud.bus.refresh.enabled=true

注意:在 bootstap.properties 配置文件中 management.endpoints.web.exposure.include=* 不能写成 management.endpoints.web.exposure.include='*' ,配置值加了当引号虽然不报错但配置不会生效。

4. 应用主类

package com.sande.configserver;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
 
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

四、Config Client 节点1

1. pom.xml

<?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.0modelVersion>
 
    <groupId>com.sandegroupId>
    <artifactId>config-clientartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
 
    <name>config-clientname>
    <description>Demo project for Spring Bootdescription>
 
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bus-kafkaartifactId>
        dependency>
        
          <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-stream-kafkaartifactId>
            <version>2.0.1.RELEASEversion>
         dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-busartifactId>
            <version>2.0.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>io.micrometergroupId>
            <artifactId>micrometer-coreartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-actuator-autoconfigureartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
            <version>1.4.4.RELEASEversion>
        dependency>
        
       
 
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
 
    dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
 
 
project>

spring-cloud-starter-bus-kafka、spring-cloud-starter-stream-kafka、spring-cloud-bus 这个几个依赖配置是实现配置中心配置动态刷新需要的。

注意:

spring-boot-starter-amqp、spring-cloud-starter-bus-amqp、spring-cloud-starter-stream-rabbit 这三项依赖是集成RabbitMQ使用的。Kafka 和 RabbitMQ 只能配置一个,如果 kafka 和 RabbitMQ 依赖包同时存在,启动时会报错。错误如下:

Failed to start bean 'outputBindingLifecycle'; nested exception is java.lang.IllegalStateException: A default binder has been requested, but there is more than one binder available for 'org.springframework.integration.channel.DirectChannel' : kafka,rabbit

2. src/main/resources/bootstrap.yml

spring: 
  application: 
    name: sdcc
  cloud:
    config: 
      #uri: http://localhost:7001
      profile: dev
      label: master
      #fail-fast: true
      discovery:
        service-id: config-server
        enabled: true
    bus:
      refresh:
        enabled: true
      #username: user
      #password: 123456
    
          
  #rabbitmq:
   # host: localhost
   #port: 5672
    #username: springcloud
    #password: 123456
  kafka:
    bootstrap-servers:
    - localhost:9092
server:
  port: 7002
eureka: 
  client: 
     serviceUrl: 
        defaultZone: http://localhost:1112/eureka/,http://localhost:1111/eureka/
management: 
    endpoints: 
        web: 
            exposure: 
                include: '*'
 
 

注意:在 bootstrap.yml 文件中

的配置值必须为 '*',如果没有单引号配置不会生效。

该配置对应的是 bootstrap.properties 配置文件中的 spring.kafka.bootstrap-servers=localhost:9092 。注意在 config-client 节点不能配置 spring.kafka.consumer.group-id=test ,一旦配置了通过 POST方法访问 config Server 的 /actuator/bus-refresh 接口就只能更新某一台 config-client 的配置,其他 config-client节点的配置不会更新。

3. 应用主类

package com.sande.configclient;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
    
    
}
 
@RefreshScope
@RestController
class MessageRestController {
 
    @Value("${message:Hello default}")
    private String message;
    
  
 
    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
    
   /* @Value("${info}")
    private String info;
    
  
    @RequestMapping("/info")
    String getInfo() {
        return this.info;
    }*/
    
    @Value("${sdccSellLimit}")
    private String sdccSellLimit;
 
    @RequestMapping("/sdccSellLimit")
    String getSdccSellLimit() {
        return this.sdccSellLimit;
    }
    
    @Value("${sdccSellLimitNumber}")
    private String sdccSellLimitNumber;
 
    @RequestMapping("/sdccSellLimitNumber")
    String getSdccSellLimitNumber() {
        return this.sdccSellLimitNumber;
    }
    
    @Value("${username}")
    private String username;
 
    @RequestMapping("/username")
    String getName() {
        return this.username;
    }
    
    @Value("${password}")
    private String password;
 
    @RequestMapping("/password")
    String getId() {
        return this.password;
    }
    
    /*@Value("${prod_version}")
    private String prod_version;
    
    @RequestMapping("/prod_version")
    String getProd_version() {
        return this.prod_version;
    }*/
    
   
    
 
}

五、Config Client 节点 2

节点2监听的端口是 7003 ,其他的配置及应用主类都完全和 Config Client 节点1 一样。

六、启动注册中心、Config Server 、Config Client 进行测试。

我们 DEMO 在码云创建的仓库 。sdcc 应用中 sdcc-dev.properties 配置文件的 sdccSellLimit=0.6

通过 http://localhost:7002/sdccSellLimit 访问 Config Client 1 , http://localhost:7003/sdccSellLimit 访问 Config Client 2 结果都是 0.6。

在代码仓库把 sdccSellLimit 修改为 0.8。然后使用 POST 方法访问 Config Server 的 /actuator/bus-refresh 接口,动态更新所有 Config Client 的参数。

 我们看到两个 Config Cleint 节点都已经动态更新了参数。