在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
为了解决这个问题,业界提出了断路器模型。
一、断路器简介
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组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图:
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
二、Ribbon+Hystrix
pom文件添加jar文件
<!-- 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
启动类添加@EnableHystrix 注解
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.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient // 定义为客户端
@EnableHystrix // 设置断路器
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
/**
* @LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
HelloService类配置断路方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
/**
* 问题一: 什么是REST REST(RepresentationalState Transfer)是Roy Fielding
* 提出的一个描述互联系统架构风格的名词。REST定义了一组体系架构原则,您可以根据这些原则设计以系统资源为中心的Web
* 服务,包括使用不同语言编写的客户端如何通过 HTTP处理和传输资源状态。
*
* 为什么称为 REST?Web本质上由各种各样的资源组成,资源由URI
* 唯一标识。浏览器(或者任何其它类似于浏览器的应用程序)将展示出该资源的一种表现方式,或者一种表现状态。
* 如果用户在该页面中定向到指向其它资源的链接,则将访问该资源,并表现出它的状态。
* 这意味着客户端应用程序随着每个资源表现状态的不同而发生状态转移,也即所谓REST。
*
* 问题二: RestTemplate Spring'scentral class for synchronous client-side HTTP
* access.It simplifies communication with HTTPservers, and enforces RESTful
* principles. Ithandles HTTP connections, leaving application code to provide
* URLs(with possible template variables) andextract results.
* 简单说就是:简化了发起HTTP请求以及处理响应的过程,并且支持REST
*
* @author 于志强
*
* 2018年11月28日 上午11:10:45
*/
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate; //
@HystrixCommand(fallbackMethod = "hiError") // hiError是你服务断后走的方法名字
public String hiService() {
return restTemplate.getForObject("http://CLIENTNAME/test", String.class);
}
public String hiError() {
return "页面访问不到,走到失败结果";
}
}
三、测试方法
启动discSystem-3
启动ribbon项目
访问http://localhost:12348/demo
启动discSystem-4项目
------------------------------------------------------------------------------------------------------------------------------------------------------
一、Feign+Hystrix
Feign是自带断路器的,在D版本的Spring Cloud中,它没有默认打开。需要在配置文件中配置打开它,在配置文件加以下代码:
feign:
hystrix:
enabled: true
SchedualCilentName类添加fallback = SchedualServiceHiHystric.class SchedualServiceHiHystric 为该接口实现类
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 定义一个Feign接口类
* @author 于志强
*
* 2018年11月28日 上午11:43:20
*/
@FeignClient(value = "clientName",fallback = SchedualServiceHiHystric.class)
public interface SchedualCilentName {
// value值必须与discSystem-4、discSystem-4-1中方法名一致
@RequestMapping(value = "/test",method = RequestMethod.GET)
String sayHiFromClientOne();
}
创建SchedualServiceHiHystric类(SchedualServiceHiHystric需要实现SchedualServiceHi 接口,并注入到Ioc容器中)
import org.springframework.stereotype.Component;
@Component
public class SchedualServiceHiHystric implements SchedualCilentName {
@Override
public String sayHiFromClientOne() {
return "sorry";
}
}
不需要修改pom文件
二、测试方法
启动discSystem-3
启动service-feign项目
访问http://localhost:12349/demo
启动discSystem-4项目