第三篇 服务的容错 (Hystrix)

在微服务架构中,根据业务来分解成一个单独的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud中可以使用RestTemplate + Ribbon和Feign来调用。通常会分配部署。由于网络原因或者本身的原因,服务并不能保证100%可用,如果拆分服务出现问题,调用此服务就会出现线程中断,此时若有大量的请求涌入,Servlet容器的线程资源会被耗尽,导致服务崩溃。。。。。为了解决这个问题,提出了断路器模式

一、断路器简介

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

在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图:Netflix开源了Hystrix组件,实现了中断模式,SpringCloud对这一组件进行了整合。

 

二、准备工作

这篇文章基于上一篇文章的工程,首先启动上一篇文章的工程,

启动eureka-server工程、端口号为1111

启动eureka-provide工程,端口为2222   服务命名为 client-provide

 

三、在ribbon中使用断路器

1、改造serice-ribbon工程的代码,首先在pox.xml文件中加入spring-cloud-starter-hystrix的依赖:

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

2、在程序的启动类ServiceRibbonApplication加@EnableHystrix注解开启Hystrix:

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonConsumerApplication {

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

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

}

3、改造ConsumerService类,在hello()方法上加上@HystrixCommand注解。该注解该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串"error" 

@Service
public class ConsumerService {

    @Autowired
    RestTemplate restTemplate;


    @HystrixCommand(fallbackMethod = "helloFallBack")
    public String hello() {
        return restTemplate.getForEntity("http://client-provide/hello", String.class).getBody();
    }
    
    public String helloFallBack()
    {
        return "error";
    }
}

ConsumerController不进行任何修改

@RestController
public class ConsumerController {

   @Autowired
   ConsumerService consumerService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return consumerService.hello();
    }

}

下面我们验证一下断路器的回调逻辑(也称服务降级)

我们依次开启注册中心(1111)、两个相同服务的生产者(端口不同,2222,2223)、ribbon

此时访问ribbon可以轮询访问两个生产者,并返回文字信息.当我们断开2223端口后,当轮询到2223端口时,输出的内容为error,而不会出现线程因服务宕机而等待响应超时,这很好的控制了容器的螺纹一对。

 

四、总结

如果不使用断路器,则断开2223端口,由于服务不存在,请求调用会延迟等待一段时间,导致响应缓慢。并返回以下内容,

除了模拟某个节点无法访问之外,我们还可以模拟服务阻塞(长时间未响应)的情况,我们对生产者的hello接口做一些修改

@RestController
public class HelloController {

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

    @RequestMapping(value = "/hello" ,method = RequestMethod.GET)
    public String hello() throws InterruptedException {
        //让处理线程等待几秒
        int sleepTime = new Random().nextInt(3000);
        System.out.println("sleepTime: " + sleepTime);
        Thread.sleep(sleepTime);
        
        return "hello,I am provider port :" + port;
    }
}

通过Thread.sleep()可以让hello接口的处理线程不是马上返回内容,而是在阻塞几秒后才返回内容,由于Hystirx默认超时时间是1000毫秒,所以这里采用了随机数让处理线程有一定的概率发生超时时间来出发断路器

我们可以观察到,当spend time大于1000的时候,就会返回error,即消费者因调用的服务超时从而触发熔断请求,并调用回调逻辑返回结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值