《 Rest微服务加入Config实现分布式配置中心客户端的配置 》
前言
在上一篇文章中,主要完成了 《 Rest微服务加入Config实现分布式配置中心服务端的配置 》,本篇将带领读者使用基于 SpringCloud Config 分布式配置中心客户端的构建工作,涉及的服务模块包括:
- 新增配置中心微服务配置文件,文件名为 “ client-microservice-config-center.yml ”;
- 新构建基于 SpringCloud Config 的分布式配置中心客户端服务模块模块,主要作用为测试从分布式配置中心服务器模块中获取指定的配置内容,模块名为 “ microservice-config-client-4001 ”;
Config实现分布式配置中心客户端的配置
1、模拟系统运维,在上一篇博客的相同文件夹目录下新建一个客户端配置文件 “ client-microservice-config-center.yml ” ,完整配置内容如下:
spring:
profiles:
active:
- dev # 默认的开发环境
---
server:
port: 8001
spring:
profiles: dev # 开发环境
application:
name: dev-etcp-microservice-config-center
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka/
---
server:
port: 8002
spring:
profiles: test # 测试环境
application:
name: test-etcp-microservice-config-center
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka/
---
server:
port: 8003
spring:
profiles: prod # 生产环境
application:
name: prod-etcp-microservice-config-center
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka/
2、将上一步新建的配置文件,推送到远程 GitHub 仓库种,使用命令
“ git add . ”
“ git commit -a -m " 版本注释 " ”
“ git push origin master ”
如下图:
3、新建 Spring Cloud Config 分布式配置中心客户端服务模块,服务名称 “ microservice-config-client-4001 ”
新增 POM 配置文件,完整配置内容如下:
<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.huazai.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>${project.version}</version>
</parent>
<!-- 当前Module的名字 -->
<artifactId>microservice-config-client-4001</artifactId>
<dependencies>
<!-- SpringCloudConfig 相关 -->
<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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- springboot 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 热部署插件相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
新增 “ bootstrap.yml ” 配置文件,完整内容如下:
spring:
cloud:
config:
name: client-microservice-config-center # 从github上读取指定名称的资源配置信息,注意不加任何后缀
profile: dev # 配置默认的环境
label: master # 从 master 分支上获取配置信息
uri: http://www.config5001.com:5001 # 本地配置客户端启动后,会去找5001config服务端服务器,通过SpringCloudConfig获取GitHub的服务地址
关于 “ bootstrap.yml ” 是什么,在前已经详细的介绍了,就不再这儿重复兹述了,我们只需要知道 “ bootstrap.yml ” 是系统级的配置文件,而 “ application.yml ” 是用户级别的,也就是说 “ bootstrap.yml ” 配置文件的加载优先级要高于 “ application.yml ”。
新增 “ application.yml ” ,完整配置类容如下:
spring:
application:
name: client-microservice-config-center # 从github上读取指定名称的资源配置信息,注意不加任何后缀
新建一个 ConfigClientController 类,用户测试能否正常从 GitHub 上获取环境配置内容,ConfigClientController 类的完整类容如下:
package com.huazia.springcloud.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description TODO
* </ul>
* @className ConfigClientController
* @package com.huazia.springcloud
* @createdTime 2018年05月31日 下午7:06:38
*
* @version V1.0.0
*/
@RestController
@RequestMapping("/config/client")
public class ConfigClientController
{
/**
* 应用名称
*/
@Value("${spring.application.name}")
private String applicationName;
/**
* 当前使用环境
*/
@Value("${spring.profiles}")
private String env;
/**
* 端口号
*/
@Value("${server.port}")
private String port;
/**
* 使用的那台 eureka 服务器
*/
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaService;
@RequestMapping("/getInfo")
public Map<String, String> getConfigInfo(){
Map<String, String> map = new HashMap<>();
map.put("applicationName", applicationName);
map.put("env", env);
map.put("port", port);
map.put("eurekaService", eurekaService);
return map;
}
}
新增主启动类 “ MicroserviceConfigClientApp_4001 ” ,完整内容如下:
package com.huazia.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description TODO
* </ul>
* @className MicroserviceConfigClientApp_4001
* @package com.huazia.springcloud
* @createdTime 2018年05月31日 下午7:07:07
*
* @version V1.0.0
*/
@SpringBootApplication
public class MicroserviceConfigClientApp_4001
{
public static void main(String[] args)
{
SpringApplication.run(MicroserviceConfigClientApp_4001.class, args);
}
}
Spring Cloud Config 分布式配置中心的客户端构建完成后,项目总览如下图:
4、测试
首先启动 Spring Cloud Config 分布式配置中心服务端模块,再启动 Spring Cloud Config 分布式配置中心客户端模块。
本次使用的 dev 开发环境,所以获取的环境配置内容就是与 dev 环境相关的配置内容,如下图:
注意:关于获取的环境配置内容,取决于 bootstrap.yml 配置文件中的 “ name: ” 属性值指定使用配置中心的那个配置文件和 “ profile: ” 属性值指定那个具体的环境。
GitLab 源码地址:
项目源码地址(zip格式的工程包):
好了,关于 Spring Cloud 进阶--Rest微服务加入Config实现分布式配置中心客户端的配置 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。
作 者: | 华 仔 |
联系作者: | [email protected] |
来 源: | CSDN (Chinese Software Developer Network) |
原 文: | https://blog.csdn.net/Hello_World_QWP/article/details/88087856 |
版权声明: | 本文为博主原创文章,请在转载时务必注明博文出处! |