tencent cloud

Service Registry and Governance

Configuration Management

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-05-07 18:12:22

Scenarios

This document describes how to develop Java applications locally, access PolarisMesh via Spring Cloud Tencent, and utilize the configuration management feature.

Prerequisite

Before starting development, please ensure you have downloaded and installed Java and Maven.

Operation Steps

Step 1: Add Configuration Management Dependencies

1.1 Add the spring cloud tencent Dependency

Modify the pom.xml in the application's root directory to add dependencyManagement:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<version>${version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Note:
Please select the appropriate Spring Cloud Tencent version based on the versions of Spring Boot and Spring Framework in the project.

1.2 Introduce spring cloud tencent starter

Method 1: Introduce all sct starters via spring-cloud-starter-tencent-all.
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-all</artifactId>
</dependency>
Method 2: Only introduce spring-cloud-starter-tencent-polaris-config.
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
</dependency>

Step 2: Add Configuration File

2.1 Configure Polaris (North Star) Configuration Center IP Address

1. Create an application.yml file in the project's main/resources directory.
2. Configure the application name, Polaris (North Star) server IP address, and other information in the application.yml file.
If the Polaris configuration center and registration center are the same Polaris cluster instance, you only need to configure spring.cloud.polaris.address.
If two Polaris cluster instances are deployed for the registration center and configuration center respectively, configure spring.cloud.polaris.address to specify the IP address of the registration center cluster, and configure spring.cloud.polaris.config.address to specify the IP address of the configuration center. As shown below:
3. spring.config.import=optional:Polaris needs to be filled in.
spring:
application:
name: ${application.name}
config:
import: optional:polaris
cloud:
polaris:
address: grpc://${replace with the Polaris service address}:8091 # required
namespace: default # global namespace parameter
config:
address: grpc://${standalone configuration center}:8093 # optional, only required when the configuration center and registration center have different IP addresses
auto-refresh: true # optional, when a configuration is published, dynamically refresh the Spring context, default is true

2.2 Injecting Configuration Files

Our recommended practice is to create a configuration group named after the current application name ${spring.application.name} in the Polaris control plane. Spring Cloud Tencent Config will automatically inject the configurations under the current application name group:
application-${activeProfile}.properties
application-${activeProfile}.yml
application-${activeProfile}.yaml
application.properties
application.yml
application.yaml
bootstrap-${activeProfile}.properties
bootstrap-${activeProfile}.yml
bootstrap-${activeProfile}.yaml
bootstrap.properties
bootstrap.yml
bootstrap.yaml
Note:
The loading order is sequential from top to bottom, meaning configurations loaded earlier have higher priority. Meanwhile, remote configurations take precedence over local configurations, with higher-priority configurations overriding lower-priority ones.
Automatic injection of the above configuration files complies with Spring Boot specifications and can meet most application scenarios. Only when you need to inject additional custom configuration files should you configure spring.cloud.polaris.config.groups in yml, as shown below:
spring:
cloud:
polaris:
config:
groups:
- name: ${spring.application.name} # optional, configuration group for injecting custom configurations
files: [ "config/application.properties", "config/bootstrap.yml" ] # List of custom configuration files to inject. When key conflicts occur, the configuration file earlier in the list takes precedence over those later.

Step 3: Using Configuration in Code

Users can update the configuration information in the code in two ways: Using Configuration Class @ConfigurationProperties and @Value annotations.
@Value is more suitable for scenarios with fewer configurations.
@ConfigurationProperties is more suitable for scenarios with a larger number of configurations.
1. Through @Value injection
@Value("${timeout:1000}")
private int timeout;
2. Through @ConfigurationProperties injection
@Component
@ConfigurationProperties(prefix = "teacher")
public class Person {

private String name;

private int age;

String getName() {
return name;
}

void setName(String name) {
this.name = name;
}

int getAge() {
return age;
}

void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "User{" + "name='" + name + '\\'' + ", age=" + age + '}';
}

}.

Step 4: Add a Configuration File in the Polaris Console

1. To create a namespace, refer to the Service Governance Guide Namespace Management for relevant documentation.
Namespaces are a core concept in Polaris, logically isolating resources by typically identifying different environments and clusters.
Note:
The namespace here must be consistent with that in the application configuration.
2. To create a configuration file group, refer to the Service Governance Guide Configuration Groups for relevant documentation.
The concept of a Polaris configuration file group refers to a collection of configuration files. It is recommended to use the application name as the group name, for example, creating a polaris-config-example group in our sample. Place all configuration files for the polaris-config-example application under the polaris-config-example group to facilitate configuration management.
3. To create and publish a configuration file, refer to the Service Governance Guide Configuration Files for relevant documentation.
The Polaris Configuration Center console allows configuration file names to be displayed in a tree-like directory structure using "/", enabling clear management of configuration files. For example, when an application is divided into different modules with independent configuration files, you can create module1/file1.properties, module1/file2.yaml, module2/file3.yaml.
Note:
Configuration file names are strongly recommended to include file extensions, such as .properties, .yaml, .yml, .json, and so on. The client determines how to parse file content by the filename extension. If the client encounters an unrecognized extension, it defaults to treating the file as a .properties file.

Step 5: Start the Application

Integration of Spring Cloud Tencent Config is now complete.

Step 6: Dynamic Configuration Refresh

6.1 Refresh @Value Property

During application startup, it scans all Beans and builds a mapping relationship between the @Value properties and their corresponding Beans. When property configurations are updated, it identifies all Beans requiring updates and modifies their properties using Java's reflection mechanism.

6.2 Refreshing @ConfigurationProperties Configuration Classes

Compared to @RefreshScope which rebuilds all Beans, the optimized refresh mechanism only needs to rebuild the @ConfigurationProperties Bean corresponding to the updated configuration, resulting in a smaller impact scope. For implementation details, see PolarisRefreshAffectedContextRefresher and AffectedConfigurationPropertiesRebinder.
If you wish to use the conventional @RefreshScope approach to refresh configurations, configure spring.cloud.polaris.config.refresh-type=refresh_context.

6.3 Disable Dynamic Refresh Capability

Configure spring.cloud.polaris.config.auto-refresh=false in bootstrap.yml.

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan