服务降级-Hystrix

<!-- hystrix -->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
Eureka正常启动
服务端注册进Eureka

服务器端

1.单个方法()一对一)降级

controller
@RestController
@RequestMapping("/payment/hystrix")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/selectPaymentById/{id}")
    public String selectPaymentById(@PathVariable String id){
//        http://localhost:8001/payment/hystrix/selectPaymentById/1
         return paymentService.selectPaymentById(id);
    }

    @GetMapping("/timeOut/{id}")
    public String timeOut(@PathVariable String id){
        return paymentService.timeOut(id);
    }

}
service
public interface PaymentService {

    String selectPaymentById(String id);

    String timeOut(String id);
}

@Service
public class PaymentServiceImpl implements PaymentService {

    @Override
    @HystrixCommand(fallbackMethod = "test_fallback")
    public String selectPaymentById(String id) {
        return "server 8001 : selectPaymentById"+ Thread.currentThread().getName() + "ok~";
    }

    @Override
    @HystrixCommand(fallbackMethod = "test_fallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    public String timeOut(String id) {
        try {
            long s = 3;
            TimeUnit.SECONDS.sleep(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "server 8001 : timeOut"+ Thread.currentThread().getName() + "ok~";
    }

    //服务降级方法
    public String test_fallback(String id){
        return "test_fallback 请重试!" + id;
    }

}

2.全局(独立于共享)降级

controller
@RestController
@RequestMapping("/payment/hystrix")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService_1 paymentService_1;

    @GetMapping("/selectPaymentById1/{id}")
    public String selectPaymentById_1(@PathVariable String id){
        return paymentService_1.selectPaymentById(id);
    }

    @GetMapping("/timeOut1/{id}")
    public String timeOut_1(@PathVariable String id){
        return paymentService_1.timeOut(id);
    }
}

service
public interface PaymentService_1 {

    String selectPaymentById(String id);


    String timeOut(String id);

}
@Service
@DefaultProperties(defaultFallback = "test_fallback")
public class PaymentServiceImpl_1 implements PaymentService_1 {


    //通用
    @Override
    @HystrixCommand //没有特别指明,使用公用降级方法test_fallback
    public String selectPaymentById(String id) {
        return "server 8001 : selectPaymentById"+ Thread.currentThread().getName() + "ok~";
    }

    @Override
    @HystrixCommand(fallbackMethod = "test_fallback_timeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    public String timeOut(String id) {
        try {
            long s = 6;
            TimeUnit.SECONDS.sleep(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "server 8001 : timeOut"+ Thread.currentThread().getName() + "ok~";
    }

    //服务降级方法
    public String test_fallback(){
        return "test_fallback 请重试!";
    }
    public String test_fallback_timeout(String id){
        return "test_fallback_timeout 请重试!" + id;
    }

}
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableCircuitBreaker //开启熔断器 @EnableHystrix
public class ApplicationHystrix8001 {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationHystrix8001.class, args);
    }
}

 

客户端

1.服务降级处理实现类

controller
@RestController
@RequestMapping("/order/hystrix")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @GetMapping("/consumer/selectPaymentById/{id}")
    public String selectPaymentById(@PathVariable String id){
        return orderService.selectPaymentById(id);
    }

    @GetMapping("/consumer/timeOut/{id}")
    public String timeOut(@PathVariable String id){
        return orderService.timeOut(id);
    }
}
service
@FeignClient(value = "CLOUD-PAYMENT-HYSTRIX-SERVICE8001", fallback = OrderServiceHystrix.class)
@Component
public interface OrderService {
    @GetMapping("/payment/hystrix/selectPaymentById/{id}")
    public String selectPaymentById(@PathVariable String id);

    @GetMapping("/payment/hystrix/timeOut/{id}")
    public String timeOut(@PathVariable String id);
}
/**
 * 降级处理
 */
@Component
public class OrderServiceHystrix implements OrderService {
    @Override
    public String selectPaymentById(String id) {
        return "selectPaymentById" + "服务不可用";
    }

    @Override
    public String timeOut(String id) {
        return "timeOut 服务不可用";
    }
}

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
@EnableFeignClients
public class ApplicationHystrix8083 {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationHystrix8083.class, args);
    }
}

server:
  port: 8083
spring:
  application:
    name: cloud-order-service-openfeign-hystrix
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka  #单机版
#      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   # 集群
ribbon:
  ReadTimeout: 5000 # 超时配置
  ConnectTimeout: 5000
feign:
  hystrix:
    enabled: true
pom加入
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

自己看的笔记,不喜勿喷!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值