【Spring Cloud 系列】五、断路器(Hystrix)+ Ribbon (Hoxton.M3 版本)

1,101 阅读3分钟

这是我参与更文挑战的第11天,活动详情查看:更文挑战

本文是基于Hystrix+ Ribbon 实现断路器模式

环境:

IDEA
JDK1.8
Spring Cloud Hoxton.M3
Spring Boot 2.2.0

一、Hystrix简介

  背景:在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。

  解决:Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。在微服务架构中,一个请求需要调用多个服务是非常常见的较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

二、创建项目

1、File ----- New -----Project

1.jpg

2、Spring Initializr ----- Next

2.jpg

3、等待创建项目中

3.jpg

4、输入 Group 和 Artifact

点击 Next

4.jpg

5、选择Hystric

5.jpg

6、点击 Finish 创建完成

6.jpg

三、完善项目

1、pom.xml增加依赖

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

2、HystrixserverApplication增加@EnableHystrix注解开启Hystrix,并创建RestTemplate ,开启客户端负载均衡。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class HystrixserverApplication {

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

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

}

3、配置文件


server.port=8765
spring.application.name=RibbonServer
#注册到服务中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#是否注册到eureka服务器,
eureka.client.registerWithEureka=true 
#是否从eureka服务器获取注册信息
eureka.client.fetchRegistry=true
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true

4、Controller


@RestController
public class HystrixController {
    @Autowired
    private HystrixService hystrixService;
    @RequestMapping("/gethystrix")
    public String getHystrix(String name){
       return hystrixService.getHystrix(name);
    }
}

5、Service


public interface HystrixService {
    String getHystrix(String name);
}

6、Serviceimpl需要注入RestTemplate,

  Serviceimpl需要注入RestTemplate,在方法上添加 @HystrixCommand(fallbackMethod ="fallback")并指定fallbackMethod ,编写fallbackMethod方法,设置返回信息。@HystrixCommand该注解对该方法创建了熔断器的功能。

@Service
public class HystrixServiceImpl implements HystrixService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod ="fallback")
    @Override
    public String getHystrix(String name) {
        String result = restTemplate.getForObject("http://Client-Server1/test?name=" + name, String.class);
        return result;
    }
    public String fallback(String name){
        return "hi,"+name+",sorry,Hystrix Ribbon error!";
    }
}

四、运行项目

1、输入项目地址和参数

http://localhost:8765/gethystrix?name=123

  在CLIENT-SERVER1 和CLIENT-SERVER2两个项目都正常运行的情况下,实现负载均衡,返回结果如下

7.jpg

8.png

2、端口8762和8763来回切换,实现了负载均衡。

  现在关闭CLIENT-SERVER1 和CLIENT-SERVER2 再次访问接口,返回接口如下,进入了fallback方法,实现了短路,减少了因链接异常造成的线程阻塞问题,避免了容器线程的浪费。

9.jpg

  以上就是一个Spring Cloud 基于Hystrix+ Ribbon实现断路器模式的学习过程。欢迎交流学习。