Using the Distributed Configuration Center
Spring Cloud Huawei supports two configuration centers:
- Use the distributed configuration center provided by CSE.
- Download the local lightweight microservice engine, which contains the configuration center.
- Connect to the CSE engine.
- Use the servicecomb-kie distributed configuration center to support the long polling push model.
Set up the environment based on the Quick Start in the readme file. Such a distributed configuration center will be rolled out on HUAWEI CLOUD
Getting Started
- Add the following dependencies.
<dependencies> <dependency> <groupId>com.huaweicloud</groupId> <artifactId>spring-cloud-starter-huawei-config</artifactId> </dependency> </dependencies> - Create a project or module and define a configuration file.
The bootstrap.yml configuration file is used. In Spring Cloud, the startup priority of the bootstrap.yml configuration file is higher than that of the application.yml configuration file. When the configuration information is obtained from the configuration center, the related information needs to be defined in the bootstrap.yml configuration file.
spring: application: name: price cloud: servicecomb: config: serverAddr: https://cse.cn-east-3.myhuaweicloud.com watch: delay: 10000 credentials: enabled: true accessKey: your ak secretKey: your sk akskCustomCipher: default - Obtain the value configured in the configuration center from the @Value annotation.
@Value("${server.port}")
Running the Demo
The following shows how to access the distributed configuration center based on a project demo. spring-cloud-huawei-sample/config-demo is used to simulate the scenario where ServiceStage is used to implement distributed configuration in a Spring Cloud project.
- Create related configurations on ServiceStage, for example, dd in the demo. For details, see Global Configuration.
- Modify the bootstrap.yml file and start the service.
spring: application: name: price cloud: servicecomb: config: serverAddr: https://cse.cn-east-3.myhuaweicloud.com watch: delay: 10000 credentials: enabled: true accessKey: your ak secretKey: your sk akskCustomCipher: default - Implement verification.
Access the following address (the value of dd configured in the configuration center will then be displayed):
http://127.0.0.1:8080/price?id=11
Configuration Items
The following table lists the configuration items in the bootstrap.yaml file.
| Configuration Item | Key | Default Value |
|---|---|---|
| ServiceComb start | spring.cloud.servicecomb.config.enabled | true |
| Service name | spring.cloud.servicecomb.discovery.serviceName | If no service name exists, use spring.application.name. |
| Application name | spring.cloud.servicecomb.discovery.appName | default |
| Environment name | server.env | "" |
| Version | spring.cloud.servicecomb.discovery.version | - |
| Configuration center address. You can use commas (,) to separate multiple addresses. | spring.cloud.servicecomb.config.serverAddr | "" |
| Configuration center type. (The value can be kie, indicating the access kie. It is connected to config-center by default.) | spring.cloud.servicecomb.config.serverType | - |
| long polling (supported by only kie). | spring.cloud.servicecomb.config.enableLongPolling | true |
| Default polling configuration period | spring.cloud.servicecomb.config.watch.delay | 10*1000 ms |
| Waiting time for establishing a long polling | spring.cloud.servicecomb.config.watch.pollingWaitTimeInSeconds | 20 s |
To interconnect with the professional engine, you need to configure the AK/SK for IAM authentication. The AK/SK configuration items in the bootstrap.yaml file are described as follows:
| Configuration Item | Key | Default Value |
|---|---|---|
| Authentication enablement | spring.cloud.servicecomb.credentials.enabled | false |
| Tenant AK | spring.cloud.servicecomb.credentials.accessKey | - |
| Tenant SK | spring.cloud.servicecomb.credentials.secretKey | - |
| Cipher (set it to default) | spring.cloud.servicecomb.credentials.akskCustomCipher | default |
| Region | spring.cloud.servicecomb.credentials.project | "" |
Last Article: Using the Distributed Registry Center
Next Article: Accessing Gateways
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.