AdSense

網頁

2020/10/29

Spring Cloud Config Client 讀取Config Server配置範例 reading properties from Config server

Spring Cloud Config Client 讀取Config Server中的properties的設定方式如下。

範例環境:

  • Spring Boot 2.3.4.RELEASE
  • Spring Cloud Hoxton.SR8

參考「Spring Cloud Config Server 檔案系統classpath配置範例」配置Config Server並啟動。

Config Client要讀取的Config Server demo-test.properties內容如下。

demo-test.properties

demo.env=DEMO TEST

在Spring Boot Web專案中加入Spring Cloud Config Client的設定如下。

<properties>
    ...
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>

<dependencies>
    ...

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

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

參考完整的pom.xml設定。


設定Config Client的application.yml配置如下,server.port設為8080

application.yml

server:
  servlet:
    context-path: /demo
  port: 8080

在classpath src/main/resources新增bootstrap.yml(或bootstrap.properties)並設定以下。

  • spring.application.name對應Conifg Server的url資源路徑{application}
  • spring.profiles.active對應Conifg Server的url資源路徑{profile}
  • spring.cloud.config.uri指到Conifg Server的domain url http://localhost:8888
  • spring.cloud.config.fail-fast設為true則Config Client無法連線到Config Server時啟動會先報錯。

bootstrap.yml

spring:
  application:
    name: demo
  profiles:
    active: test
  cloud:
    config:
      uri: http://localhost:8888
      fail-fast: true

Config Server可透過下面url格式取得對應的設定檔資源,因此上面設定可取得Config Server的demo-test.properties

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

新增ConfigServerDemoProperties,在類別名稱前加上@ConstructorBinding綁定Config Server的demo-test.properites,並加上@ConfigurationProperties(prefix = "demo")指定讀取properties中以demo為開頭的設定值。

ConfigServerDemoProperties

package com.abc.demo.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@ConstructorBinding
@ConfigurationProperties(prefix = "demo")
public class ConfigServerDemoProperties {

    private final String env; // demo.env

    public ConfigServerDemoProperties(String env) {
        this.env = env;
    }

    public String getEnv() {
        return env;
    }
}

@SpringBootApplication類使用@ConfigurationPropertiesScan掃描所在package下的@ConstructorBinding的類(即ConfigServerDemoProperties)來註冊為bean。

DemoApplication

package com.abc.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@ConfigurationPropertiesScan
@SpringBootApplication
public class DemoApplication {

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

}

到此便完成Config Client的設定。

使用ConfigServerDemoPropertiesTests測試ConfigServerDemoProperties有成功讀取到Config Server demo-test.propertiesdemo.dev設定值DEMO TEST

ConfigServerDemoPropertiesTests

package com.abc.demo.properties;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConfigServerDemoPropertiesTests {

    @Autowired
    private ConfigServerDemoProperties configServerDemoProperties;

    @Value("${demo.env}")
    private String demoEnv;

    @Test
    void getEnv_correct() {

        System.out.println(demoEnv); // DEMO TEST

        String env = configServerDemoProperties.getEnv();
        Assertions.assertEquals("DEMO TEST", env); // pass

    }

}

參考github


沒有留言:

AdSense