config-server-bus动态更新配置

config-server-bus动态更新配置,第1张

config-server用来搭建配置中心,而配置信息一般使用gitlab仓库来存储,这样在你的配置发生改变时,不需要从新打包,而如果使用native的试,则需要从新打一个config-server的jar包。

配置的热更新

当你的服务的配置信息发生改变时,一般来说需要从新重启你的服务,配置信息才能生效,这对于我们来说是不友好的,所以springcloud有一种消息总线的方式来实现配置信息的热更新,更你的服务不需要从新启动。

项目搭建 eureka-server

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

程序添加注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {

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

}

yml文件

server:
 port: 8761

eureka:
 instance:
   hostname: localhost
 client:
  registerWithEureka: false
  fetchRegistry: false
  serviceUrl:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
   enable-self-preservation: false #自我保护机制
   eviction-interval-timer-in-ms: 30000  #及时踢出已关停的节点
config-server

它是配置中心,其它服务如果通过config-server在eureka里的服务名去连接它,这种是以eureka为核心;也可以单独指定,并把eureka的信息写到config-server仓库里,这是以config-server为核心,这两种方式都可以,咱们这个例子是以eureka为核心的,所以config-server也是一个eureka-client.


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


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

程序添加注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigserverApplication {

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

}

添加yml配置

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: [email protected]:***/config_repo.git
          username: ***
          password: ***
          searchPaths: '{profile}'
server:
  port: 8888
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
config-client-service1

这是我们具体的业务服务,它是一个eureka-client也是一个config-client,它需要把自己注册到eureka里,也需要从config-server拉自己的信息,它需要有配置信息的热更新,所以这需要引用amqp包和actuator健康检测包。


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

        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

程序添加注解

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class Service1Application {

  @Value("${auth.name:empty}")
  String author;

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

  @GetMapping("/hello")
  public String hello() {
    return author;
  }
}

添加yml文件

spring:
  cloud:
    bus.trace.enabled: true #配置动态更新
    rabbitmq:
      host: 127.0.0.1
      port: 5672
      username: guest
      password: guest
    config:
      discovery:
        enabled: true #这块表示启用service-id不用uri
        service-id: config-server  #这块是服务的id
      label: master
      profile: svt
  application:
    name: service1
server:
  port: 8081
eureka:
  instance:
    prefer-ip-address: true #基于IP地址的注册而不是主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
logging:
  level:
    org:
      springframework:
        security: DEBUG
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh
配置更新

当你的config_repo仓库有文件更新时,你可以调用任意一个服务去触发它,测试的代码如

 curl -v -X POST "http://localhost:8081/actuator/bus-refresh"

如果成功后,一般会返回httpstatuscode:204的状态码,当然这种也是手动更新,如果希望动态更新,可以在gitlab或者github上对config_repo项目添加webhook的事件,当有分支合并到dev或者master时,去自动触发bus-refresh。

在此我向大家推荐一个架构学习交流圈。交流学习微信:539413949(里面有大量的面试题及答案)里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/729403.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存