Hystrix-HystrixCommand注解方式实现信号量隔离
目录
这里使用的是接口方式,我们来实现信号隔离模式
代码:
/** * @ClassName: HystrixCommandSemaphoreController * @Desc: 信号量隔离, 支持 多个service 共用同一个 信号配置 * @author: lance.lan * @date: 2020年11月15日 下午10:07:15 * */ @RestController @RequestMapping(value = "/hystrix/semaphore") public class HystrixCommandSemaphoreController {
@Autowired private UserService userService;
/** * @Title: getUser * @author: lance.lan * @Description:无参 * @param httpServletResponse * @throws InterruptedException * @return Object */ @RequestMapping(value = "/getUser", method = RequestMethod.GET) public Object getUser(HttpServletResponse httpServletResponse) throws InterruptedException { Thread.sleep(1000L); return new HystrixSemaphoreCommand(userService,"getUserName", null, null).execute(); }
/** * @Title: getUser2 * @author: lance.lan * @Description: 有参数 * @param httpServletResponse * @throws InterruptedException * @return Object */ @RequestMapping(value = "/getUser2", method = RequestMethod.GET) public Object getUser2(HttpServletResponse httpServletResponse) throws InterruptedException { Thread.sleep(1000L); Class<?>[] paramVarArgs = {String.class}; String[] paramValues = {"lance"}; return new HystrixSemaphoreCommand(userService,"getUserName", paramVarArgs, paramValues).execute(); } } |
public class HystrixSemaphoreCommand extends HystrixCommand<Object> { private UserService userService; private String methodName; private Class<?>[] paramVarArgs; private Object[] paramValue;
/** * @param userService * @param methodName:要调用的方法名称 * @param paramVarArgs:方法参数类型 * @param paramValue:参数值 */ public HystrixSemaphoreCommand(UserService userService, String methodName, Class<?>[] paramVarArgs, Object[] paramValue) { super(setter()); this.userService = userService; this.methodName = methodName; this.paramVarArgs = paramVarArgs; this.paramValue = paramValue; }
private static Setter setter() { // 服务分组 HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("user"); // 命令属性配置 采用信号量模式 HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE) // 使用一个原子计数器(或信号量)来记录当前有多少个线程在运行,当请求进来时先判断计数 // 器的数值,若超过设置的最大线程个数则拒绝该请求,若不超过则通行,这时候计数器+1,请求返 回成功后计数器-1。 .withExecutionIsolationSemaphoreMaxConcurrentRequests(2); return HystrixCommand.Setter.withGroupKey(groupKey).andCommandPropertiesDefaults(commandProperties); }
@Override protected Object run() throws Exception { Thread.sleep(1000); //反射 Object obj = UserService.class.getMethod(methodName, paramVarArgs).invoke(userService, paramValue); //String name = userService.getUserName(); System.out.println("当前线程名称:" + Thread.currentThread().getName()); return obj; }
@Override protected String getFallback() { // 如果Hystrix发生熔断,当前服务不可用,直接执行Fallback方法 System.out.println("系统错误!"); return "系统错误!"; } } |
@Service public class UserService { public String getUserName() { return "lance.lan"; }
public String getUserName(String name) { return "return:" + name; } } |
测试:
|
|
当连续访问:http://localhost:8764/hystrix/semaphore/getUser2, 也会产生熔断
|