【Spring Cloud】分布式必学springcloud(六)——服务容错Hystrix

一、前言

      在上篇博客中,小编向大家介绍了springcloud负载均衡的Ribbon。它可以帮助我们把服务器请求转换为负载均衡请求。但是当ribbon在调用的时候,如果没有调用成功。会引起雪崩效应。微服务架构中,存在很多的服务单元,若一个单元出现故障,就容易因依赖关系而引发故障蔓延,最终导致整个系统的瘫痪,这样的脚骨相较传统的架构更加不稳定,为了解决这种问题,就产生了断路器等一系列的服务保护机制。

二、什么是断路器

      在生活中,我们家里都有保险丝,当电流过大的时候,就会烧断。个人感觉断路器就跟保险丝很相似。

      断路器 能够及时检测短故障电路,防止发生过载,发热甚至起火等严重后果。在分布式系统中,当一个服务出现异常后,通过断路器的故障监控,向调用方返回一个错误响应,而不是漫长的等待。这样就不会使得线程因调用故障服务被长时间占用不释放。

      Spring Cloud Hystrix提供了断路器、线程隔离等一系列服务保护功能。具备服务降级、服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等强大功能。

这里写图片描述

三、实践

3.1 准备

      根据上一篇博客搭建好的框架,我们在这个基础上进行修改,您可以在这里得到代码。

      https://github.com/AresKingCarry/SpringCloudDemo

3.2 引入依赖

      首先在ribbon项目的pox.xml文件中加入spring-cloud-starter-hystrix的依赖:

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

3.2 修改相关的备注

      启动类上添加@EnableHystrix依赖或@EnableCricuitBreaker依赖:

package com.wl.ribbon;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
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 RibbonApplication {

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


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


	@Bean
	public IRule ribbonRule() {
		return new RandomRule();//实例化与配置文件对应的策略类
	}

}

      调用其他服务的类的方法上,加 @HystrixCommand(fallbackMethod = "xxxx")注解:

package com.wl.ribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Ares on 2018/4/18.
 */
@RestController
public class HelloController {

    @Autowired
    RestTemplate restTemplate;


    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "hiFallback")
    public String getUser(@RequestParam("id") String id) {
        this.loadBalancerClient.choose("CLIENT1");//随机访问策略
        return restTemplate.getForEntity("http://CLIENT1/user/findById?id="+id, String.class).getBody();
    }


    @RequestMapping(value = "/hi")
    @HystrixCommand(fallbackMethod = "hiFallback")
    public String hi(@RequestParam("id") String id){
        return  restTemplate.getForObject("http://CLIENT1/user/findById?id="+id,String.class);

    }

    public String hiFallback(String id){
        return "hi方法远程调用超时"+id;
    }
}

      注意:这里需要指明的是, @HystrixCommand(fallbackMethod = “hiFallback”),hiFallback方法要和hi方法的返回值,参数相同。

3.3 尝试

      依次开启 注册中心Eureka,负载均衡ribbon,两个提供者。

      通过访问ribbon:http://localhost:7000/hi?id=wl,当服务可以使用的时候,多次访问正常显示:

这里写图片描述

这里写图片描述

      当把其中一个提供者的服务关闭后,再次访问:可见,当报错的时候会优雅的返回字符串,不会报错误。

这里写图片描述

      如果不设置断路器呢?是什么样的呢?

这里写图片描述

      通过对比效果还是蛮明显的。

四、小结

      如果您读了小编前面的博客,并跟着做了,相信到这里,您是不是感觉有种循序渐进的感觉,一环套一环。springcloud为我们提供了这些的东西真是好东西,相信您会更加的喜欢。下面会向您介绍spirng cloud Hystrix的监控平台。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值