2019独角兽企业重金招聘Python工程师标准>>>
一, 简介
既然项目中已经有了配置中心 config-server , 那么我们怎么去消费配置服务呢? 当然就是 config-client ;
二, 创建 config-client 服务 cloud-g
1, pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gy.cloud</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-g</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Demo project for config-client</description>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 配置刷新 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2, bootstrap.properties
# 和 application 配置文件相比, bootstrap 配置文件具有以下几个特性:
# 1, bootstrap 由父 ApplicationContext 加载,比 application 优先加载;
# 2, bootstrap 里面的属性不能被覆盖;
# 3, bootstrap 和application 的应用场景:
# application: 主要用于spring boot 项目的自动化配置;
# bootstrap:
# a, 使用 spring Cloud config 配置中心时, 这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;
# b, 一些固定的不能被覆盖的配置; c, 一些加密/解密的场景;
server.port=8767
spring.application.name=config-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)
#spring.cloud.config.uri= http://localhost:8766/
spring.cloud.config.label=master
spring.cloud.config.profile=dev
# 开放配置刷新接口 /actuator/refresh
management.endpoints.web.exposure.include=refresh
3, CloudGApplication
package com.gy.cloud.cloudg;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope // 配置刷新
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGApplication {
public static void main(String[] args) {
SpringApplication.run(CloudGApplication.class, args);
System.out.println("=== 服务G config-client 启动SUCCESS ===");
}
@Value("${testUsername}")
private String param;
@GetMapping("getConfigParam")
public String getConfigParam() {
return param;
}
}
4, 启动 cloud-g
启动日志 :
2019-01-11 11:03:04.093 INFO 10844 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://XUQ3937QAYKT5WO:8766/
2019-01-11 11:03:04.729 INFO 10844 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=f2f23650522db4787e8a2a7d3b030058a8598899, state=null
2019-01-11 11:03:04.730 INFO 10844 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/ge.yang/SpringBoot//src/main/resources/config/config-client-dev.properties'}]}
......
2019-01-11 11:03:09.462 INFO 10844 --- [ main] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient
2019-01-11 11:03:09.474 INFO 10844 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2019-01-11 11:03:09.486 INFO 10844 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
根据启动日志, 发现 POST 接口 : /actuator/refresh
1, 访问 : http://localhost:8767/getConfigParam
2, 修改Git仓库 testUsername 值 访问 : http://localhost:8767/getConfigParam
3, POST 访问 : http://localhost:8767/actuator/refresh
4, 访问 : http://localhost:8767/getConfigParam
三, 总结
注意 1, bootstrap 与 application 的区别;
2, 注意 POST /actuator/refresh 刷新接口的使用;
学习文档
方志朋的博客 : https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f7-config/
项目源码: https://gitee.com/ge.yang/spring-demo/tree/master/cloud