spring cloud RestTemplate消费者使用Hystrix进行容错和服务降级

spring cloud RestTemplate消费者使用Hystrix进行容错和服务降级

(1)消费者如下

@RestController
public class DcController {

    @Autowired
    ConsumerService consumerService;

    @GetMapping("/consumer")
    public String dc(Boolean delay) {
        return consumerService.consumer(delay);
    }

    @Service
    class ConsumerService {

        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer(Boolean delay) {
            System.out.println("consumer delay :" + delay);
            return restTemplate.getForObject("http://eureka-client/dc" + (null == delay ? "" : "?delay=" + delay), String.class);
        }

        public String fallback(Boolean delay) {
            System.out.println("fallback delay :" + delay);
            return "fallbck22";
        }

    }

要解决的问题:

(a)降级方法可以重定向吗?

可以.

?a 借助于org.springframework.web.client.RestTemplate

 public String fallback(Boolean delay) {
            
            return restTemplate.getForObject("http://eureka-client/client/hello/json",String.class);
        }

?b 使用HttpServletResponse

@RestController
public class DcController {

    @Autowired
    ConsumerService consumerService;

    @GetMapping("/consumer")
    public String dc(Boolean delay,
                     HttpServletRequest request,
                     HttpServletResponse response) {
        return consumerService.consumer(delay,
                 request,
                 response);
    }

    @Service
    class ConsumerService {
        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer(Boolean delay,
                               HttpServletRequest request,
                               HttpServletResponse response) {
            System.out.println("consumer delay :" + delay);
            return restTemplate.getForObject("http://eureka-client/dc" + (null == delay ? "" : "?delay=" + delay), String.class);
        }

        public String fallback(Boolean delay,
                               HttpServletRequest request,
                               HttpServletResponse response) {
            System.out.println("fallback delay :" + delay);
            try {
                response.sendRedirect("error2.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

(b)降级的标准如何设置(重写)

设置调用者执行的超时时间(单位毫秒)

默认值:1000

// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=...

注意事项:

(1)真实方法和降级方法除了方法名称不同,其他签名信息必须完全一样

例如consumer 有一个参数,那么fallback也必须有一个参数,否则报错:

(2)注解@HystrixCommand 不能加在控制器层,而应该加在Service 层

所以才专门抽取了一个Service 类:ConsumerService

(3)

代码:

https://github.com/liuyu520/SpringCloud-Learning-Dalston-/tree/master/eureka-consumer-ribbon-hystrix

猜你喜欢

转载自my.oschina.net/huangweiindex/blog/1826023