斷路器
斷路器是什麼?
之前做的,有資料服務和視圖服務,視圖服務要通路資料服務,如果資料服務挂掉的話,那麼肯定得報500,會出現一個錯誤頁面,這個讓使用者看到不合适啊,得找個東西給他糊弄一下,賣個萌、耍個賤什麼的,不行直接給他個愛的魔力轉圈圈畫面。
當然,這個不是主要的。
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以互相調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證其高可用,單個服務通常會叢集部署。由于網絡原因或者自身的原因,服務并不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet容器的線程資源會被消耗完畢,導緻服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的“雪崩”效應。
為了解決這個問題,業界提出了斷路器模型。

較底層的服務如果出現故障,會導緻連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystric 是5秒20次) 斷路器将會被打開。
斷路打開後,可用避免連鎖故障,fallback方法可以直接傳回一個固定值。
改造項目
還是改造 product-view-service-feign.
pom.xml
添加spring-cloud-starter-netflix-hystrix 以支援斷路器。
<?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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>edu.hpu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product-view-service-feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <!--對feign方式的支援-->
</dependency>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <!--用于通路路徑:/actuator/bus-refresh-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId> <!--用于支援RabbitMQ-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
用戶端
ProductClientFeign,用戶端的注解中注解中加上fallback的指定類,表示如果通路的 PRODUCT-DATA-SERVICE 不可用的話,就調用 ProductClientFeignHystrix 來進行回報資訊。
package edu.hpu.springcloud.client;
import edu.hpu.springcloud.pojo.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(value = "PRODUCT-DATA-SERVICE",fallback = ProductClientFeignHystrix.class)
public interface ProductClientFeign {
@GetMapping("/products")
public List<Product> listProducts();
}
ProductClientFeignHystrix類
ProductClientFeignHystrix類實作ProductClientFeign接口。
package edu.hpu.springcloud.client;
import edu.hpu.springcloud.pojo.Product;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class ProductClientFeignHystrix implements ProductClientFeign{
@Override
public List<Product> listProducts() {
List<Product> result=new ArrayList<>();
result.add(new Product(0,"呵呵,你還想白服務",0));
return result;
}
}
配置
application.yml,在配置中啟動斷路器。
spring:
application:
name: product-view-service-feign
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
content-type: text/html
mode: HTML5
zipkin:
base-url: http://localhost:9411
feign.hystrix.enabled: true
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
啟動通路
依次啟動注冊中心、配置伺服器、視圖微服務,不用啟動資料微服務。
通路位址:
http://localhost:8012/products
參考:
【1】、
http://how2j.cn/k/springcloud/springcloud-hystrix/2042.html#nowhere【2】、
https://www.fangzhipeng.com/springcloud/2018/08/04/sc-f4-hystrix.html