1.依赖
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.降级示例:
请求端:cloud-hystrim-order80
主启动类
@EnableFeignClients()
@SpringBootApplication
@EnableHystrix
public class OrderHystrimMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrimMain80.class,args);
}
}
超时测试:
//Hystrim >test
@GetMapping("/timeOut/{id}")
public String timeOut(@PathVariable("id") Integer id) throws InterruptedException {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String sssss= paymentFeginService.payment_timeOut(id);
log.info(">>>>>>>>>>>>>>>>>>ssssss:"+sssss);
return sssss ;
}
调用远程出:
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVER",fallback = PaymentFeginServiceFallback.class)
public interface PaymentFeginService {
@GetMapping("/payment/selectOne/{id}")
CommonResult selectOne(@PathVariable(name = "id") Integer id);
@GetMapping("/payment/timeOut")
CommonResult timeOut();
@GetMapping("/payment/payment_timeOut/{id}")
String payment_timeOut(@PathVariable("id") Integer id);
@GetMapping("/payment/get/zipkin")
String getZipkin();
}
PaymentFeginServiceFallback:这个类是请求端做的降级处理
@Component
public class PaymentFeginServiceFallback implements PaymentFeginService {
@Override
public CommonResult selectOne(Integer id) {
return new CommonResult(404,"客户端超时了:",null);
}
@Override
public CommonResult timeOut() {
return new CommonResult(404,"客户端超时了:",null); }
@Override
public String payment_timeOut(Integer id) {
return "客户端超时了:" ; }
@Override
public String getZipkin() {
return "测试链路返回";
}
}
降级服务端示例:
@GetMapping("/payment_timeOut/{id}")
public String payment_timeOut(@PathVariable("id") Integer id) {
String result = paymentService.payment_timeOut(id);
log.info("<<<<<<<<"+result);
return result;
}
//超过3s就调用pay_timeOutHandler方法进行降级
@HystrixCommand(fallbackMethod ="pay_timeOutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
public String payment_timeOut(Integer id){
int num=6000;
try {
Thread.sleep(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "线程池:"+Thread.currentThread().getName()+":id:"+id;
}
//服务端降级
public String pay_timeOut(Integer id){
return "线程池:"+Thread.currentThread().getName()+":超时了:"+id;
}
3.0
服务端熔断示例:
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker //主要注解
public class PaymentHistrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHistrixMain8001.class, args);
}
//服务熔断 开启熔断器,请求次数超过10次,10s内,错误率达到60然后进行跳闸熔断,服务恢复正常后,继续调用链调用,不会出现降级后不可用情况(这里小于0抛异常,大于0请求真正常)
@HystrixCommand(fallbackMethod ="paymentC ircuitBreak_fallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value ="10000"),//时间窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")//失败率达到多少后跳闸
})
@GetMapping("/circuit/{id}")
public String paymentCrircuitBreaker(@PathVariable("id")Integer id){
if(id<0){
throw new RuntimeException("******id不能为负数");
}
String sss= UUID.randomUUID().toString();
return Thread.currentThread().getName()+"调用成功:"+sss;
}