介绍
之前 Spring Cloud Config 基础篇这篇文章介绍了 Spring Cloud Config 配置中心基础的实现, 今天继续聊下 Spring Cloud Config 并结合 nacos 做服务注册中心, 实现多项目, 多配置文件, 按项目目录划分等功能的配置服务中心.
阅读本篇文章之前, 最好要有 nacos 基础; 关于 nacos 是什么, 如何使用, 可以参考我的上一篇文章 Spring Cloud Alibaba(一) 如何使用 nacos 服务注册和发现, 或者直接链接到官网教程 Nacos 快速开始 https://nacos.io/zh-cn/docs/quick-start.html
本示例主要内容
采用 nacos 做服务注册中心, Spring Cloud Config 做配置服务中心, 在上一篇基础上新建了 ali-nacos-config-server 配置服务中心项目, ali-nacos-config-client 配置客户端项目, 并把 ali-nacos-consumer-feign 配置也调整成从配置中心加载配置
支持多项目, config-repo 配置文件目录按项目名称来规划, 在配置中心 searchPaths: /cloud-alibaba/config-repo/{application}/ 使用 application 自动识别查找目录
支持单项目多配置文件, ali-nacos-config-client 项目的配置文件 spring.cloud.config.name=${spring.application.name},myconfig, 通过指定多个 name 实现多配置文件
实现示例过程
新建 ali-nacos-config-server 项目
该项目用来做配置服务中心, 以下贴出关键部分代码
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
application.YAML
- server:
- port: 8001
- spring:
- application:
- name: ali-nacos-config-server
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848
- config:
- server:
- Git:
- #uri: https://github.com/smltq/spring-boot-demo.git
- uri: https://gitee.com/tqlin/spring-boot-demo.git
- searchPaths: /cloud-alibaba/config-repo/{application}/
- force-pull: true
启动类 AnConfigServerApplication.java
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableConfigServer
- public class AnConfigServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(AnConfigServerApplication.class, args);
- }
- }
新建 ali-nacos-config-client 项目
该项目用来做配置中心客户端测试之一, 以下贴出几处关键代码
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</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-ribbon</artifactId>
- </dependency>
- </dependencies>
Bootstrap.YAML
- spring:
- application:
- name: ali-nacos-config-client
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848
- config:
- name: ${spring.application.name},myconfig
- uri: http://localhost:8001/ # config server 配置服务地址
- profile: ${spring.profiles.active}
- label: master
- profiles:
- active: pro # 配置文件版本 (该示例分为 test,dev,pro)
写个配置读取测试类 HelloController.java
- @RestController
- public class HelloController {
- @Value("${easy.hello}")
- private String hello;
- @Value("${easy.myconfig}")
- private String myconfig;
- @RequestMapping("/hello")
- public Map hello() {
- Map map = new HashMap<>();
- map.put("hello", hello);
- map.put("myconfig", myconfig);
- return map;
- }
- }
启动类 AnConfigClientApplication.java
- @SpringBootApplication
- @EnableDiscoveryClient
- public class AnConfigClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(AnConfigClientApplication.class, args);
- }
- }
调整 ali-nacos-consumer-feign 项目
以下贴出调整部分代码
pom.xml 增加 spring-cloud-starter-config 依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</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-config</artifactId>
- </dependency>
- </dependencies>
YAML 配置文件增加 Bootstrap.YAML, 把核心配置移到该配置文件
Bootstrap.YAML
- spring:
- application:
- name: ali-nacos-consumer-feign
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848
- config:
- name: ${spring.application.name}
- uri: http://localhost:8001/ # config server 配置服务地址
- profile: ${spring.profiles.active}
- label: master
- profiles:
- active: dev # 配置文件版本 (该示例分为 test,dev,pro)
编写配置读写测试类 HomeController.java
- @RestController
- @Slf4j
- public class HomeController {
- @Autowired
- private HelloService helloService;
- @Value("${easy.hello}")
- private String hello;
- @GetMapping(value = "/", produces = "application/json")
- public String home() {
- log.info("-----------------consumer 调用开始 -----------------");
- String param = "云天";
- log.info("消费者传递参数:" + param);
- String result = helloService.hello(param);
- log.info("收到提供者响应:" + result);
- return "feign 消费者" + result;
- }
- @RequestMapping("/hello")
- public Map hello() {
- Map map = new HashMap<>();
- map.put("hello", hello);
- return map;
- }
- }
最后放上配置文件目录规划
config-repo 配置总目录
ali-nacos-config-server 项目 Git 的配置目录
ali-nacos-consumer-feign 项目 Git 的配置目录
使用示例
在上一篇基础上, 我们新建了 2 个项目, 并调整 ali-nacos-consumer-feign 项目使它支持配置远程读取, 有以下三个项目做测试.
ali-nacos-config-server: 配置服务中心, 服务名: ali-nacos-config-server, 端口: 8001
ali-nacos-config-client: 配置客户端 1(消费端), 服务名: ali-nacos-config-client, 端口: 8002
ali-nacos-consumer-feign: 配置客户端 2(消费端), 服务名: ali-nacos-consumer-feign, 端口: 9101
运行测试
首先要启动服务注册中心 nacos
启动 ali-nacos-config-server 服务, 配置服务中心测试
访问: http://localhost:8001/ali-nacos-config-client/dev , 返回:
- {
- name: "ali-nacos-config-client",
- profiles: [
- "dev"
- ],
- label: null,
- version: "5456d7ca31d46e91464b6efd3a0831a8208413d9",
- state: null,
- propertySources: [ ]
- }
访问: http://localhost:8001/ali-nacos-config-client/test , 返回:
- {
- name: "ali-nacos-config-client",
- profiles: [
- "test"
- ],
- label: null,
- version: "5456d7ca31d46e91464b6efd3a0831a8208413d9",
- state: null,
- propertySources: [ ]
- }
这表示配置能正确从 Git 上加载到了.
启动 ali-nacos-config-client 服务, 运行客户端测试 1
Bootstrap.YAML 的 active 调成 dev, 访问: http://localhost:8002/hello , 返回:
- {
- hello: "ali-nacos-config-client 项目的 dev config",
- myconfig: "ali-nacos-config-client 项目的 myconfig config"
- }
Bootstrap.YAML 的 active 调成 test, 访问: http://localhost:8002/hello , 返回:
- {
- hello: "ali-nacos-config-client 项目的 test config",
- myconfig: "ali-nacos-config-client 项目的 myconfig config"
- }
表示我 Git 上该项目的 2 个配置文件都成功读取到了.
启动 ali-nacos-consumer-feign 项目, 测试客户端测试 2
访问: http://localhost:9101/hello
返回结果
- {
- hello: "ali-nacos-consumer-feign 项目的 dev config"
- }
表示该项目的配置文件加载成功了
资料
Spring Cloud Alibaba 示例源码
来源: https://www.cnblogs.com/tqlin/p/11725487.html