马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

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

[复制链接]
查看2595 | 回复2 | 2019-10-24 09:51:06 | 显示全部楼层 |阅读模式

先容

之前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

  1. <code> <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-config-server</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.alibaba.cloud</groupId>
  12. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-test</artifactId>
  17. <scope>test</scope>
  18. </dependency>
  19. </dependencies></code>
复制代码

application.yml

  1. <code>server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: ali-nacos-config-server
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: localhost:8848
  10. config:
  11. server:
  12. git:
  13. #uri: https://github.com/smltq/spring-boot-demo.git
  14. uri: https://gitee.com/tqlin/spring-boot-demo.git
  15. searchPaths: /cloud-alibaba/config-repo/{application}/
  16. force-pull: true</code>
复制代码

启动类AnConfigServerApplication.java

  1. <code>@SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableConfigServer
  4. public class AnConfigServerApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(AnConfigServerApplication.class, args);
  7. }
  8. }</code>
复制代码

新建ali-nacos-config-client项目

该项目用来做设置中心客户端测试之一,以下贴出几处关键代码

pom.xml

  1. <code> <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.cloud</groupId>
  8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-config</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-actuator</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  21. </dependency>
  22. </dependencies></code>
复制代码

bootstrap.yml

  1. <code>spring:
  2. application:
  3. name: ali-nacos-config-client
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: localhost:8848
  8. config:
  9. name: ${spring.application.name},myconfig
  10. uri: http://localhost:8001/ # config server 设置服务地址
  11. profile: ${spring.profiles.active}
  12. label: master
  13. profiles:
  14. active: pro # 设置文件版本(该示例分为test,dev,pro)</code>
复制代码

写个设置读取测试类HelloController.java

  1. <code>@RestController
  2. public class HelloController {
  3. @Value("${easy.hello}")
  4. private String hello;
  5. @Value("${easy.myconfig}")
  6. private String myconfig;
  7. @RequestMapping("/hello")
  8. public Map hello() {
  9. Map map = new HashMap<>();
  10. map.put("hello", hello);
  11. map.put("myconfig", myconfig);
  12. return map;
  13. }
  14. }</code>
复制代码

启动类AnConfigClientApplication.java

  1. <code>@SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class AnConfigClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(AnConfigClientApplication.class, args);
  6. }
  7. }</code>
复制代码

调整ali-nacos-consumer-feign项目

以下贴出调整部分代码

pom.xml增加spring-cloud-starter-config依赖

  1. <code> <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-openfeign</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.alibaba.cloud</groupId>
  16. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-actuator</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-config</artifactId>
  25. </dependency>
  26. </dependencies></code>
复制代码

yml设置文件增加bootstrap.yml,把核心设置移到该设置文件
bootstrap.yml

  1. <code>spring:
  2. application:
  3. name: ali-nacos-consumer-feign
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: localhost:8848
  8. config:
  9. name: ${spring.application.name}
  10. uri: http://localhost:8001/ # config server 设置服务地址
  11. profile: ${spring.profiles.active}
  12. label: master
  13. profiles:
  14. active: dev # 设置文件版本(该示例分为test,dev,pro)</code>
复制代码

编写设置读写测试类HomeController.java

  1. <code>@RestController
  2. @Slf4j
  3. public class HomeController {
  4. @Autowired
  5. private HelloService helloService;
  6. @Value("${easy.hello}")
  7. private String hello;
  8. @GetMapping(value = "/", produces = "application/json")
  9. public String home() {
  10. log.info("-----------------consumer调用开始-----------------");
  11. String param = "云天";
  12. log.info("斲丧者通报参数:" + param);
  13. String result = helloService.hello(param);
  14. log.info("收到提供者相应:" + result);
  15. return "feign斲丧者" + result;
  16. }
  17. @RequestMapping("/hello")
  18. public Map hello() {
  19. Map map = new HashMap<>();
  20. map.put("hello", hello);
  21. return map;
  22. }
  23. }</code>
复制代码

末了放上设置文件目录规划

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 ,返回:
  1. <code>{
  2. name: "ali-nacos-config-client",
  3. profiles: [
  4. "dev"
  5. ],
  6. label: null,
  7. version: "5456d7ca31d46e91464b6efd3a0831a8208413d9",
  8. state: null,
  9. propertySources: [ ]
  10. }</code>
复制代码
  • 访问:http://localhost:8001/ali-nacos-config-client/test ,返回:
  1. <code>{
  2. name: "ali-nacos-config-client",
  3. profiles: [
  4. "test"
  5. ],
  6. label: null,
  7. version: "5456d7ca31d46e91464b6efd3a0831a8208413d9",
  8. state: null,
  9. propertySources: [ ]
  10. }</code>
复制代码

这表示设置能正确从git上加载到了。

启动ali-nacos-config-client服务,运行客户端测试1

  • bootstrap.yml的active调成dev,访问:http://localhost:8002/hello ,返回:
  1. <code>{
  2. hello: "ali-nacos-config-client 项目的 dev config",
  3. myconfig: "ali-nacos-config-client 项目的 myconfig config"
  4. }</code>
复制代码
  • bootstrap.yml的active调成test,访问:http://localhost:8002/hello ,返回:
  1. <code>{
  2. hello: "ali-nacos-config-client 项目的 test config",
  3. myconfig: "ali-nacos-config-client 项目的 myconfig config"
  4. }</code>
复制代码

表示我git上该项目的2个设置文件都成功读取到了。

启动ali-nacos-consumer-feign项目,测试客户端测试2

访问:http://localhost:9101/hello

返回结果

  1. <code>{
  2. hello: "ali-nacos-consumer-feign 项目的 dev config"
  3. }</code>
复制代码

表示该项目的设置文件加载成功了

资料

  • Spring Cloud Alibaba 示例源码
  • 原文地址






来源:https://www.cnblogs.com/tqlin/p/11725487.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
lesliehuang | 2020-11-2 23:45:44 | 显示全部楼层
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则