Spring Cloud Config 使用Bus的动态配置中心

server端配置

POM文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

 

配置文件

application.yml

server:
  port: 8251
​
spring:
  application:
    name: config-server-bus
  cloud:
    config:
      server:
        git:
          username: git userName
          password: gitPassword
          uri:git Url
          search-paths: /spring-cloud-config
          default-label: spring-cloud-config
    bus:
      trace:
        enabled: true # 启用日志跟踪
  rabbitmq:   # rabbitmq的配置信息
    host: localhost
    port: 5672
    username: guest
    password: guest
​
management:   # 管理端信息
  endpoints:
    web:
      exposure:
        include: bus-env,bus-refresh  # 暴露端点
  endpoint:
    health:
      show-details: always  # 日志显示时机

 

client端配置

POM文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

 

配置文件

bootstrap.yml

server:
  port: 8081
​
spring:
  rabbitmq: # Rabbit Mq 配置信息
    host: localhost
    port: 5672
    username: guest
    password: guest
  profiles:
    active: dev # 启动项目加载的配置文件类型
  application:
    name: config
  cloud:
    config:
      uri: http://localhost:8251  # 配置服务器地址
      name: ${spring.application.name} # 服务名称
      profile: ${spring.profiles.active} # 那个类型的配置为文件  文件类型分为 dev test prod
    bus:
      trace:
        enabled: true
management:
  endpoints:
    web:
      exposure:
        include: bus-env,bus-refresh
  endpoint:
    health:
      show-details: always
    refresh:
      enabled: true

 


原来代码需要修改的

需要在原来的组件类上加该注解@RefreshScope表示这个类是刷新作用域,其他注解保持不变。

组件类定义:使用了类级注解@RestController@Controller@Service@Repository@Component@Configuration的类

 

注意:服务端和客户端都需要依赖amqp的依赖。

posted @ 2019-05-10 11:09  屈小舒  阅读(1000)  评论(0编辑  收藏  举报