温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Cloud 中Hystrix有什么用

发布时间:2021-06-18 16:37:27 来源:亿速云 阅读:159 作者:Leah 栏目:大数据

本篇文章给大家分享的是有关Spring Cloud 中Hystrix有什么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

断路器简介

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.

Netflix创建了一个名为Hystrix的库,该库实现了断路器模式。在微服务架构中,通常有多个服务调用层。

Spring Cloud 中Hystrix有什么用

较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。

Spring Cloud 中Hystrix有什么用

断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

准备工作

在之前工程的基础上, 启动eureka-server,端口为9090;启动eureka-client, 端口为8040。

在Ribbon中使用断路器

改造rebbon-service工程的代码,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依赖:

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

在项目启动类上注解@EnableHystrix, 开启断路器能力:

@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
public class RibbonServiceApplication {

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

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

}

改造HelloController类, 在hello方法加上@HystrixCommand注解。 该注解给方法低通了熔断器的能力, 指定fallbackMethod熔断方法, 当远程服务调用时候后执行熔断方法:

@RestController
public class HelloController {

    private final RestTemplate restTemplate;

    @Autowired
    public HelloController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @HystrixCommand(fallbackMethod = "helloError")
    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);
    }

    public String helloError(String name) {
        return String.format("Hello, %s! Access remote service fail!", name);
    }
}

启动ribbon-service项目,在浏览器访问 http://localhost:8050/hello?name=Mars:

Hello, My name is Mars, I'm from port: 8040

这时候我们关闭eureka-service项目, 再次访问 http://localhost:8050/hello?name=Mars:

Hello, Mars! Access remote service fail!

这就说明eureka-service服务不可达时, ribbon-service调用接口会快速失败, 直接调用熔断方法, 而不是等待响应超时, 这很好的控制了容器的线程阻塞。

Feign中使用断路器

Feign已经集成了断路器, 基于feign-service项目进行改造, 只需要在@FeignClient注解中加上fallback的指定类就行:

@FeignClient(value = "hello-eureka-client", fallback = FeignServiceHystrix.class)
public interface FeignService {

    @GetMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}

FeignServiceHystrix需要实现FeignService接口,并注入到Ioc容器中:

@Component
public class FeignServiceHystrix implements FeignService {
    @Override
    public String hello(String name) {
        return String.format("Hello, %s! Access remote service fail!", name);
    }
}

先启动eureka-client和eureka-server项目, 然后再启动feign-service项目,在浏览器访问 http://localhost:8080/hello?name=Mars:

Hello, My name is Mars, I'm from port: 8040

这时候我们关闭eureka-service项目, 再次访问 http://localhost:8080/hello?name=Mars:

Hello, Mars! Access remote service fail!

以上就是Spring Cloud 中Hystrix有什么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI