spring cloud Feign 通过注解配置线程池等 Hystrix 属性

package com.sande.order.accountApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sande.order.domain.OrderPayDTO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.ClientProperties;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.sande.order.domain.OrderDTO;
import com.sande.order.domain.User;
import com.sande.order.userApi.UserApiFeignHystrix;

@FeignClient(value = "account-server", fallback = AccountApiFeignHystrix.class)
public interface AccountApi  {
	/**
	 * 该 HystrixCommand 注解为 pay 方法设置组KEY/命令KEY/线程池KEY/threadPoolProperties 
	 * 配置线程池的线程数等参数。groupKey 不配置默认是注解所在类的类名。commandKey 不配置默认是
	 * 带注释的方法名
	 * 
	 * **/
	@HystrixCommand(groupKey="accountPayGroup",commandKey="accountPay",threadPoolKey="account",threadPoolProperties= {
			@HystrixProperty(name="coreSize",value="20"),
			@HystrixProperty(name="maxQueueSize",value="50")
	},commandProperties={
			@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="3000"),
			@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="40")
	})
	
	
	//@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="3000")
	
	
	
	@PostMapping(value="/account/pay")
	OrderPayDTO pay(@RequestBody OrderDTO orderDTO);
	
	@HystrixCommand(commandProperties={
			@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="3000"),
			@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="40")
	})
	
	@PostMapping(value="/account/orderRefund")
	OrderPayDTO  orderRefund(@RequestBody OrderDTO orderDTO); 
	
	@PostMapping(value="/account/test")
	void test();
	
	@PostMapping(value="/account/updateOrderPay")
	OrderDTO  updateOrderPay(@RequestBody OrderDTO orderDTO);
}

经过测试发现在使用了 @FeignClient  注解的接口中,不能使用 @HystrixCommand 注解。使用了 @HystrixCommand 注解会报错:

spring cloud Feign 通过注解配置线程池等 Hystrix 属性_第1张图片

 

在 application.yml 配置文件中配置线程池大小

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
  threadpool:
      default:
         coreSize: 20
      AccountApi:
         coreSize: 10

如果没有使用 @HystrixCommand 注解显示配置线程池名称,默认会使用 groupKey 的值作为线程池的KEY,如果 groupKey 没有设置就会使用类名作为 groupKey 的值,所以对于  public interface AccountApi  现场池的 KEY 就是  AccountApi  。

 

 

你可能感兴趣的:(spring,cloud)