Skip to content

Commit

Permalink
添加 part5 circuit breaker的2篇参考文章
Browse files Browse the repository at this point in the history
  • Loading branch information
ehutong committed Sep 25, 2020
1 parent 005f77d commit 8f80266
Show file tree
Hide file tree
Showing 29 changed files with 1,588 additions and 75 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README02_Config_Vault.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,4 @@ vault kv put secret/catalog-service @${CONFIG_DIR}/catalog-service.json
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README03_Registry_Discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,4 @@ java -jar -Dserver.port=8282 inventory-service/target/inventory-service.jar &
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
85 changes: 85 additions & 0 deletions README04_Circuit_Breaker.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
# Part4 Circuit Breaker using Netflix Hystrix

<!--https://www.sivalabs.in/2018/03/spring-cloud-netflix-circuit-breaker/-->

在微服务中,Netfix 创建了 Hystrix库,来实现 熔断机制,我们可以使用 Spring Cloud Netflix Hystrix Circuit Breaker来保护广播式的大流量导入造成的系统失败。

> In the microservices world, to fulfill a client request one microservice
> may need to talk to other microservices. We should minimize this kind of
> direct dependencies on other microservices but in some cases it is unavoidable.
> If a microservice is down or not functioning properly then the issue may
> cascade up to the upstream services. Netflix created Hystrix library
> implementing Circuit Breaker pattern to address these kinds of issues.
> We can use Spring Cloud Netflix Hystrix Circuit Breaker to protect
> microservices from cascading failures.
In this post we are going to learn:
* Implementing Circuit Breaker pattern using @HystrixCommand
* How to propagate ThreadLocal variables
* Monitoring Circuit Breakers using Hystrix Dashboard


## 1 Implementing Netflix Hystrix Circuit Breaker pattern

> From catalog-service we are invoking REST endpoint on inventory-service
> to get the inventory level of a product. What if inventory-service is
> down? What if inventory-service is taking too long to respond thereby
> slowing down all the services depending on it? We would like to have
> some timeouts and implement some fallback mechanism.

### 1.1 修改 catalog-service的 pom.xml文件
```xml
<dependency>
Expand Down Expand Up @@ -43,6 +67,12 @@ public class CatalogServiceApplication {
```
### 1.3 可以使用 @HystrixCommand 注解,应用在超时和回调方法中

> Now we can use @HystrixCommand annotation on any method we want to apply timeout and fallback method.
> Let us create InventoryServiceClient.java which will invoke inventory-service
> REST endpoint and apply @HystrixCommand with a fallback implementation.


如下 创建 InventoryServiceClient.java 文件
```java
package com.thefirstwind.catalogservice.services;
Expand Down Expand Up @@ -102,7 +132,57 @@ public class InventoryServiceClient {
}

```

> We have annotated the method from where we are making a REST call with
> @HystrixCommand(fallbackMethod = “getDefaultProductInventoryByCode”)
> so that if it doesn’t receive the response within the certain time limit
> the call gets timed out and invoke the configured fallback method.
> The fallback method should be defined in the same class and should have
> the same signature. In the fallback method getDefaultProductInventoryByCode()
> we are setting the availableQuantity to 50, obviously, this behavior
> depends on what business wants.
> We can customize the @HystrixCommand default behavior by configuring
> properties using @HystrixProperty annotations.
如下所示
```java
@HystrixCommand(fallbackMethod = "getDefaultProductInventoryByCode",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value="60")
}
)
public Optional<ProductInventoryResponse> getProductInventoryByCode(String productCode)
{
....
}

```

同时,也可以设置到 bootstrap.properties/yml 文件中, 如下所示
```properties
hystrix.command.getProductInventoryByCode.execution.isolation.thread.timeoutInMilliseconds=2000
hystrix.command.getProductInventoryByCode.circuitBreaker.errorThresholdPercentage=60
```

另外还可以通过指定 commandKey 实现默认的行为,如下所示
```java
@HystrixCommand(commandKey = "inventory-by-productcode", fallbackMethod = "getDefaultProductInventoryByCode")
public Optional<ProductInventoryResponse> getProductInventoryByCode(String productCode)
{
...
}

```

## 2 How to propagate ThreadLocal variables
> By default, the methods with @HystrixCommand will be executed on a different thread because the default execution.isolation.strategy is ExecutionIsolationStrategy.THREAD. So, the ThreadLocal variables we set before invoking @HystrixCommand methods won’t be available within @HystrixCommand methods.
> One option to make the ThreadLocal variables available is using execution.isolation.strategy=SEMAPHORE.


## 3 Monitoring Circuit Breakers using Hystrix Dashboard
### 3.1 创建项目 hystrix-dashboard项目
### 3.2 在pom.xml中添加以下依赖
Expand All @@ -117,6 +197,10 @@ public class InventoryServiceClient {
可以通过 http://localhost:8181/actuator/hystrix.stream 来监控


## 为了加深对hystrix的理解可以参考以下2篇文章
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)

## Related Content
* [Part1 overview](README.md)
* [Part2 Spring Cloud Config and Vault](README02_Config_Vault.md)
Expand All @@ -131,3 +215,4 @@ public class InventoryServiceClient {
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README05_API_Gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README06_Distributed_Tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README11_Spring_Cloud_Config_Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,4 @@ curl -H "Content-Type: application/json" -d {} http://localhost:8181/actuator/re
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README11_Spring_Cloud_Config_Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ http://localhost:8888/catalogservice/prod then you will get the following respon
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README12_Spring_Cloud_Bus.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ http://47.103.128.159:8181/properties
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
1 change: 1 addition & 0 deletions README13_Spring_Cloud_Eureka.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,4 @@ mvn spring-boot:run
* [Spring Microservices Docker Example](https://github.com/thefirstwind/spring-microservices-docker-example/blob/master/README.md)
* [Spring Cloud Eureka and Feign](README13_Spring_Cloud_Eureka.md)
* [Netflix Hystrix How It Works](README14_Netflix_Hystrix_How_it_works.md)
* [Netflix Hystrix How to Use](README15_Netflix_Hystrix_How_To_Use.md)
Loading

0 comments on commit 8f80266

Please sign in to comment.