11、SpringCloud Bus刷新Config

一、代码示例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
前面介绍了SpringCloud Config的Server端和Client端,通过某以微服务实现刷新配置,但是实际中微服务有但多,我们不可能每一个都去刷新。下面我们将Config添加到Eureka Server并使用SpringCloud Bus进行刷新。
使用SpringCloud Bus需要使用RabbitMQ或kafka,此文使用的是RabbitMQ。安装方式参照:https://www.cnblogs.com/nongzihong/p/11578255.html
注意Erlang和RabbitMQ的版本一定要一致,不然会很坑。。。
本文的github地址为https://github.com/panli1988/config-server
此例读取的配置文件为/cloud02下的

image.png

此处我们创建三个工程
image.png

具体代码如下:
parent.pom



    4.0.0

    org.example
    cloud02
    1.0-SNAPSHOT
    
        eureka-server-7001
        provider-8001
        provider-8002
    

    pom

    
        1.8
        Greenwich.SR5
        2.1.13.RELEASE
        ${java.version}
        UTF-8
        ${java.version}
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        cloud02
        
            
                src/main/resources
                true
            
        
        
            
                org.apache.maven.plugins
                maven-resources-plugin
                
                    
                        $
                    
                
            
        
    

eureka-server-7001
1、maven依赖

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
    

2、application.yml

server:
  port: 7001
spring:
  application:
    name: eureka-server
  cloud:
    config:
      server:
        git:
          search-paths: /cloud02
          username:
          password:
          default-label: master
          uri: https://github.com/panli1988/config-server
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    #要不要去注册中心获取其他服务的地址
    fetch-registry: false
    #自己就是注册中心,不用注册自己
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    hostname: localhost
    instance-id: eureka-server
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
@EnableConfigServer
public class EurekaServer7001Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer7001Application.class,args);
    }
}

provider-8001
1、maven依赖
2、application.yml
3、启动类
4、其他类
provider-8002
1、maven依赖


        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
    

2、application.yml

#info信息
info:
  app:
    name: provider-8001
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

3、bootstrap.yml

spring:
  application:
    #要读取的配置文件:如git上的文件为provider2-dev.yml
    name: provider
  cloud:
    config:
      label: master
      #环境
      profile: dev
      uri: http://localhost:7001/config

3、启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Provider8001Application {

    public static void main(String[] args) {
        SpringApplication.run(Provider8001Application.class,args);
    }
}

4、其他类
controller类

package org.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ProviderController {

    @Value("${spring.application.name}")
    public String name;

    @Value("${text}")
    public String text;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name){
        return "hello,"+name;
    }

    @GetMapping("/name")
    public String name(){
        return name+"===="+text;
    }
}

provider-8002
1、maven依赖

   
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
    

2、application.yml

#info信息
info:
  app:
    name: provider-8002
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}
spring:
    rabbitmq:
      host: localhost
      port: 5672
      username: guest
      password: guest

3、bootstrap.yml

spring:
  application:
    #要读取的配置文件:如git上的文件为provider2-dev.yml
    name: provider2
  cloud:
    config:
      label: master
      #环境
      profile: dev
      uri: http://localhost:7001/config

4、启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Provider8002Application {

    public static void main(String[] args) {
        SpringApplication.run(Provider8002Application.class,args);
    }
}

5、其他类

package org.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ProviderController {

    @Value("${spring.application.name}")
    public String name;

    @Value("${text}")
    public String text;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name){
        return "hello2,"+name;
    }

    @GetMapping("/name")
    public String name(){
        return name+"===="+text;
    }
}

其实provider-8001和provider-8002区别很小,此处可以不贴代码,为了完整性还是贴一些,且方便后续测试。

2、测试验证

先启动RabbitMQ,再一次启动eureka-server-7001、provider-8001、provider-8002。启动后测试下服务是否正常,不然就笑了。
先访问下http://localhost:7001/

image.png

再访问http://localhost:8001/hello/zs
http://localhost:8001/hello/zs
image.png

还有http://localhost:8002/hello/zs
image.png

以上服务均正常。
下面测试下获取的配置:
image.png

image.png

此处读取了spring.application.name和text两个属性,均正常获取。
特别说明一下:provider-8001和provider-8002分别读取的是provider-profile.yml和provider2-profile.yml,所以我们要修改两个配置文件,此处我们修改text属性:
image.png

image.png

修改后同样需要手动刷新,以post方式访问http://localhost:7001/actuator/bus-refresh,还是用postman进行
image.png

再次访问http://localhost:8001/name
和http://localhost:8002/name
image.png

image.png

provider-80001和provider-8002配置均已更新。
每次手动刷新比较麻烦,github支持Webhooks方式在每次commit时去通知SpringCloud去刷新配置。
image.png

本人没有试过,感兴趣的可以自己试下。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频

你可能感兴趣的:(11、SpringCloud Bus刷新Config)