Home >Java >javaTutorial >SpringCloud Tencent complete solution one
Spring Cloud Tencent is Tencent’s open source one-stop microservice solution. Spring Cloud Tencent implements the Spring Cloud standard microservice SPI. 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 Polarismesh to implement various distributed microservice scenarios.
The capabilities provided by Spring Cloud Tencent include but are not limited to:
Project address:https://github. com/Tencent/spring-cloud-tencent
https://github.com/lltx/spring-cloud-tencent-demo
Polaris is Tencent’s open source service discovery and governance center dedicated to solving service visibility, fault tolerance, flow control and security issues in distributed or microservice architectures. Although "there are already some components in the industry that can solve some of these problems", there is a lack of a standard, multi-language, framework-independent implementation.
Tencent has a large number of distributed services, coupled with the diversity of business lines and technology stacks, it 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 the business improve R&D efficiency and operational quality.
Polaris installation is very simple. Download the zip of the response platform and run it directly,Download address:https://github.com/polarismesh/polaris/releases/tag/v1 .9.0
Service adds polaris-discovery dependency
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> </dependency>
application.yaml Connect to polaris server
spring: cloud: polaris: address: grpc://127.0.0.1:8091
Start service observation 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); }
During the Bootstrap phase of 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,@ConfigurationProperties annotation. Dynamic configuration refresh capability is implemented through Spring Cloud's 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
@Value("${name:}") private String name;4. Service current limiting
Service current limiting is one of the most common service self-protection measures to prevent traffic peaks from overwhelming the service . The Spring Cloud Tencent Rate Limit module has built-in current limiting Filter for Spring Web and Spring WebFlux scenarios, combined with the current limiting function of Polaris to help businesses quickly access the current limiting capability.
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> </dependency>com.tencent.cloud spring-cloud-starter-tencent-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 can implement more routing forms than metadata routing, nearest routing, rule routing, custom routing, etc.,This article Demonstration of metadata routing,As shown below, only services with the same metadata information will be routed
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId> </dependency>
spring: cloud: polaris: address: grpc://127.0.0.1:8091 tencent: metadata: content: version: local
故障实例熔断是常见的一种容错保护机制。故障实例熔断能实现主调方迅速自动屏蔽错误率高或故障的服务实例,并启动定时任务对熔断实例进行探活。在达到恢复条件后对其进行半开恢复。在半开恢复后,释放少量请求去进行真实业务探测。并根据真实业务探测结果去判断是否完全恢复正常。
添加限流熔断相关的依赖 polaris-circuitbreaker
com.tencent.cloud spring-cloud-starter-tencent-polaris-circuitbreaker <dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> </dependency> org.springframework.cloud spring-cloud-starter-loadbalancer org.springframework.cloud spring-cloud-circuitbreaker-spring-retry org.springframework.cloud spring-cloud-starter-openfeign
提供 Feign 服务调用实现
spring-cloud-tencent 当前版本仅支持 feign 熔断
@FeignClient(contextId = "demoFeign", value = "lengleng-circuitbreaker-tencent-circuitbreaker-provider", fallback = DemoFeignFallback.class) public interface DemoFeign { @GetMapping("/provider") String get(@RequestParam String name); }
服务接入 polaris-circuitbreaker
spring: cloud: polaris: address: grpc://127.0.0.1:8091 #开启断路器 feign: circuitbreaker: enabled: true
编写熔断规则 polaris.yml
consumer: circuitBreaker: checkPeriod: 100ms chain: - errorCount - errorRate plugin: errorCount: continuousErrorThreshold: 1 metricNumBuckets: 1 errorRate: errorRateThreshold: 100 metricStatTimeWindow: 1s requestVolumeThreshold: 1
The above is the detailed content of SpringCloud Tencent complete solution one. For more information, please follow other related articles on the PHP Chinese website!