From d87a0f774a47171af68cad6da638a6521ef02541 Mon Sep 17 00:00:00 2001 From: lepdou Date: Sat, 2 Apr 2022 11:41:30 +0800 Subject: [PATCH] optimize example --- .../README-zh.md | 64 ++++++++---------- .../polaris-circuitbreaker-example/README.md | 2 +- .../example/ServiceBController.java | 3 +- .../polaris-circuitbreaker-example-b2/pom.xml | 51 ++++++++++++++ .../ciruitbreaker/example/ServiceB2.java | 36 ++++++++++ .../example/ServiceBController.java | 52 ++++++++++++++ .../src/main/resources/bootstrap.yml | 10 +++ .../polaris-circuitbreaker-example/pom.xml | 3 +- .../polaris/config/example => }/README-zh.md | 3 +- .../config/example => }/polaris-config-ui.png | Bin .../polaris-discovery-example/README-zh.md | 32 +++------ .../src/main/resources/rule.json | 19 ------ 12 files changed, 194 insertions(+), 81 deletions(-) create mode 100644 spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceB2.java create mode 100644 spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceBController.java create mode 100644 spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml rename spring-cloud-tencent-examples/polaris-config-example/{src/main/java/com/tencent/cloud/polaris/config/example => }/README-zh.md (91%) rename spring-cloud-tencent-examples/polaris-config-example/{src/main/java/com/tencent/cloud/polaris/config/example => }/polaris-config-ui.png (100%) delete mode 100644 spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/rule.json diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README-zh.md b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README-zh.md index 9f340eb66..23fe50ad6 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README-zh.md +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README-zh.md @@ -4,21 +4,24 @@ 本样例将介绍如何在Spring Cloud项目中使用```spring-cloud-starter-tencent-polaris-circuitbreaker```以使用其各项功能。 -该样例分为两个微服务,即polaris-circuitbreaker-example-a和polaris-circuitbreaker-example-b。其中,polaris-circuitbreaker-example-a对polaris-circuitbreaker-example-b发生调用。 +该样例分为两个微服务,即 + +1. ```polaris-circuitbreaker-example-a``` +2. ```polaris-circuitbreaker-example-b``` 有两个实例 B(默认正常服务)和 B2(模拟异常服务) + +``` polaris-circuitbreaker-example-a``` 对 ```polaris-circuitbreaker-example-b```发生调用。 ## 使用说明 ### 修改配置 -配置如下所示。其中,${ip}和${port}为Polaris后端服务的IP地址与端口号。 +修改 resource/bootstrap.yml 中北极星的服务端地址 ```yaml spring: - application: - name: ${application.name} cloud: polaris: - address: ${ip}:${port} + address: grpc://${ip}:8091 ``` ### 启动样例 @@ -27,40 +30,20 @@ spring: 参考[Polaris Getting Started](https://github.com/PolarisMesh/polaris#getting-started)。 -#### 启动应用 +### 启动应用 -注意,由于需要验证熔断功能,因此需要部署两个及以上的被调服务(样例中部署两个即可)。 - IDEA启动 -分别启动```spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a```下的```ServiceA```和```spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b```下的```ServiceB```。 - -注意,ServiceB需要启动两个。同机器上可以修改端口号来实现。 - -两个ServiceB的com.tencent.cloud.polaris.circuitbreaker.example.ServiceBController.info的逻辑需不同,即一个正常返回一个抛出异常。 - -- Maven打包启动 - -在```spring-cloud-tencent-examples/polaris-discovery-example```下执行 - -注意,ServiceB需要启动两个。同机器上可以修改端口号来实现。 - -两个ServiceB的com.tencent.cloud.polaris.circuitbreaker.example.ServiceBController.info的逻辑需不同,即一个正常返回一个抛出异常。 - -```sh -mvn clean package -``` +分别启动 -然后在```polaris-circuitbreaker-example-a```和```polaris-circuitbreaker-example-b```下找到生成的jar包,运行 +1. ```spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a```下的```ServiceA``` +2. ```spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b```下的```ServiceB``` +3. ```spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2```下的```ServiceB2``` -``` -java -jar ${app.jar} -``` -启动应用,其中${app.jar}替换为对应的jar包名。 +## 验证 -### 验证 - -#### Feign调用 +### Feign调用 执行以下命令发起Feign调用,其逻辑为```ServiceB```抛出一个异常 @@ -72,7 +55,18 @@ curl -L -X GET 'localhost:48080/example/service/a/getBServiceInfo' 在出现 ``` -trigger the refuse for service b -``` -时,表示请求到有异常的ServiceB,需要熔断这个实例。后面的所有请求都会得到正常返回。 +hello world ! I'm a service B1 +``` + +时,表示 B2 已经被熔断了,请求只会打到 B1。 + +### 验证更多场景 + +您也可以调整 ```example-b``` 和 ```example-b2``` 中 ```resource/bootstrap.yml``` is-throw-runtime-exception +参数调整服务是否抛出异常。 + +例如测试以下场景: +1. 两个实例都是正常的,这时候预期是 B1 和 B2 都能正常被调用到 +2. 一个实例正常一个实例不正常,这时候预期是不正常实例被熔断,请求只会打到正常的实例 +3. 两个实例都不正常,这时候 Feign 会自动 Fallback 到 ProviderBFallback.java 的实现类 diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README.md b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README.md index cf30a8c7d..d298346d1 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README.md +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/README.md @@ -75,4 +75,4 @@ when appear trigger the refuse for service b ``` -it means the request signals abnormal ServiceB, and will ciruitbreak this instance, the later requests will return normally. \ No newline at end of file +it means the request signals abnormal ServiceB, and will ciruitbreak this instance, the later requests will return normally. diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/java/com/tencent/cloud/polaris/circuitbreaker/example/ServiceBController.java b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/java/com/tencent/cloud/polaris/circuitbreaker/example/ServiceBController.java index edcef56e7..67540c2c9 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/java/com/tencent/cloud/polaris/circuitbreaker/example/ServiceBController.java +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/java/com/tencent/cloud/polaris/circuitbreaker/example/ServiceBController.java @@ -13,6 +13,7 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. + * */ package com.tencent.cloud.polaris.circuitbreaker.example; @@ -44,7 +45,7 @@ public class ServiceBController { throw new RuntimeException("failed for call my service"); } else { - return "hello world ! I'm a service B"; + return "hello world ! I'm a service B1"; } } diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/pom.xml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/pom.xml new file mode 100644 index 000000000..ba28b0cdd --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/pom.xml @@ -0,0 +1,51 @@ + + + + polaris-circuitbreaker-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + polaris-circuitbreaker-example-b2 + + + + + org.springframework.boot + spring-boot-starter-webflux + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceB2.java b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceB2.java new file mode 100644 index 000000000..4aacc0d22 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceB2.java @@ -0,0 +1,36 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.ciruitbreaker.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Circuit breaker example callee application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +public class ServiceB2 { + + public static void main(String[] args) { + SpringApplication.run(ServiceB2.class); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceBController.java b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceBController.java new file mode 100644 index 000000000..7e4bb9b4c --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/java/com/tencent/cloud/polaris/ciruitbreaker/example/ServiceBController.java @@ -0,0 +1,52 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.ciruitbreaker.example; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Service B Controller. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/example/service/b") +public class ServiceBController { + + @Value("${is-throw-runtime-exception:#{false}}") + private boolean isThrowRuntimeException; + + /** + * Get service information. + * @return service information + */ + @GetMapping("/info") + public String info() { + if (isThrowRuntimeException) { + throw new RuntimeException("failed for call my service"); + } + else { + return "hello world ! I'm a service B2"; + } + } + +} diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..fcea98bb3 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml @@ -0,0 +1,10 @@ +server: + port: 48082 +spring: + application: + name: polaris-circuitbreaker-example-b + cloud: + polaris: + address: grpc://9.134.122.18:8091 + namespace: default +is-throw-runtime-exception: true diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml index 115e40e5f..5876625dc 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml @@ -17,6 +17,7 @@ polaris-circuitbreaker-example-a polaris-circuitbreaker-example-b + polaris-circuitbreaker-example-b2 @@ -26,4 +27,4 @@ - \ No newline at end of file + diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/README-zh.md b/spring-cloud-tencent-examples/polaris-config-example/README-zh.md similarity index 91% rename from spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/README-zh.md rename to spring-cloud-tencent-examples/polaris-config-example/README-zh.md index 3b1048a61..f4c12aec3 100644 --- a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/README-zh.md +++ b/spring-cloud-tencent-examples/polaris-config-example/README-zh.md @@ -2,6 +2,7 @@ ## 1. bootstrap.yml 配置 +修改 resources/bootstrap.yml ```spring.cloud.polaris.config.address``` 北极星服务端地址。 > 注意是在 bootstrap.yml 里配置,而不是在 application.yml 里配置。因为配置中心相关的配置是在 bootstrap 阶段依赖的配置。 ```` yaml @@ -12,7 +13,7 @@ spring: polaris: namespace: dev config: - addresses: grpc://9.134.122.18:8093 # the address of polaris config server + address: grpc://9.134.122.18:8093 # the address of polaris config server auto-refresh: true # auto refresh when config file changed groups: - name: ${spring.application.name} # group name diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/polaris-config-ui.png b/spring-cloud-tencent-examples/polaris-config-example/polaris-config-ui.png similarity index 100% rename from spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/polaris-config-ui.png rename to spring-cloud-tencent-examples/polaris-config-example/polaris-config-ui.png diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/README-zh.md b/spring-cloud-tencent-examples/polaris-discovery-example/README-zh.md index e58d13464..851860f06 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/README-zh.md +++ b/spring-cloud-tencent-examples/polaris-discovery-example/README-zh.md @@ -4,21 +4,20 @@ 本样例将介绍如何在Spring Cloud项目中使用```spring-cloud-starter-tencent-polaris-discovery```以使用其各项功能。 -该样例分为两个微服务,即discovery-caller-service和discovery-callee-service。其中,discovery-caller-service对discovery-callee-service发生调用。 +该样例分为两个微服务,即 ```discovery-caller-service``` 和 ```discovery-callee-service ```。 +其中 ```discovery-caller-service``` 调用 ```discovery-callee-service``` ## 使用说明 ### 修改配置 -配置如下所示。其中,${ip}和${port}为Polaris后端服务的IP地址与端口号。 +修改 resource/bootstrap.yml 中北极星的服务端地址 ```yaml spring: - application: - name: ${application.name} cloud: polaris: - address: ${ip}:${port} + address: grpc://${ip}:8091 ``` ### 启动样例 @@ -31,29 +30,16 @@ spring: - IDEA启动 -分别启动```spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service```下的```DiscoveryCallerService```和```spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service```下的```DiscoveryCalleeService```。 +分别启动 -- Maven打包启动 - -在```spring-cloud-tencent-examples/polaris-discovery-example```下执行 - -```sh -mvn clean package -``` - -然后在```discovery-caller-service```和```discovery-callee-service```下找到生成的jar包,运行 - -``` -java -jar ${app.jar} -``` - -启动应用,其中${app.jar}替换为对应的jar包名。 +1. ```spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service```下的```DiscoveryCallerService``` +2. ```spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service```下的```DiscoveryCalleeService``` ### 验证 -#### Feign调用 +#### 调用 discovery-caller-service 暴露的接口 -执行以下命令发起Feign调用,其逻辑为```DiscoveryCalleeService```返回value1+value2的和 +执行以下命令发起Feign调用,其逻辑为```DiscoveryCalleeService```返回 value1+value2 的和 ```shell curl -L -X GET 'http://localhost:48080/discovery/service/caller/feign?value1=1&value2=2' diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/rule.json b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/rule.json deleted file mode 100644 index f047dc2bb..000000000 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/rule.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "service": "RateLimitCalleeService", - "namespace": "Production", - "priority": 0, - "resource": "QPS", - "type": "LOCAL", - "labels": { - "method": { - "value": "/business/invoke" - } - }, - "amounts": [ - { - "maxAmount": 10, - "validDuration": "1s" - } - ], - "action": "REJECT" -}