天天看點

Spring Cloud Alibaba(二) 配置中心多項目、多配置檔案、分目錄實作

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項目

該項目用來做配置中心用戶端測試之一,以下貼出幾處關鍵代碼

<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: 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>
           

yml配置檔案增加bootstrap.yml,把核心配置移到該配置檔案

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.yml的active調成dev,通路:http://localhost:8002/hello ,傳回:
{
    hello: "ali-nacos-config-client 項目的 dev config",
    myconfig: "ali-nacos-config-client 項目的 myconfig config"
}
           
  • bootstrap.yml的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 示例源碼
  • 原文位址

繼續閱讀