文章目錄
-
- 一: 什麼是SpringCloud
- 1.1 為什麼要使用熔斷器(雪崩效應)
- 1.2 什麼是Hystrix
- 二:熔斷器的簡單示例:
- 三:測試熔斷器:
一: 什麼是SpringCloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
翻譯如下:
Spring Cloud為開發人員提供了快速建構分布式系統中的一些常見模式的工具(例如配置管理、服務發現、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、上司層選舉、分布式會話、叢集狀态)。分布式系統的協調導緻了鍋爐闆模式,使用Spring Cloud開發人員可以快速建立實作這些模式的服務和應用程式。它們在任何分布式環境中都能很好地工作,包括開發人員自己的筆記本電腦、裸機資料中心和雲計算等托管平台。
SpringCloud主要架構:
- 服務發現——Netflix Eureka
- 服務調用——Netflix Feign
- 熔斷器——Netflix Hystrix
- 服務網關——Netflix Zuul
- 分布式配置——Spring Cloud Config
- 消息總線 —— Spring Cloud Bus
1.1 為什麼要使用熔斷器(雪崩效應)
在微服務架構中通常會有多個服務層調用,基礎服務的故障可能會導緻級聯故障,進而造成整個系統不可用的情況,這種現象被稱為服務雪崩效應。
服務雪崩效應是一種因“服務提供者”的不可用導緻“服務消費者”的不可用,并将不可用逐漸放大的過程。
如果下圖所示:A作為服務提供者,B為A的服務消費者,C和D是B的服務消費者。A不可用引起了B的不可用,并将不可用像滾雪球一樣放大到C和D時,雪崩效應就形成了。

使用
Hystrix
熔斷器可以避免雪崩效應
1.2 什麼是Hystrix
在分布式環境中,許多服務依賴項中的一些必然會失敗。Hystrix是一個庫,通過添加延遲容忍和容錯邏輯,幫助你控制這些分布式服務之間的互動。Hystrix通過隔離服務之間的通路點、停止級聯失敗和提供回退選項來實作這一點,所有這些都可以提高系統的整體彈性。
下面是熔斷器的機制:如下
二:熔斷器的簡單示例:
1、pom`導入依賴(在Feign中已經包含Hystrix了)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在pin_teacher子產品的
application.yml
中,開啟Hystrix:
feign:
#熔斷器配置
hystrix:
#開啟熔斷器
enabled: true
完整如下:
server:
port: 8001
spring:
application:
name: pin-teacher
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ml?characterEncoding=UTF8&serverTimezone=UTC
username: root
password: 123456
profiles:
active: dev
feign:
#熔斷器配置
hystrix:
#開啟熔斷器
enabled: true
mybatis:
mapper-locations: classpath:mapper/**/*.xml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3、在pin_teacher子產品中,com.pin_teacher.feign包下建立impl包,建立熔斷器類,實作TeacherFeign 接口(點選檢視TeacherFeign 接口)
注意:TeacherFeign的實作類上必須加上@Component,否則會出現以下錯誤:
No fallback instance of type class com.**.impl.TeacherFeignImpl found for feign client pin-user
import com.ml.pin_teacher.feign.TeacherFeign;
import com.ml.pin_teacher.model.Student;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* 熔斷器類
*/
@Component
public class TeacherFeignImpl implements TeacherFeign {
@Override
public List<Student> findAll() {
System.out.println("teacher調用了熔斷器。。。");
List<Student> students = new ArrayList<>();
Student student = new Student();
student.setId(22);
student.setAge(18);
student.setName("我是熔斷器");
students.add(student);
return students;
}
}
4、修改
pin_teacher
子產品中的
TeacherFeign
接口
import com.ml.pin_teacher.feign.impl.TeacherFeignImpl;
import com.ml.pin_teacher.model.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
//fallback = TeacherFeignImpl.class 一旦熔斷器打開,則執行該實作類
@FeignClient(value = "pin-user",fallback = TeacherFeignImpl.class)
public interface TeacherFeign {
@RequestMapping("student")
List<Student> findAll();
}
三:測試熔斷器:
1.啟動所有程式,檢視eureka注冊中心:
2、分别調用
A. 通過
pin_user
服務直接調用查詢所有學生接口:如下圖
B. 通過
pin_teacher
服務直接調用查詢所有學生接口:如下圖
此時我們把pin_user服務停掉,看下調用情況:
C. 通過
pin_user
服務直接調用查詢所有學生接口:如下圖
D. 通過
pin_teacher
服務直接調用查詢所有學生接口:如下圖
pin_teacher
服務也列印了teacher調用了熔斷器。。。
開心一刻
老鼠沒女朋友特别郁悶,終于一隻蝙蝠答應嫁給他,老鼠十分高興。
别人笑他沒眼光,老鼠:你們懂什麼,她好歹是個空姐。
如果覺得不錯,幫忙點個贊,您的點贊将是我的動力!
上一篇:Feign的使用
下一篇:Hystrix-Dashboard的使用