0062-Config Client搭建

1. 前言

上一節Config Server已經獲取到遠程的配置文件,Client端需要連上Server端,從Server端獲取配置,真實環境中所有的微服務都是Client端。

2. Client端搭建

2.1 pom依賴

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

2.2 bootstrap.yml配置文件

bootstrap.yml優先級比application.yml高

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:9800 #服務端地址
      fail-fast: true
      profile: dev    # 啓用的profile
      label: release1.0 # 啓用的分支

2.3 主啓動類

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

    // 配置文件注入
    @Value("${foo}")
    private String foo;
    @Value("${label:master}")
    private String label;

    @GetMapping("/getconfig")
    public String getConfig() {
        return "Current label is " + label + ", and the foo content is " + foo + ".";
    }
}

3. 測試

打開瀏覽器訪問
http://localhost:9900/getconfig出現下面字符串則表示讀取成功

Current label is release1.0, and the foo content is dev foo version.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章