What is Spring Cloud Tencent
Spring Cloud Tencent is an open source one-stop microservice solution from Tencent. Spring Cloud Tencent implements the Spring Cloud standard microservice SPI, and developers can quickly develop Spring Cloud microservice architecture applications based on Spring Cloud Tencent. The core of Spring Cloud Tencent relies on Tencent's open source one-stop service discovery and governance platform Polarismmesh to realize various distributed microservice scenarios.
The capabilities provided by Spring Cloud Tencent include but are not limited to:
1. Install Polaris
Polaris is Tencent's open source service discovery and governance center, dedicated to solving service visibility, fault tolerance, traffic control and security issues in distributed or microservice architectures. Although there are some components in the industry that can solve some of these problems, there is a lack of a standard, multi-language, framework-agnostic implementation.
Tencent has a large number of distributed services, coupled with the diversity of business lines and technology stacks, and has accumulated dozens of related components, large and small. Starting in 2019, we have abstracted and integrated these components through Polaris to create a unified service discovery and governance solution for the company to help businesses improve R&D efficiency and operational quality.
Polaris installation is very simple, just download the zip of the response platform and run it directly
2. Service registration and discovery
- Service adds polaris-discovery dependency
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
- application.yaml access polaris server
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
- Start the service and observe the polaris console
- Service call example
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer")
public String consumer() {
return restTemplate.getForObject("http://lengleng-tencent-discovery-provider/provider/lengleng", String.class);
}
3. Configuration management
In the Bootstrap stage of the application startup, Spring Cloud will call PolarisConfigFileLocator to obtain the configuration file from the Polaris server and load it into the Spring context. Configuration content can be obtained through Spring Boot's standard @Value and @ConfigurationProperties annotations. The dynamic configuration refresh capability is implemented through the Spring Cloud standard @RefreshScope mechanism.
- Service adds polaris-config dependency
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
</dependency>
- bootstrap.yaml access polaris-config
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8081
config:
groups:
- name: ${spring.application.name}
files: "application"
Special Note: This needs to be configured in bootstrap, spring-cloud-tencent is not adapted to the latest file loading mechanism of spring boot
- Polaris console added configuration
- code usage configuration
@Value("${name:}")
private String name;
4. Service current limit
Service throttling is one of the most common self-protection measures for services to prevent traffic floods from overwhelming services. The Spring Cloud Tencent Rate Limit module has a built-in current limiting filter for Spring Web and Spring WebFlux scenarios, combined with the Polaris current limiting function to help businesses quickly access the current limiting capability.
- Service adds polaris-ratelimit dependency
Add discovery when using current limiting components to facilitate editing in the service list
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
</dependency>
- service access polaris-ratelimit
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
namespace: default
ratelimit:
reject-http-code: 403
reject-request-tips: "lengleng test rate limit"
- Polaris console adds throttling rules
5. Service routing
There are many routing forms that polaris can implement, such as metadata routing, nearest routing, rule routing, custom routing, etc. This article uses metadata routing to demonstrate, the following figure will only route to services with the same metadata information
- Service adds polaris-router dependency
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
</dependency>
- service tag metadata
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
tencent:
metadata:
content:
version: local
6. Current limiting fuse
Fusing fault instances is a common fault-tolerant protection mechanism. The fault instance fuse can realize that the master dispatcher can quickly and automatically shield service instances with high error rate or failure, and start a scheduled task to detect the fuse instance. It is half-open for recovery after the recovery condition is reached. After half-open recovery, a small number of requests are released for real business detection. And according to the real business detection results to determine whether it is completely back to normal.
- Add current limiting fuse related dependencies polaris-circuitbreaker
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-circuitbreaker-spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- Provide Feign service call implementation
The current version of spring-cloud-tencent only supports feign fuse
@FeignClient(contextId = "demoFeign", value = "lengleng-circuitbreaker-tencent-circuitbreaker-provider",
fallback = DemoFeignFallback.class)
public interface DemoFeign {
@GetMapping("/provider")
String get(@RequestParam String name);
}
- Service access polaris-circuitbreaker
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
#开启断路器
feign:
circuitbreaker:
enabled: true
- Write circuit breaker rules polaris.yml
consumer:
circuitBreaker:
checkPeriod: 100ms
chain:
- errorCount
- errorRate
plugin:
errorCount:
continuousErrorThreshold: 1
metricNumBuckets: 1
errorRate:
errorRateThreshold: 100
metricStatTimeWindow: 1s
requestVolumeThreshold: 1
Full-text supporting source code: https://github.com/lltx/spring-cloud-tencent-demo
This article is published by mdnice Multiplatform
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。