1. 程式人生 > >Hystrix使用詳解

Hystrix使用詳解

Hystrix

Propagating the Security Context or Using Spring Scopes

如果您希望某些執行緒本地上下文傳播到@HystrixCommand,則預設宣告不起作用,因為它線上程池中執行該命令(如果超時)。您可以通過配置或直接在註釋中切換Hystrix,以使用與呼叫者相同的執行緒,方法是要求它使用不同的“隔離策略”。

  • 示例:
    • 演示如何在註釋中設定執行緒:
    @RestController
    public class GoodsController {
    	@Autowired
    	private RestTemplate restTemplate;
    	
    	@GetMapping("/goods/{id}")
    	//fallbackMethod定義的方法的引數名和返回值一定要和原引數一致
    	@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定義的方法的引數名和返回值一定要和原引數一致
	@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;
	}
	
}

監控Hystrix介面:Hystrix dashboard 和 Turbine

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

  • 概念
    • 一個準實時的叢集介面監控工具
    • 有一定的延遲
    • 因為取服務需要一定的時間
  • 如何使用
    • 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