在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
为了解决这个问题,业界提出了断路器模型。
断路器介绍
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图:
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
在Ribbon中使用断路器
改造之前的Ahut-Service-Ribbon项目
pom.xml文件中添加hystrix的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<description>service ribbon</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 添加hystrix依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目启动类添加@EnableHystrix注解开启Hystrix
package com.ahut;
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.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author cheng
* @className: ServiceRibbonApplication
* @description: service ribbon
* @dateTime 2018/4/26 14:23
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
/**
* @description: 注入restTemplate, 并且开启负载均衡
* @author cheng
* @dateTime 2018/4/26 14:25
*/
@Bean
@LoadBalanced
public RestTemplate createRestTemplate() {
return new RestTemplate();
}
}
改造HelloServiceImpl类,在showPort方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,代码如下:
package com.ahut.serviceImpl;
import com.ahut.service.HelloService;
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;
/**
* @author cheng
* @className: HelloServiceImpl
* @description:
* @dateTime 2018/4/26 14:35
*/
@Service
public class HelloServiceImpl implements HelloService {
@Autowired
private RestTemplate restTemplate;
/**
* @description: 展示服务器端口
* @author cheng
* @dateTime 2018/4/26 14:35
*/
@HystrixCommand(fallbackMethod = "handleError")
@Override
public String showPort() {
return restTemplate.getForObject("http://AHUT-EUREKA-CLIENT/v1/showPort", String.class);
}
/**
* @description: 断路器方法
* @author cheng
* @dateTime 2018/4/27 10:17
*/
public String handleError() {
return "I am so sorry,Server has error";
}
}
启动项目:
- Ahut-Eureka-Server(注册中心)
- Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
- Ahut-Service-Ribbon(AHUT-SERVICE-RIBBON服务消费者)
访问eureka:
访问Ahut-Service-Ribbon http://localhost:8940/v1/hello
此时,关闭Ahut-Eureka-Client-One服务,再次访问Ahut-Service-Ribbon http://localhost:8940/v1/hello
这就说明当 Ahut-Eureka-Client-One 工程不可用的时候,Ahut-Service-Ribbon调用 Ahut-Eureka-Client-One的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。
此时的项目架构
在Feign中使用断路器
Feign是自带断路器的,在D版本的Spring Cloud中,它没有默认打开。需要在配置文件中配置打开它。
改造之前的Ahut-Service-Feign项目
在application.properties配置文件中打开hystrix配置:
# 配置启动断路器
feign.hystrix.enabled=true
在FeignClient的HelloService接口的注解中加上fallback的指定类就行了:
package com.ahut.service;
import com.ahut.hystrix.HelloServiceHystrix;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author cheng
* @interfaceName: HelloService
* @description:
* @dateTime 2018/4/26 16:39
*/
@FeignClient(value = "AHUT-EUREKA-CLIENT", fallback = HelloServiceHystrix.class)
public interface HelloService {
@RequestMapping(value = "/v1/showPort")
String showPort();
/**
* @description: 演示带参数
* @author cheng
* @dateTime 2018/4/26 16:45
*/
//@GetMapping(value = "/demo")
//void demo(@RequestParam(value = "name") String name);
}
HelloServiceHystrix需要实现HelloService 接口,并注入到Ioc容器中,代码如下:
package com.ahut.hystrix;
import com.ahut.service.HelloService;
import org.springframework.stereotype.Component;
/**
* @author cheng
* @className: HelloServiceHystrix
* @description: 断路器
* @dateTime 2018/4/27 10:25
*/
@Component
public class HelloServiceHystrix implements HelloService {
/**
* @description: 断路器方法
* @author cheng
* @dateTime 2018/4/27 10:25
*/
@Override
public String showPort() {
return "I am so sorry,Server has error";
}
}
启动项目:
- Ahut-Eureka-Server(注册中心)
- Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
- Ahut-Service-Feign(AHUT-SERVICE-FEIGN服务消费者)
访问eureka
访问Ahut-Service-Feign项目 http://localhost:8930/v1/hello:
此时关闭Ahut-Eureka-Client-One项目,再次访问Ahut-Service-Feign项目 http://localhost:8930/v1/hello:
断路器起作用
此时的项目架构
Hystrix Dashboard (断路器:Hystrix 仪表盘)
改造Ahut-Service-Ribbon项目,Ahut-Service-Feign项目的改造和这个一样
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
完整的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<description>service ribbon</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在主程序启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard:
package com.ahut;
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.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author cheng
* @className: ServiceRibbonApplication
* @description: service ribbon
* @dateTime 2018/4/26 14:23
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
/**
* @description: 注入restTemplate, 并且开启负载均衡
* @author cheng
* @dateTime 2018/4/26 14:25
*/
@Bean
@LoadBalanced
public RestTemplate createRestTemplate() {
return new RestTemplate();
}
}
启动项目:
- Ahut-Eureka-Server(注册中心)
- Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
- Ahut-Service-Ribbon(AHUT-SERVICE-RIBBON服务消费者)
访问eureka:
访问hystrix仪表盘 http://localhost:8940/hystrix
修改以下部分:
- 复制浏览器的地址栏,粘贴到输入框,并且加上.stream
- 自定义一个Title
修改完后,点击monitor stream,进入下一个页面
中间访问一下Ahut-Service-Ribbon项目 http://localhost:8940/v1/hello
此时Hystrix仪表盘显示信息如下: