单独使用hytrix
依赖
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.12</version>
</dependency>
配置文件
@Configuration
public class HystrixConfig {
//用来拦截处理HystrixCommand注解
@Bean
public HystrixCommandAspect hystrixAspect() {
return new HystrixCommandAspect();
}
//用来像监控中心Dashboard发送stream信息
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/hystrix.stream");
return registration;
}
}
使用
只要在需要使用的方法上加上@HystrixCommand注解,并加上配置就可以了。 fallbackMethod = “getOrderPageListFallback”,这边引号里的就是执行降解会执行的方法。
@Controller
public class GateWayService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@HystrixCommand(
fallbackMethod = "getOrderPageListFallback",
threadPoolProperties = { //10个核心线程池,超过20个的队列外的请求被拒绝; 当一切都是正常的时候,线程池一般仅会有1到2个线程激活来提供服务
@HystrixProperty(name = "coreSize", value = "5"),
@HystrixProperty(name = "maxQueueSize", value = "5"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "10")},
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"), //命令执行超时时间
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"), //若干10s一个窗口内失败三次, 则达到触发熔断的最少请求量
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000") //断路30s后尝试执行, 默认为5s
})
@RestController("/test")
public Object getRemote() {
// return restTemplate.getForEntity("http://localhost:8080/adp/exportExcel",Object.class);
return null;
}
public String getOrderPageListFallback() {
logger.error("===================== 执行降级策略");
return "调用失败" ;
}
集成feign后使用hytrix
使用feign的话需要使用eureka来作为注册中心
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
需要在properties中指定 注册进入eureka的名字 用于其他服务调用本服务
spring.application.name=test
@FeignClient注解在接口上 interface,fallback指定熔断执行方法。
@FeignClient(name="dataresource-adp4",fallback = DataResourceServiceImpl.class) //name中的名字就是在eureka中注册的名字
public interface DataResourceService {
@RequestMapping("/dr/dataSources")
AdpResponse getDataSources();
@RequestMapping("/dr/tables/bySourceId")
AdpResponse bySourceId(@RequestParam(value = "sourceId") String sourceId);//有参数的需要用@RequestParam来指定参数名称
}
fallback方法
@Component
public class DataResourceServiceImpl implements DataResourceService {
@Override
public AdpResponse getDataSources() {
AdpResponse adpResponse = new AdpResponse();
adpResponse.setDesc("服务器似乎已经爆炸,请稍后重试");
adpResponse.setCode(500);
return adpResponse;
}
@Override
public AdpResponse bySourceId(String id) {
AdpResponse adpResponse = new AdpResponse();
adpResponse.setDesc("服务器似乎已经爆炸,请稍后重试");
adpResponse.setCode(500);
return adpResponse;
}
}
@Autowired
DataResourceService dataResourceService;//这就是上面的接口,和普通的service调用方式一样
@RequestMapping("/dr/tables/bySourceId")
public AdpResponse getTableBySourceId(HttpServletRequest request){
String id = request.getParameter("id");
AdpResponse notice = dataResourceService.bySourceId(id); //调用feign的接口
return notice;
}
可能不全,后面还会继续补充,有疑惑欢迎提问,内容有什么问题欢迎指出。。。。