Hystrix学习(一、初探)

本文介绍了如何在Spring Cloud环境中利用Hystrix进行服务熔断和降级,以防止微服务雪崩。通过在服务提供者引入Hystrix依赖并配置注解,实现在调用链路中某环节失败时执行降级逻辑,确保系统稳定性。
摘要由CSDN通过智能技术生成

Hystrix,熔断器。阻止应用长时间请求无法收到响应而造成奔溃,如果是A调B,B调C,C调D这种复杂的调用,如果C调D阻塞了,那么B、A也会跟着阻塞,可能导致整个应用的雪崩。

引入包

父工程

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                <version>${hystrix.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
                <version>${hystrix.dashboart.version}</version>
            </dependency>

对应版本:

        <hystrix.version>2.2.2.RELEASE</hystrix.version>
        <hystrix.dashboart.version>2.2.2.RELEASE</hystrix.dashboart.version>

 这里遇到一个小插曲,父项目引用了这两个依赖后,用idea的maven插件的clean、install,始终报红色,找不到依赖。后来实在没招了,直接在子工程里引用这两个依赖,然后用子工程clean、install,就OK了。猜测是因为父工程师pom打包形式,并不会实质的下载jar包。以前没有用过maven,没有经验。

 子工程

hystrix是针对服务端的,所以我们在producer工程上引用hystrix的两个依赖。

即,如果是A调B,则在B上加。

引入依赖

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

降级

producer工程的controller上,增加HystrixCommond注解,以及降级方法

package com.dean.controller;

import cn.hutool.core.util.RandomUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

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

    @GetMapping(value = "/hello")
    @HystrixCommand(fallbackMethod = "hystrixFallback")
    public String hello() throws Exception{
        //随机产生一个boolean,模拟异常
        boolean flag = RandomUtil.randomBoolean();
        if(flag){
            return "你好,这里是Eureka Client,端口号是:" + port;
        }else{
            throw new Exception("" + port + "端口的hello接口报错了");
        }
    }

    /**
     * 出错后会调用该降级方法,返回指定的信息
     * @return
     */
    public String hystrixFallback(){
        return "这是"+port+"的hystrixFallback方法(降级)";
    }

}

还有很重要的,在producer工程的启动类上,添加Hystrix的启用注解(EnableHystrix),刚开始的时候我漏加了,直接就抛异常了,并没有进入降级的方法里。

package com.dean;

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;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class EurekaProducerApplication {

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

PS:有的教程里写得是在Application类中引用EnableCircuitBreaker标签,实际访问看,我这个版本的Hystrix已经将这个标签置为过时了。

 

启动,访问consumer的接口:http://localhost:9201/getInfo

多次F5刷新地址,访问的端口是随机的,连续6次的端口刷新,分别是9102、9101、9103循环的,并且是否进入降级方法,也是随机的,是根据flag这个随机boolean判断的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值