Hystrix
Propagating the Security Context or Using Spring Scopes
如果您希望某些線程本地上下文傳播到@HystrixCommand舌狗,則默認聲明不起作用井佑,因為它在線程池中執(zhí)行該命令(如果超時)斤彼。您可以通過配置或直接在注釋中切換Hystrix,以使用與調(diào)用者相同的線程,方法是要求它使用不同的“隔離策略”。
- 示例:
- 演示如何在注釋中設置線程:
@RestController
public class GoodsController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/goods/{id}")
//fallbackMethod定義的方法的參數(shù)名和返回值一定要和原參數(shù)一致
@HystrixCommand(fallbackMethod = "findByIdFallback")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
}
public User findByIdFallback(Long id){
User user = new User();
user.setId(0L);
user.setUsername("zhang三");
return user;
}
}
一般首先不配置commandProperties ,如果遇到運行時異常钝计,表示無法找到作用域上下文酿炸,則需要使用相同的線程瘫絮,才需要配置。
因為請求是一個線程填硕,@HystrixCommand是一個隔離的線程麦萤;
如果您使用@SessionScope或@RequestScope,也能達到同樣的效果扁眯。
- 使用commandProperties
@RestController
public class GoodsController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/goods/{id}")
//fallbackMethod定義的方法的參數(shù)名和返回值一定要和原參數(shù)一致
@HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = { @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")} )
//commandProperties = @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
//一般首先不做配置壮莹,如果遇到運行時異常,表示無法找到作用域上下文姻檀,則需要使用相同的線程垛孔,才需要配置。
//因為請求是一個線程施敢,@HystrixCommand是一個隔離的線程,由于不在同一個線程周荐,容易導致找不到上下文
//如果您使用@SessionScope或@RequestScope,也能達到同樣的效果僵娃。
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
}
public User findByIdFallback(Long id){
User user = new User();
user.setId(0L);
user.setUsername("zhang三");
return user;
}
- 使用@SessionScope或@RequestScope
@RestController
@SessionScope
@Scope("session")
public class GoodsController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/goods/{id}")
@HystrixCommand(fallbackMethod = "findByIdFallback" )
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
}
public User findByIdFallback(Long id){
User user = new User();
user.setId(0L);
user.setUsername("zhang三");
return user;
}
}
==注:== 了解scope的分類:Spring Scope
監(jiān)控Hystrix界面:Hystrix dashboard 和 Turbine
鏈接:https://blog.csdn.net/hry2015/article/details/78617954
Hystrix Metrics Stream
要啟用Hystrix度量標準流概作,請在==spring-boot-starter-actuator==上包含依賴項,并設置==management.endpoints.web.exposure.include:hystrix.stream==默怨。 這樣做會將 ==/actuator/hystrix.stream==公開為管理端點讯榕,如以下示例所示:
- pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- application.yml
# 配置Hystrix Metrics Stream
management:
endpoints:
web:
exposure:
include: hystrix.stream
Hystrix Dashboard
- pom.xml添加依賴
<!-- hystrix dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
- 啟動類添加Hystrix Dashboard注解
@EnableHystrixDashboard
Hystrix Turbine
- 概念
- 一個準實時的集群界面監(jiān)控工具
- 有一定的延遲
- 因為取服務需要一定的時間
- 如何使用
- pom.xml添加依賴
<!-- hystrix turbine --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
- 啟動類添加Hystrix Turbine注解
@EnableTurbine
- application.xml
# 配置turbine turbine: aggregator: clusterConfig: MICROSERVICE-CONSUMER-GOODS-RIBBON-WITH-HYSTRIX appConfig: microservice-consumer-goods-ribbon-with-hystrix