springCloud Finchley 微服务架构从入门到精通【七】断路器 Hystrix(ribbon)

一、Hystrix 简介

在分布式系统中经常会出现某个微服务故障的情况,而出现此情况如果不添加任何措施,那么可能出现的现象是:

1、每个调用者调用到此故障服务就会出现等待直至超时;
2、其他的调用者依然有机率去调用到此故障节点,不断消耗线程资源;
3、微服务的依赖调用会使系统中故障传播,直至系统性能瘫痪

Hystrix 便是为了解决上述问题,其配置了故障时的容错方法,当某个微服务节点出现故障,会快速执行定义的容错方法中,并在若干秒后通知所有的微服务调用者,该节点为故障节点,之后会从可用列表中选择调用。

二、Ribbon 整合 Hystrix

1、添加Hystrix依赖

在pom.xml中添加spring-cloud-starter-netflix-hystrix依赖,主要xml如下:

       <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2、代码实现

启动类添加 @EnableHystrix 注解以开启 Hystrix 特性

package com.mayi.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class BussnessserviceUserClientRibbonHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(BussnessserviceUserClientRibbonHystrixApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

使用 @HystrixCommand 注解的 fallbackMethod 指定失败方法,并实现该方法:

package com.mayi.springcloud.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class UserManagementRibbonClient {

    @Autowired
    RestTemplate restTemplate;

    @Value("${server.port}")
    String port;

    @GetMapping("/listUsersByRibbon")
    @HystrixCommand(fallbackMethod="listUsersByRibbonFallback")
    public String listUsersByRibbon(){
        String result = this.restTemplate.getForObject("http://service-user/listUsers", String.class);
        return result;
    }

    public String listUsersByRibbonFallback(){
        return "listUsersByRibbon异常,端口:" + port;
    }
}

三、测试

依次运行eureka server – config server – service-user(启动两个不同端口) – service-user-client-ribbon-hystrix:

这里写图片描述

使用service-user-client-ribbon-hystrix调用,交替出现如下结果:

这里写图片描述

断掉其中一个service-user节点:

这里写图片描述

发现,数秒钟后(可配置)便不再去调用故障节点。

源码地址:https://github.com/tianyana/springcloud/tree/master/bussnessservice-user-client-ribbon-hystrix

欢迎加入JAVA架构师QQ群(初建):618578034

这里写图片描述

以上公众号从2018-5.1日 - 2019.5.1日期间,将要按照JAVA高级软件架构师实战培训的路线发布一期完整的架构文章,难度由浅入深,适合有一定开发基础想转架构和正在做初级架构开发的人员学习(springcloud更新完毕即开始)

猜你喜欢

转载自blog.csdn.net/wcblog/article/details/80251226