optimize example

pull/86/head
lepdou 3 years ago
parent 8c9da70750
commit d87a0f774a

@ -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 的实现类

@ -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.
it means the request signals abnormal ServiceB, and will ciruitbreak this instance, the later requests will return normally.

@ -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";
}
}

@ -0,0 +1,51 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>polaris-circuitbreaker-example</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>polaris-circuitbreaker-example-b2</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

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

@ -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";
}
}
}

@ -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

@ -17,6 +17,7 @@
<modules>
<module>polaris-circuitbreaker-example-a</module>
<module>polaris-circuitbreaker-example-b</module>
<module>polaris-circuitbreaker-example-b2</module>
</modules>
<dependencies>
@ -26,4 +27,4 @@
</dependency>
</dependencies>
</project>
</project>

@ -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

@ -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'

@ -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"
}
Loading…
Cancel
Save