Using the Distributed Registry Center
Spring Cloud Extension
Service Provider Interface (SPI) is a built-in JDK mechanism for service discovery and can be regarded as a type of specifications. You only need to define APIs in specified directories and import standard JAR packages. This method is suitable for third-party developers to implement extension.
However, SPI itself has some problems. For example, during JDK SPI implementation, all extension points will be instantiated at a time, which increases the application startup time and wastes resources.
Spring uses the SPI mechanism for extension. Different from other frameworks, Spring emphasizes openness or scalability. In the Spring framework, many components are integrated from external systems. Various starters are a collection of such components. For example, you can use Consul, Eureka, ZooKeeper, and ETCD for registry and discovery.
Spring Cloud depends on Spring Boot, and Spring Boot depends on Spring. Spring Cloud scalability comes from @EnableAutoConfiguration in Spring Boot. The loading procedure is as follows:
- During the startup of Spring Boot, scan all META-INF/spring.factories files in JAR packages in the class path.
- Read the specified configuration and automatically create a bean based on the condition in the configuration.
- Put the bean into Spring Context. In this way, it is injected to the container and can be used anywhere.
Spring Cloud Huawei provides multi-registry centers.
Getting Started
spring-cloud-huawei supports service registry and discovery in Spring Cloud mode. You only need to modify some configuration files. The procedure is as follows:
- Add the following dependencies.
<dependencies> <dependency> <groupId>com.huaweicloud</groupId> <artifactId>spring-cloud-starter-huawei-servicecomb-discovery</artifactId> </dependency> </dependencies> - Create a project or module and define a configuration file.
Define configurations by setting the application.yml file.
server: port: 8080 spring: application: name: price cloud: servicecomb: discovery: enabled: true address: https://cse.cn-xxxx-2.myhuaweicloud.com serviceName: price version: 0.0.1 healthCheckInterval: 30 credentials: enabled: true accessKey: your ak secretKey: your sk akskCustomCipher: default project: cn-xxxx-2 - Add annotations for start classes. Example:
Note: In the case of Eureka access, if the @EnableEurekaClient annotation is used, delete it.
@SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Running the Demo
The following shows how to implement service registry and discovery based on a project demo. spring-cloud-huawei-sample/discovery-demo provides three projects and is used to simulate the scenario where Spring Cloud projects are migrated to the registry center of Apache ServiceComb.
- price-provider: Price service for service providers; RESTful API.
- order-consumer: Order service for service consumers; RESTful API; invoking price-provider to obtain price information through RestTemplate.
- product-consumer-feign: Product service for service consumers; RESTful API; invoking price-provider to obtain price information through Feign.
Run the demo according to the following procedure:
- Download the local lightweight microservice engine, decompress it, and run start.bat. Then you can access the following address to go to the console.
http://localhost:30106
- Respectively run the main function to start the three demo services.
- Configure the actuator in the application.properties.
management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always management.security.enabled=false
- Check the service running status through the actuator.
http://127.0.0.1:8080/actuator/service-registry
If the service is normal, UP is displayed.
- To change the service status to DOWN, run the following command:
curl -i -H "Content-Type: application/json" -X POST -d '{"status":"DOWN"}' http://localhost:8080/actuator/service-registry
- Configure the actuator in the application.properties.
- Access the service registry center and check the instance running status. The address is as follows:
http://127.0.0.1:30103
- Use the curl, browser, or Postman to verify the invocation.
Address of product-consumer-feign:
http://127.0.0.1:8089/product?id=11
Address of order-consumer:
http://127.0.0.1:8088/order?id=11
Configuration Items
The following table lists the configuration items in the application.yaml file.
| Configuration Item | Key | Default Value |
|---|---|---|
| Enabling ServiceComb service discovery | spring.cloud.servicecomb.discovery.enabled | true |
| Enabling the WebSocket watch mechanism | spring.cloud.servicecomb.discovery.watch | true (Set this parameter to false for professional engines.) |
| Registry center address. You can use commas (,) to separate multiple addresses. | spring.cloud.servicecomb.discovery.address | - |
| Service name | spring.cloud.servicecomb.discovery.serviceName | If no service name exists, use the Spring Cloud application name. |
| Application name | spring.cloud.servicecomb.discovery.appName | default |
| Environment name | server.env | "" |
| Version | spring.cloud.servicecomb.discovery.version | - |
| Enabling health check | spring.cloud.servicecomb.discovery.healthCheck | true |
| Health check interval | spring.cloud.servicecomb.discovery.healthCheckInterval | 10s |
| DNS-resolved IP address (avoiding repeated DNS resolution) | spring.cloud.servicecomb.discovery.autoDiscovery | false |
| Cross-app invoking | spring.cloud.servicecomb.discovery.allowCrossApp | false |
| Name (AZ affinity. After this parameter is configured, instances in the same AZ are preferentially selected.) | spring.cloud.servicecomb.discovery.datacenter.name | - |
| Region (AZ affinity. After this parameter is configured, instances in the same AZ are preferentially selected.) | spring.cloud.servicecomb.discovery.datacenter.region | - |
| AZ (AZ affinity. After this parameter is configured, instances in the same AZ are preferentially selected.) | spring.cloud.servicecomb.discovery.datacenter.availableZone | - |
To interconnect with the professional engine, you need to configure the AK/SK for IAM authentication. The AK/SK configuration items in the application.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 | "" |
Spring Cloud Huawei can automatically obtain the AK/SK. During the configuration, set spring.cloud.servicecomb.credentials.enabled to true. Then, the AK/SK can be automatically injected without being configured.
Last Article: Overview
Next Article: Using the Distributed Configuration Center
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.