Spring cloud微服务 Hystrix熔断器学习教程

       以下demo代码:https://github.com/wades2/HystrixtDemo

       官网定义:Hystrix是一个延迟容错库。在分布式环境中,许多服务依赖项中的一些不可避免地会失败。Hystrix是一个库,可通过添加延迟容错和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,阻止它们之间的级联故障以及提供后备选项来实现这一点,所有这些都可以提高系统的整体弹性。

       因为在分布式系统中很多服务是相互依赖的,假如以下某个功能实现依赖于几个服务:

       假如此时【家庭信息查询服务】由于请求过多或者服务器原因出现了大批量服务不可用,【个人信息查询服务】由于无法请求到【家庭信息查询服务】,一直阻塞,造成服务器压力越来越大,后续又有【人员信息查询】的请求来了。就好比堵车的公路上又来了一大批车;此时会每个依赖关系都会在某些时候不可避免地失败。如果主机应用程序未与这些外部故障隔离,则整个系统可能会崩溃。

        此时Hystrix熔断器就发挥了作用:

        1、通过第三方客户端库访问(通常通过网络)依赖关系,以防止和控制延迟和故障。
        2、在复杂的分布式系统中停止级联故障。(在一段时间内侦测到许多类似的错误,会强迫其以后的多个调用快速失败,不再访问远程服务器)
        3、快速失败并迅速恢复。(快速失败后【个人信息查询服务】不会由于阻塞过载,就不会导致雪崩,)
        4、在可能的情况下,后退并优雅的降级。
        5、实现近实时监控,警报和操作控制。(如果熔断器诊断【家庭信息查询服务】错误已经修正,应用程序会再次尝试调用这个服务。)

        这样我们就在之前学习Euraka+ribbon的基础上加熔断机制:

        首先在我们的EurekaCaller里面添加上需要的依赖:

【注:如果不清楚的朋友或者没有重头看的朋友,这里附上我demo的

git地址:https://github.com/wades2/EurekaDemo2

完整的搭建教程在:https://blog.csdn.net/asd529735325/article/details/85044158

如果你的Eureka和ribbon已经搭建好了可以直接跳过。】

我们的项目基本是这个样子的:

Eurekacaller是这个样子:

       添加pom依赖引入需要的jar包:

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

 重写一下我们的Controller:

package com.example.demo.controller;

import com.example.demo.service.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
public class Controller {

    @Autowired
    private Services service;

    @RequestMapping(value = "getUser/routine",method = RequestMethod.GET)
    public String routine(@RequestParam String id){
        String json=service.routineService(id);
        return json;
    }
}

       然后是我们具体的service,负载的部分,我们的断熔就放在service这里,一旦请求超时或者失败,立即失败并触发降级方法,我这里的降级方法是:callback。

package com.example.demo.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

@Service
public class Services {
    @Autowired
    private RestTemplate restTemplate;

    //实现的callback方法来实现短路保护
    @HystrixCommand(fallbackMethod = "callback")
    public String routineService(String id){
        System.out.println(id);
        String jsons = restTemplate.getForObject("http://Eureka-client/user/getUser2", String.class);
        return jsons;
    }

    public String callback(String id){
        String msg="出现错误id:"+id;
        return msg;
    }

}

 因为在前期我们没有扫描/user/getUser2所在的包路径,所以这个是肯定扫描不到的,访问一定是404not found的。

 然后启动我们的5个启动类,访问http://localhost:8086/getUser/routine?id=52,我们可以看到访问到了callback页面:

这样一来,即使EurkaClient都出现了问题或者过载,我们也可以降级处理。那我们再试试如果超时会怎么样。

修改配置文件。,添加超时设置:

spring.application.name=Eureka_caller
server.port=8086
eureka.client.serviceUrl.defaultZone=http://user:123456@localhost:8083/eureka,http://user:123456@localhost:8082/eureka

#hystrix配置
hystrix.metrics.enabled=true
#默认的超时时间设置
hystrix.metrics.polling-interval-ms=2000

修改下Ribbon请求的路径:

//实现的callback方法来实现短路保护
    @HystrixCommand(fallbackMethod = "callback")
    public String routineService(String id){
        System.out.println(id);
        String jsons = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);
        return jsons;
    }

 修改对应Service提供者的Client具体实现类(也就是修改EurekaClient和EurekaClient2),使其沉睡时间大于我们的熔断请求延迟时间,模拟服务器过载阻塞:

package com.example.demo.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.converters.Auto;
import com.netflix.discovery.shared.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private EurekaClient eurekaClients;

    @GetMapping("getUser")
    public String getUser() {
        //todo 得到eureka server的服务实例
        InstanceInfo info=eurekaClients.getNextServerFromEureka("Eureka-client",false);
        try{
            Thread.sleep(60000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "hello one";
    }

}

我们看到请求后依然页面被拦截熔断了,因为等待时间60000毫秒远远大于了我们设置熔断等待时间:2000毫秒。

以上就是我们关于Hystrix熔断器的学习,各位看官大佬爷有什么问题请留言区评论,大家一起讨论解决。

 

猜你喜欢

转载自blog.csdn.net/asd529735325/article/details/85124842