SpringCloud創建Config Client配置讀取


1.說明

本文詳細介紹配置中心客戶端使用方法,
即Config Client到Config Server讀取配置,
這里以創建Config Client服務為例,
基於已經創建好的Config Server模塊,
請參考SpringCloud創建Config模塊,
到配置中心讀取配置。

2.創建工程,添加依賴

在父工程下面創建一個Maven模塊config-client,
在pom.xml中增加config client的依賴:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

其中spring-boot-starter-web是spring boot應用,
可以對外提供Rest服務,
下面會通過Rest接口查詢客戶端讀取到的配置。

3.新增配置文件

新增配置文件bootstrap.yml:

server:
  port: 8001

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config-client-demo
      profile: test
      uri: http://localhost:9009

上面的spring.cloud.config配置,
指定了配置中心http://localhost:9009
拉取master分支下的config-client-demo.yml中的test配置。

4.git倉庫增加配置

由於http://localhost:9009配置中心,
指定了spring-cloud-config倉庫的地址:

https://gitee.com/bugzeroman/spring-cloud-config.git

需要把config-client-demo.yml上傳到倉庫,
這樣客戶端才能通過配置中心拉取到配置文件。
config-client-demo.yml內容:

spring:
  profiles:
    active:
    - test
    
---
# 開發環境
spring:
  profiles: dev
  application:
    name: config-server-dev

config:
  info: config info dev

---
# 測試環境
spring:
  profiles: test
  application:
    name: config-server-test
config:
  info: config info test

5.新增查詢接口

ConfigClientController.java對外提供config.info參數的查詢,
config.info是從配置中心讀取的。

package com.yuwen.spring.config.client.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

6.新增啟動類

主啟動類ConfigClientApplication.java,
只要加上SpringBootApplication注解:

package com.yuwen.spring.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

7.啟動服務

Config Client服務啟動日志如下,
注意要先啟動Config Server服務:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-09-11 15:59:00.454  INFO 19236 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:9009
2020-09-11 15:59:01.868  INFO 19236 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client-demo, profiles=[dev], label=master, version=befaf0fdb3897ac7b7de2a8df3192decb0192cb7, state=null
2020-09-11 15:59:01.869  INFO 19236 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/config-client-demo.yml (document #1)'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/application.yml (document #1)'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/config-client-demo.yml (document #0)'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/bugzeroman/spring-cloud-config.git/application.yml (document #0)'}]
2020-09-11 15:59:01.874  INFO 19236 --- [           main] c.y.s.c.client.ConfigClientApplication   : No active profile set, falling back to default profiles: default
2020-09-11 15:59:02.239  INFO 19236 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=7db33b17-4254-3471-a240-faf5a3bf9fa9
2020-09-11 15:59:02.660  INFO 19236 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8001 (http)
2020-09-11 15:59:02.668  INFO 19236 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-09-11 15:59:02.668  INFO 19236 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-09-11 15:59:02.750  INFO 19236 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-09-11 15:59:02.750  INFO 19236 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 865 ms
2020-09-11 15:59:02.847  INFO 19236 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-11 15:59:03.248  INFO 19236 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8001 (http) with context path ''
2020-09-11 15:59:03.531  INFO 19236 --- [           main] c.y.s.c.client.ConfigClientApplication   : Started ConfigClientApplication in 4.184 seconds (JVM running for 4.49)
2020-09-11 15:59:30.056  INFO 19236 --- [nio-8001-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-11 15:59:30.056  INFO 19236 --- [nio-8001-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-09-11 15:59:30.060  INFO 19236 --- [nio-8001-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms

8.測試服務

訪問Config Client提供的Rest服務:
http://localhost:8001/configInfo

修改bootstrap.yml中spring.cloud.config.profile為dev,
然后重新啟動,再次訪問上面的接口:


可以看到兩次分別拉取到了config-client-demo.yml中對應的配置。

9.參考文章

Spring Cloud Config Server 官方手冊
Spring Cloud Config 中文手冊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM