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 注解会报错:

 

在 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  。

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值