欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微服务-配置中心-客户端动态刷新

程序员文章站 2022-03-15 13:43:37
...

1.修改pom文件

<dependencies>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
   <dependency>
       <groupId>com.atguigu.springcloud</groupId>
       <artifactId>cloud-api-commons</artifactId>
       <version>${project.version}</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <scope>runtime</scope>
       <optional>true</optional>
   </dependency>
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
   </dependency>
</dependencies>

2.创建bootstrap.yml

server:
  port: 3355
spring:
  application:
    name: config-client #注册eureka服务器的微服务名
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称http://config-3344.com:3344/master/config-dev.yml
      #更新配置文件curl -X POST "http://localhost:3355/actuator/refresh"
      uri: http://localhost:3344 #配置中心地址
eureka:
  client:
    service-url:
      #设置eureka server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.修改启动类

@EnableEurekaClient
@SpringBootApplication
public class ConfigCenterMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3355.class,args);
    }
}

4.创建controller测试

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

5.访问controller

http://localhost:3355/configInfo

master branch,springcloud-config/config-dev.yml version=4

6.客户端实现动态刷新

在配置中心的配置文件修改后,服务端会实时刷新,客户端需要重新启动才能刷新,如果不想重启,就要按以下步骤配置,这样在服务端修改之后,客户端能实时访问到最新的配置文件。

关键配置

  1. pom
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
  1. yml
#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. controller添加注解
@RefreshScope
  1. cmd脚本
curl -X POST "http://localhost:3355/actuator/refresh"

重新访问http://localhost:3355/configInfo
配置已更新