服务熔断

服务提供者

项目初始化

使用 jetbrain idea 等工具初始化一个 maven 项目

引入依赖

在上一步初始化好一个 maven 项目之后,我们在 pom.xml 中引入 Spring Cloud Tencent 相关依赖。

  • 引入 spring-cloud-tencent-dependencies 进行管理 Spring Cloud Tencent 相关组件的依赖版本。
  • 引入 spring-cloud-starter-tencent-polaris-discovery 实现 Spring Cloud 服务注册到北极星中。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    ...

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tencent.cloud</groupId>
                <artifactId>spring-cloud-tencent-dependencies</artifactId>
                <version>1.7.0-2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- 简单的 Spring Cloud Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入 Spring Cloud Tencent 的服务注册发现依赖 -->
        <dependency>
            <groupId>com.tencent.cloud</groupId>
            <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
        </dependency>
    </dependencies>

    ...

</project>

配置 application.yml

在 resources 目录下创建 application.yml,并按照如下进行配置

.
├── java
│   └── com
│       └── example
│           └── springcloudpolarisquickstart
│               └── SpringCloudProviderApplication.java
└── resources
    └── application.yml

配置 application.yml

server:
  port: 28888
spring:
  application:
    name: CircuitBreakerEchoServer

  cloud:
    tencent:
      metadata:
        content:
          env: dev1

    polaris:
      namespace: default # 设置注册中心命名空间
      discovery:
        enabled: true
      stat:
        enabled: true
        port: 28082
      address: grpc://127.0.0.1:8091

示例代码

@SpringBootApplication
public class SpringCloudProviderApplication {

    @RestController
    static class EchoController {

        private final PolarisDiscoveryProperties properties;

        EchoController(PolarisDiscoveryProperties properties) {
            this.properties = properties;
        }

        @GetMapping(value = "/echo/{string}")
        public ResponseEntity<String> echo(@PathVariable String string) {
            String badService = System.getProperty("BAD_SERVICE");
            if (Objects.equals(badService, "true")) {
                return new ResponseEntity<>("I'm bad, " + properties.getService() + ":" + properties.getPort(), HttpStatus.BAD_GATEWAY);
            }

            String val = "Hello PolarisMesh " + string + ", I'm " + properties.getService() + ":" + properties.getPort();
            return new ResponseEntity<>(val, HttpStatus.OK);
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudProviderApplication.class, args);
    }
}

运行

  1. 执行 mvn clean install 构建出 jar 运行包
  2. 按照以下命令启动两个服务提供者进程

启动第一个服务提供者

java -DBAD_SERVICE=false -Dserver.port=20001 -Dspring.cloud.polaris.stat.port=21001 -jar xxx.jar 

启动第二个服务提供者

java -DBAD_SERVICE=true -Dserver.port=20002 -Dspring.cloud.polaris.stat.port=21002 -jar xxx.jar 

确认实例注册情况

执行以下命令,查看该服务下的实例信息

curl --location --request POST '127.0.0.1:8090/v1/Discover' \
--header 'Content-Type: application/json' \
--data-raw '{
    "type": 1,
    "service": {
        "name": "CircuitBreakerEchoServer",
        "namespace": "default"
    }
}'

服务调用者

项目初始化

使用 jetbrain idea 等工具初始化一个 maven 项目

引入依赖

在上一步初始化好一个 maven 项目之后,我们在 pom.xml 中引入 Spring Cloud Tencent 相关依赖。

  • 引入 spring-cloud-tencent-dependencies 进行管理 Spring Cloud Tencent 相关组件的依赖版本。
  • 引入 spring-cloud-starter-tencent-polaris-discovery 实现 Spring Cloud 服务注册到北极星中。
  • 引入 spring-cloud-starter-tencent-polaris-circuitbreaker 实现 Spring Cloud 的服务调用熔断。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    ...
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tencent.cloud</groupId>
                <artifactId>spring-cloud-tencent-dependencies</artifactId>
                <version>1.7.0-2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- 简单的 Spring Cloud Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入 Spring Cloud Tencent 的服务注册发现依赖 -->
        <dependency>
            <groupId>com.tencent.cloud</groupId>
            <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
        </dependency>
        <!-- 引入 Spring Cloud Tencent 的服务熔断依赖 -->
        <dependency>
            <groupId>com.tencent.cloud</groupId>
            <artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
        </dependency>
        <!-- 引入 Spring Cloud 的 LoadBalancer 定义 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>
    ...
</project>

配置 application.yml

在 resources 目录下创建 application.yml 文件,并按照如下进行配置

.
├── java
│   └── com
│       └── example
│           └── springcloudpolarisquickstart
│               └── SpringCloudConsumerApplication.java
└── resources
    └── application.yml

配置 application.yml

server:
  port: 38888
spring:
  application:
    name: CircuitBreakerEchoConsumer
  cloud:
    polaris:
      namespace: default # 设置注册中心命名空间
      address: grpc://127.0.0.1:8091
      discovery:
        enabled: true
      stat:
        enabled: true
        port: 38082
    loadbalancer:
      configurations: polaris
    tencent:
      rpc-enhancement:
        enabled: true
        reporter:
          ignore-internal-server-error: true
          series: server_error
          statuses: gateway_timeout, bad_gateway, service_unavailable

示例代码

@SpringBootApplication
public class SpringCloudConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    static class EchoController {

        private final RestTemplate restTemplate;

        EchoController(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }

        @GetMapping(value = "/echo")
        public String echo(@RequestHeader HttpHeaders headers, @RequestParam(name = "value") String val) {
            HttpEntity<String> entity = new HttpEntity<>(headers);
            ResponseEntity<String> response = restTemplate.exchange("http://CircuitBreakerEchoServer/echo/" + val, HttpMethod.GET, entity, String.class);
            return response.getBody();
        }
    }
}

设置 CircuitBreakerEchoServer 的被调熔断规则

验证

快速发起多次 curl 命令调用,出现过几次错误请求响应之后,所有的请求将全部由端口为20001的进程进行响应

➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
Hello PolarisMesh hello, I'm EchoServer:20001%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
I'm bad, EchoServer:20002%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
Hello PolarisMesh hello, I'm EchoServer:20001%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
I'm bad, EchoServer:20002%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
Hello PolarisMesh hello, I'm EchoServer:20001%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
I'm bad, EchoServer:20002%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
Hello PolarisMesh hello, I'm EchoServer:20001%
➜  ~ curl --location --request GET '127.0.0.1:38888/echo?value=hello'
Hello PolarisMesh hello, I'm EchoServer:20001%