首页 > 其他 > 详细

服务容错和Hystrix

时间:2019-02-17 23:12:54      阅读:226      评论:0      收藏:0      [点我收藏+]

一、雪崩效应

在微服务架构中,通常有多个服务层调用,如果某个服务不可用,造成调用的服务也不可用,造成整个系统不可用的情况,叫做雪崩效应

技术分享图片

 

二、Hystrix

放雪崩利器Hystrix,基于Netflix对应的Hystrix。

Hystrix功能: 服务降级,服务熔断,依赖隔离, 监控(Hystrix Dashboard)

1、服务降级

优先核心服务,非核心服务不可用或弱可用

通过HystrixCommand注解指定

fallbackMethod(回退函数)中具体实现降级逻辑

 

三、Order工程中使用RestTemplate调用Product中的方法

 @GetMapping("/getProductInfoList")
    public  String getProductInfoList(){
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class);

    }

  返回结果

技术分享图片

 当Product服务关闭后,再次访问,将返回连接拒绝

技术分享图片

 

然后使用Hystrix进行服务降级

在Order服务中增加引用

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>RELEASE</version>
        </dependency>

  

增加注解EnableCircuitBreaker

技术分享图片

或者用@SpringCloudApplication替换另外三个注解

 技术分享图片

 

增加HystrixController 类

@RestController
public class HystrixController {



    @HystrixCommand(fallbackMethod = "fallback")
    @GetMapping("/getProductInfoList")
    public  String getProductInfoList(){
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class);

    }

    private String fallback(){
        return "太拥挤了,请稍后再试~~";
    }
}

  

再次调用,Product服务此时是关闭的。

技术分享图片

说明服务降级了。

服务容错和Hystrix

原文:https://www.cnblogs.com/linlf03/p/10392738.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!