前言
完整知识点:spring cloud netflix 系列技术栈
在spring cloud netflix中使用ribbon和feign作为服务与服务之间的通信机制,这种通信方式是同步的HTTP通信方式,在大访问量的情况下会发生阻塞,直观体现就是标签页一直在等待响应,这就会有一个响应时间。而熔断器的作用就是:在发现服务不可用时,立即做出反应,避免请求一直处于等待响应的状态,从而避免出现响应时间,减轻服务器压力,避免出现雪崩。
- 没有熔断器,响应处于等待状态
- 使用熔断器,发现服务不可用,立即做出反应,避免等待(阻塞)
Hystrix熔断器
通过在前言中的理解,我们知道了熔断器的作用是:当服务与服务之间的通信出现问题时,熔断器会立即做出响应,避免发生阻塞。
Hystrix + Ribbon
- Hystrix pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 完整pom文件
<?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>
<!-- 关联统一的依赖管理 -->
<parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- 指向统一的依赖管理的位置 -->
<relativePath>../spring-cloud-netflix-dependencies/pom.xml</relativePath>
</parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-web-admin-ribbon</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 配置程序的入口类 -->
<mainClass>com.baiyang.spring.cloud.netflix.web.admin.ribbon.AdminWebRibbonApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 在application类中使用@EnableHystrix开启熔断器
package com.baiyang.spring.cloud.netflix.web.admin.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class AdminWebRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(AdminWebRibbonApplication.class,args);
}
}
- 使用 @HystrixCommand(fallbackMethod = “”)注解为方法配置熔断机制
当被请求的方法出现问题,不能提供服务时, @HystrixCommand(fallbackMethod = “方法名称”) 就会触发fallbackMethod 指向的方法。
package com.baiyang.spring.cloud.netflix.web.admin.ribbon.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 AdminWebRibbonService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloError")
public String hello(String message){
return restTemplate.getForObject("http://spring-cloud-netflix-server-admin/hello?message="+message,String.class);
}
public String helloError(String message){
return String.format("您输入的消息是 %s , 但是请求失败。",message);
}
}