Updated on 2023-10-18 GMT+08:00

Using the Configuration Center in Spring Cloud

When the configuration center is used in Spring Cloud, you need to add the following dependencies to the project:

<dependency> 
  <groupId>com.huaweicloud</groupId>  
  <artifactId>spring-cloud-starter-huawei-config</artifactId> 
</dependency>

If the dependencies have been directly or indirectly included in the project, you do not need to add them. Spring Cloud contains the configuration items listed in Table 1. The values of these configuration items specify the identity of microservices in the configuration center and the interaction between microservices and the configuration center.

Table 1 Common configuration items of Spring Cloud

Item

Description

Default Value

Remarks

spring.cloud.servicecomb.discovery.appName

Application

default

-

spring.cloud.servicecomb.discovery.serviceName

Microservice name

-

If no service name exists, use spring.application.name.

spring.cloud.servicecomb.discovery.version

Microservice version

-

-

server.env

Environment

-

The value can be production, development, etc.

spring.cloud.servicecomb.config.enabled

Whether to enable dynamic configuration

true

-

spring.cloud.servicecomb.config.serverType

Configuration center type

config-center

  • For microservice engine 1.x, set it to config-center.
  • For microservice engine 2.x, set it to kie (recommended) or config-center.

spring.cloud.servicecomb.config.serverAddr

Access address. The format is http(s)://{ip}:{port}. Use commas (,) to separate multiple addresses.

-

-

spring.cloud.servicecomb.config.fileSource

List of YAML configuration items, which are separated by commas (,)

-

This parameter is valid only when the configuration center is config-center.

Spring Cloud users who use microservice engine 1.x need to frequently add configuration files in YAML format to the configuration center. Spring Cloud Huawei provides the configuration item spring.cloud.servicecomb.config.fileSource to enable users to configure configuration files in YAML format. The value of this configuration item is the key list of the key-value system. Multiple keys are separated by commas (,). The values of these keys are text content in YAML format. Spring Cloud Huawei performs special processing and parsing on the values of these keys.

After accessing the configuration center, you can use the @Value and @ConfigurationProperties labels to inject configurations for Spring Cloud applications. Alternatively, you can also use Environment to read configurations and @RefreshScope to dynamically change configurations. For details, see the developer guide of the community.