微服务Spring-Cloud配置中心自动刷新

只需要添加 bus(rabbitmq )依赖和连接地址 会自动开启自动刷新
访问 http://{ 配置中心地址 }/actuator/bus-refresh 触发自动刷新

重点: 获取属性的属性类上必须要增加@RefreshScope 注解。

@RefreshScope
public class EnvController {
    @Value("${girl.age}")
    private String age;

    @GetMapping("/env")
    public String getGirlInfo(){
        return age;
    }
}

config项目


启动类增加注解

@EnableEurekaClient
@EnableConfigServer

application.yml配置

server:
  port: 8888
spring:
  application:
    name: CONFIG
  rabbitmq:
    host: 192.168.213.128
    port: 5672
    username: guest
    password: guest
  cloud:
    config:
      server:
        git:
          uri: http://git.baozun.com/mis/mis-config-repo.git
          username: hsh10732
          password: azheng@2018
          basedir: D:\Users\hsh10732\MIS\spring-cloud\config-repo
management:
  endpoints:
      web:
        exposure:
          include: "*" #暴露所有url,用于触发自动刷新
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/

添加的依赖

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
}

register项目


启动类增加注解
@EnableEurekaServer

bootstrap.yml配置

spring:
  application:
    name: Eureka #项目名称(必填:会作为文件夹名称前缀查找配置文件)
  cloud:
    config:
      profile: dev #指定环境(必填:会作为文件夹名称后缀查找配置文件)

添加的依赖

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
    runtime 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compileOnly 'org.projectlombok:lombok'
}

对应到git库中的配置文件
Eureka-dev.yml

client项目


启动类增加注解
@EnableEurekaClient

bootstrap.yml配置

spring:
  application:
    name: BiDataApi
  cloud:
    config:
      profile: dev
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/

添加的依赖

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    compileOnly 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

对应到git库中的配置文件
BiDataApi-dev.yml


 本篇
微服务Spring-Cloud配置中心自动刷新 微服务Spring-Cloud配置中心自动刷新
只需要添加 bus(rabbitmq )依赖和连接地址 会自动开启自动刷新访问 http://{ 配置中心地址 }/actuator/bus-refresh 触发自动刷新 重点: 获取属性的属性类上必须要增加@RefreshScope 注解
2019-12-16
下一篇 
java虚拟机内存模型 java虚拟机内存模型
程序计数器记录程序运行的下一条指令的地址,在多线程环境下,每一个线程都有自己的程序计数器虚拟机栈(JVM Stack)虚拟机栈是Java方法执行的内存模型,每个方法执行的时候,会在栈中创建一帧用于存储局部变量表、操作数栈、动态链接、方法出
2019-12-15
  目录