search
HomeJavajavaTutorialSpringCloud Tencent complete solution one

SpringCloud Tencent complete solution one

Jul 14, 2022 pm 02:32 PM
microservicesspring cloudBig Data

What is Spring Cloud Tencent?

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

Project source code address

https://github.com/lltx/spring-cloud-tencent-demo

1. Install Polaris

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

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 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);
}

3. Configuration management

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

  • ##Code usage 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.

  • Service adds polaris-ratelimit dependency,Add discovery when using the current limiting component to facilitate editing of the service list

<dependency>
    <groupid>com.tencent.cloud</groupid>
    <artifactid>spring-cloud-starter-tencent-polaris-discovery</artifactid>
</dependency>

    com.tencent.cloud
    spring-cloud-starter-tencent-polaris-ratelimit
  • 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 current limiting rules

5. Service Routing

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

  • service adds polaris-router Dependencies

<dependency>
    <groupid>com.tencent.cloud</groupid>
    <artifactid>spring-cloud-starter-tencent-polaris-router</artifactid>
</dependency>
  • Service mark metadata

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



    org.springframework.cloud
    spring-cloud-starter-loadbalancer


<dependency>
    <groupid>com.tencent.cloud</groupid>
    <artifactid>spring-cloud-starter-tencent-polaris-discovery</artifactid>
</dependency>


    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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?Apr 19, 2025 pm 08:27 PM

When using MyBatis-Plus or tk.mybatis...

How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?Apr 19, 2025 pm 08:24 PM

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

How to parse next-auth generated JWT token in Java and get information in it?How to parse next-auth generated JWT token in Java and get information in it?Apr 19, 2025 pm 08:21 PM

In processing next-auth generated JWT...

Why can't JavaScript directly obtain hardware information on the user's computer?Why can't JavaScript directly obtain hardware information on the user's computer?Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Circular dependencies appear in the RuoYi framework. How to troubleshoot and solve the problem of dynamicDataSource Bean?Apr 19, 2025 pm 08:12 PM

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?When building a microservice architecture using Spring Cloud Alibaba, do you have to manage each module in a parent-child engineering structure?Apr 19, 2025 pm 08:09 PM

About SpringCloudAlibaba microservices modular development using SpringCloud...

Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Treatment of x² in curve integral: Why can the standard answer be ignored (1/3) x³?Apr 19, 2025 pm 08:06 PM

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)