SpringCloud的搭建4_断路器(Hystrix)

断路器(Hystrix)

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

一、断路器简介

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图在这里插入图片描述
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
在这里插入图片描述
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

二、准备

首先启动上一篇文章的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8762。

三、在ribbon使用断路器

第一步 改造工程

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

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

2、修改appliation.java,加入@hystrix注解

package com.jiawen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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;

@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(RibbonClientApplication.class,args);
    }

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

3、修改service

package com.jiawen.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(){
        return "sorry , hi server is error , please try soon.";
    }
    
}

第二步 验证

1、启动EurekaServer,启动EurekaClient,启动EurekaRabbon。
2、访问 http://localhost:1213/hi?name=nihao 界面如下:

在这里插入图片描述
3、关闭EurekaClient服务,重新访问
访问:http://localhost:1213/hi?name=nihao 界面如下:
在这里插入图片描述

四、在ribbon使用断路器

第一步 改造工程

在第一次创造service-feign工程中,feign组件其实已经集成了hystirx,但是没有开启,需要到application.yml中开启。

application.yml插入以下信息

feign:
  hystrix:
    enabled: true

SchedualServiceHi 类添加一个属性

package com.jiawen.service;

import com.jiawen.service.impl.SchedualHytrixServiceHi;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-hi" , fallback = SchedualHytrixServiceHi.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi" , method = RequestMethod.GET)
    String SayHi(@RequestParam (value = "name") String name);
}

创建一个SchedualServiceHi 的实现类

package com.jiawen.service.impl;

import com.jiawen.service.SchedualServiceHi;
import org.springframework.stereotype.Component;

@Component
public class SchedualHytrixServiceHi implements SchedualServiceHi {
    @Override
    public String SayHi(String name) {
        return "hi " + name + " , sorry ,please try soon.";
    }
}

第二步 开始验证

启动EurekaServer服务,EurekaClient服务,EurekaHytrix服务。

启动成功后开始访问:http://localhost:1214/hi?name=nihao 界面如下
在这里插入图片描述

关闭EurekaClient服务,开始访问:http://localhost:1214/hi?name=nihao 界面如下:
在这里插入图片描述
到这里就结束了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值