spring cloud config 自动刷新配置

spring cloud 的配置中心,在真实的开发中,大多数都采用本地文件存放配置。那么接下来就来说说吧。

首先讲一下配置中心大致的原理:
spring cloud有两种配置文件:即通常的application.yml以及bootstrap.yml两种。先发挥作用的是bootstrap.yml,它根据里面的config server的service-id,到注册中心eureka server找到可用的config server列表,然后选择一个可用的config server来获取配置文件,然后将配置文件跟本项目的application.yml内容合并,然后启动项目。

下面以spring boot 2.3.2.RELEASE ,spring cloud Hoxton SR7版本为例来说明。
好,接下来是相应的代码:对于config server端,pom如下:


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.cloud
    spring-cloud-config-server

config server端的application.yml配置:

server:
   port: 8888
eureka:
  client:
    service-url:
        defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          #search-locations: file:D:/config/dev
          search-locations: classpath:/dev

config server的配置在classpath路径下,即maven的resources目录下,当项目在开发环境时,读取dev下的配置文件。当在生产环境,读取prd下的配置文件。

config server启动类加上注解:@EnableConfigServer

config client端,也就是其余需要从config server拉取配置的节点,pom如下:


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.cloud
    spring-cloud-config-client

config client的application.yml配置:

server:
  port: 8080
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: order-server
config client的application.yml配置:

spring:
  cloud:
    config:
      discovery:
        service-id: config-server
        enabled: true
      name: product

先启动eureka,再启动config,然后启动其余的服务,就可以实现从配置中心读取配置文件了。

验证config server是否正确读取到了配置:

检验服务端是否配置好,比如有一个product.yml的配置文件。访问http://localhost:8888/product.yml是访问不到的,必须加label,比如:http://localhost:8888/product-a.yml 如果浏览器出现了配置文件,则说明配置成功。8888是config server的端口。

说明:有的config client采用了这样的写法,理由是当config server的端口改变的时候,就没法获取到配置了,其实这个就不用担心了。

spring:
  cloud:
    config:
      discovery:
        uri: http://localhost:8888

只要有了下面的一个配置即可,另外config server 的service-id也必须要填写。上面的写法写死了地址,同时如果是config server是集群配置的话,就要写多个,不优雅。

spring:
  cloud:
    config:
      discovery:
        enabled: true

到这里呢,配置中心说完了。但是各位看官一定很纳闷,为什么没有见到自动刷新呢。好,接下来说一说。自动刷新最大的好处就是,再也不用改一个配置,就要重启所有的服务了。

首先的前置准备工作就是需要安装RabbitMQ。

自动配置刷新的原理:spring boot会暴露一个端口,这里的项目指的是config server。当调用这个端口地址的时候,config server就会重新从指定的地方拉取配置,同时把最新的配置放入RabbitMQ,config client作为消费者,消费该消息,替换内存中的旧值。经过上面的步骤,就实现了配置的自动刷新。

config server端,配置文件不能放在classpath下了,因为项目是按照jar或者镜像来启动,因此你很难再去更改配置了,所以把配置放到磁盘目录中:

search-locations: file:D:/config/dev
如果RabbitMQ的端口,用户名密码改变了(默认是5672端口,guest/guest),那么config server、config client都要配置一下RabbitMQ:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

同时config server和config client引入如下pom:


    org.springframework.cloud
    spring-cloud-starter-bus-amqp

config server端的application.yml加入以下配置:

spring:
    bus:
      refresh:
        enabled: true
 management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

config client需要刷新配置的地方加上注解:@RefreshScope。如:

@RefreshScope
@Component
public class EnvController {
    @Value("${aa}")
    private String aa;// 假如配置里面有aa这个键,它的值会改变的
}

依次启动各个项目,然后改配置文件,post请求config server暴露的端口:http://127.0.0.1:8888/actuator/bus-refresh。就可以实现自动刷新配置了。

你可能感兴趣的:(spring cloud config 自动刷新配置)