Spring Cloud Alibaba(二) 配置中心多项目、多配置文件、分目录实现

之前Spring Cloud Config基础篇这篇文章介绍了Spring Cloud Config 配置中心基础的实现,今天继续聊下Spring Cloud Config 并结合nacos做服务注册中心,实现多项目、多配置文件、按项目目录划分等功能的配置服务中心。

阅读本篇文章之前,最好要有nacos基础;关于nacos是什么,如何使用,可以参考我的上一篇文章 Spring Cloud Alibaba(一) 如何使用nacos服务注册和发现,或者直接链接到官网教程Nacos 快速开始

本示例主要内容

采用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.yml

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.yml

spring: application: name: ali-nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 config: name: ${spring.application.name},myconfig uri: :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>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxzsz.html