SpringCloud 实战二十三:服务配置动态刷新-手动版

  作者:记性不好的阁主

1、修改配置文件


添加监控端口暴露


server:
port: 3355

spring:
application:
name: cloud-client
cloud:
config:
label: master # 分支
name: application # 配置文件名
profile: dev # 读取后缀名
uri: http://localhost:3344 # 配置中心地址

# 拼接后:http://localhost:3344/master/application-dev.yml

eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka


# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"


 2、在配置客户端的控制器上添加配置刷新注解


@RefreshScope


package com.laoxu.springcloud.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RefreshScope
public class ConfigClientController {

@Value("${server.port}")
private String serverPort;

@Value("${config.info}")
private String configInfo;

@GetMapping("/serverPort")
public String getServerPort(){
return serverPort;
}

@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}

}


3、启动服务







  • 修改配置文件










服务并没有动态刷新


4、还需手动发送刷新信号才能生效








刷新成功!!



相关推荐

评论 抢沙发

表情

分类选择