1、服务提供者端故意制造异常
package com.laoxu.springcloud.service;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
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;
@Service
@DefaultProperties(defaultFallback = "payment_Globle_FallbackMethod")
public class PaymentService {
//正常访问返回ok的方法
@HystrixCommand
public String paymentInfo_OK(Integer id){
int age = 10/0;
return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK, id:" + id + "\t" + "哈哈";
}
@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
})
public String paymentInfo_Timeout(Integer id){
long second = 3;
try {
TimeUnit.SECONDS.sleep(second);
}catch (InterruptedException e){
e.printStackTrace();
}
return "线程池:" + Thread.currentThread().getName() + "paymentInfo_Timeout, id:" + id + "\t" + "哈哈" + "耗时" +second+"秒钟";
}
public String paymentInfo_TimeoutHandler(Integer id){
return "线程池:" + Thread.currentThread().getName() + "paymentInfo_Timeout, id:" + id + "\t" + "系统繁忙!请稍后再试!!";
}
//全局fallback方法
public String payment_Globle_FallbackMethod(){
return "系统繁忙!请稍后再试!!";
}
}
2、在消费者端新建服务降级类继承服务
package com.laoxu.springcloud.service;
import org.springframework.stereotype.Component;
@Component
public class PaymentFallBackService implements PaymentHystrixService {
public String paymentInfo_OK(Integer id) {
return "PaymentFallBackService fall back-- paymentInfo_OK";
}
public String paymentInfo_Timeout(Integer id) {
return "PaymentFallBackService fall back-- paymentInfo_Timeout";
}
}
3、在消费者端 feign调用端指定服务降级类进行通配降级
package com.laoxu.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallBackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Timeout(@PathVariable("id") Integer id);
}
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallBackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Timeout(@PathVariable("id") Integer id);
}
4、配置开启服务降级
server:
port: 80
spring:
application:
name: cloud-consumer-feign-hystrix-order
eureka:
client:
# 将自己注册进eureka中心
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
feign:
hystrix:
enabled: true
server:
port: 80
spring:
application:
name: cloud-consumer-feign-hystrix-order
eureka:
client:
# 将自己注册进eureka中心
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
feign:
hystrix:
enabled: true
5、访问:http://localhost/consumer/payment/hystrix/ok/31
调用通配的异常方法