springcloud——hystrix服务消费者方服务降级
程序员文章站
2024-03-20 22:10:58
...
1、主配置文件
server.port=81
spring.application.name=payment-consumer
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/
#开启feign对hystrix的支持
feign.hystrix.enabled=true
2、主启动类
package com.springcloud;
import lombok.extern.slf4j.Slf4j;
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;
/**
* @author dc
* @date 2020/7/29 - 15:28
*/
@SpringBootApplication
@Slf4j
@EnableEurekaClient //开启eureka客户端
@EnableFeignClients //开启feign客户端
@EnableHystrix //开启hystrix
public class OrderHystrixMain81 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain81.class, args);
}
}
3、控制器
package com.springcloud.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author dc
* @date 2020/7/29 - 15:33
*/
@RestController
@Slf4j
public class PaymentHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok")
public String paymentInfo_OK(Integer id) {
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout")
@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties =
{@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
value="1500")})
//fallbackMethod:服务降级方法
//name:属性名 value:属性值(当控制器运行时间超过该值时,将会进入服务降级方法)
public String paymentInfo_TimeOut(Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentTimeOutFallbackMethod(Integer id) {
return "我是消费者81,对方支付系统繁忙请10秒钟后再试或者自己运行出错,请检查自己!";
}
}
上一篇: C++内联函数inline
下一篇: @HystrixCommand使用
推荐阅读
-
springcloud——hystrix服务消费者方服务降级
-
Kite的学习历程SpringCloud之Hystrix服务降级客户端通配服务降级
-
springCloud Finchley 微服务架构从入门到精通【七】断路器 Hystrix(ribbon)
-
springCloud Finchley 微服务架构从入门到精通【四】服务消费者(feign)
-
springCloud Finchley 微服务架构从入门到精通【三】服务提供者/服务消费者(ribbon)
-
springCloud Finchley 微服务架构从入门到精通【八】断路器 Hystrix(feign)
-
springcloud使用Hystrix进行微服务降级管理
-
详解springcloud 基于feign的服务接口的统一hystrix降级处理
-
详解springcloud 基于feign的服务接口的统一hystrix降级处理
-
SpringCloud微服务:Sentinel哨兵组件,管理服务限流和降级