1.降级配置
@HystrixCommand
8001先从自身找问题,设置自身调用超时时间的峰值,峰值内可以正常运行, 超过了需要有兜底的方法处理,做服务降级fallback
2.8001fallback
业务类启用
package com.itxiongmao.service.impl;
import com.itxiongmao.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.service.impl
* @CreateTime: 2020-11-22 12:39
* @Description: TODO
*/
public class PaymentServiceImpl implements PaymentService {
/*
* 正常访问
* @param id
* @return
*/
public String getPaymentInfo_OK(Integer id) {
return "线程池:" + Thread.currentThread().getName() + " paymentInfo_OK,id:" + id + "\t" + "O(∩_∩)O哈哈~";
}
/**
* 超时访问
*
* @param id
* @return
*/
//@HystrixCommand:启用hystrix
(fallbackMethod = "getPaymentInfo_TimeOutHandler",commandProperties = {
(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String paymentInfo_TimeOut(Integer id) {
int timeNumber = 5;
try {
// 暂停3秒钟
TimeUnit.SECONDS.sleep(timeNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:" + id + "\t" +
"O(∩_∩)O哈哈~ 耗时(秒)" + timeNumber;
}
public String getPaymentInfo_TimeOutHandler(Integer id) {
return Thread.currentThread().getName()+"-----------getPaymentInfo_ErrorHandler----TimeOut----"+id;
}
}
@HystrixCommand报异常后如何处理
主启动类激活
@EnableCircuitBreaker
package com.itxiongmao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao
* @CreateTime: 2020-11-22 12:37
* @Description: TODO
*/
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}
3.80fallback
80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端端降级保护
我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务
POM:
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
YML:
server:
port: 80
spring:
application:
name: cloud-comsumer-feign-hystrix-order80
eureka:
client:
service-url:
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
feign:
hystrix:
enabled: true
主启动
package com.itxiongmao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao
* @CreateTime: 2020-11-22 13:01
* @Description: TODO
*/
//激活Hystrix
//@EnableCircuitBreaker
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class, args);
}
}
业务类
"info/timeout/{id}")(
(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
})
public String paymentInfo_timeout( ("id") Integer id){
//int age = 10/0;
String res = paymentHystrixService.paymentInfo_OK(id);
System.out.println(res);
return res;
};
public String paymentTimeOutFallbackMethod( ("id") Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒种后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
4.目前问题
每个业务方法对应一个兜底的方法,代码膨胀,统一和自定义的分开
解决办法
4.1.每个方法配置一个???膨胀
feign接口系列,
@DefaultProperties(defaultFallback="")
4.2.和业务逻辑混在一起???混乱
服务降级,客户端去调用服务端,碰上服务端宕机或关闭.
本次案例服务降级处理是在客户端80实现完成,与服务端8001没有关系 只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦
未来我们要面对的异常
运行
超时
宕机
再看我们的业务类PaymentController,
修改cloud-consumer-feign-hystrix-order80
根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现接口,统一为接口里面的方法进行异常处理
PaymentFallbackService类实现PaymentFeginService接口
YML
PaymentFeignClientService接口
4.2.测试
单个eureka先启动7001
PaymentHystrixMain8001启动
正常访问测试:http://localhost/order/payment/hystrix/ok/32
故意关闭微服务8001
客户端自己调用提示
此时服务端provider已经downl ,但是我们做了服务降级处理, 让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器