3config 客户端 · SpringCloud微服务实战 · 看云
导航
本节代码地址
1.新建模块fw-cloud-config-native-client
1.1maven配置
加入spring-boot-starter-actuator是为了感应服务端变化
<dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
1.2设置启动代码
@EnableDiscoveryClient
@SpringBootApplication
public class FwConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(FwConfigClientApplication.class, args);
}
}
1.3 加一个controller 用于验证git中配置的值
@RestController
@RefreshScope
@RequestMapping("api")
@Slf4j
public class TestController {
@Value("${version}")
private String versionValue;
@GetMapping("/version")
@ResponseBody
public String returnFormValue(){
log.debug("输出信息{}",versionValue);
return versionValue;
}
}
1.4 添加配置文件(需要用bootstrap.yml)
bootstrap.yml里的配置优先于application.properties和application.yml加载
server:
port: 8779
spring:
application:
name: fw-register-eureka-client #对应 git 的配置文件名称
cloud:
config:
uri: http://localhost:8778/ #Config Server 地址
profile: dev #环境名
label: master #分支名
management:
endpoints:
web:
exposure:
include: refresh,health,info #暴露监控的点
配置中心的更新配置的坑,2.0前调用/refresh更新配置的方法,不再适用。
现在的方法如下:
management.endpoints.web.exposure.include=/actuator/refresh,/actuator/health,/actuator/info
这里我并没有配置eureka信息,在fw-register-eureka-client.yml配置的
日志级别配置的debug模式
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
logging:
level:
com.yisu: debug
version: eureka-1.0
1.5 启动客户端(服务端先起)
可以看到,启动的时候先到Config Server 拉去配置信息
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8778/
1.6 Postman 测试接口
2019-12-17 21:42:10.063 DEBUG 16204 --- [nio-8779-exec-2] c.y.c.client.controller.TestController : 输出信息eureka-2.0
返回结果
eureka-1.0
1.7 eureka 服务信息
1.8 查看服务状态
{
"status": "UP"
}
1.9 修改版本号提交
修改version的内容,并提交
请求localhost:8779/api/version
返回结果
eureka-1.0
没有变化,需要我们refresh一下
localhost:8779/actuator/refresh post请求
返回结果
[
"config.client.version",
"version"
]
再次请求localhost:8779/api/version
返回结果
eureka-2.0
server:
port: 8779
spring:
application:
name: fw-register-eureka-client
cloud:
config:
#uri: http://localhost:8778/ 自己指定的
profile: dev
label: master
discovery: #基于服务发现的
enabled: true
service-id: fw-config-server
management:
endpoints:
web:
exposure:
include: refresh,health,info
- 如果自己指定的方式配置的
Config Server不需要配置eureka和discovery.enabled和discovery.service-id的配置 - 如果基于eureka发现的方式,需要注释掉
spring.cloud.config.uri,加入eureka客户端依赖并且开启discovery.enabled和discovery.service-id的配置
