alexa排名全球前50網(wǎng)站排名優(yōu)化方案
這里寫目錄標(biāo)題
- 系列文章目錄
- 背景
- 一、Hystrix是什么
- 服務(wù)雪崩
- 服務(wù)容錯的相關(guān)概念
- 熔斷器
- 降級
- 超時控制
- 限流
- 二、會什么要有Hystrix
- 三、如何使用Hystrix進行熔斷處理整體
- 項目代碼
- 服務(wù)提供者
- pom文件
- yml配置文件
- 啟動類
- controller
- 服務(wù)消費者
- pom文件
- yml配置文件
- 啟動類
- feign
- hystrix
- controller
- 服務(wù)啟動
- 總結(jié)
系列文章目錄
【Spring Cloud一】微服務(wù)基本知識
【Spring Cloud 三】Eureka服務(wù)注冊與服務(wù)發(fā)現(xiàn)
【Spring Cloud 四】Ribbon負載均衡
【Spring Cloud 五】OpenFeign服務(wù)調(diào)用
背景
目前開發(fā)的項目其微服務(wù)之間的熔斷處理方式使用的就是Hystrix的方式,為了更加的體會到它代碼的便捷和高效,所以博主對Hystrix進行了再次學(xué)習(xí)和實踐,加強對Hystrix的整體理解。
一、Hystrix是什么
Hystrix是Netflix開源的一款用于處理分布式系統(tǒng)故障容錯和延遲容錯的工具。它提供了熔斷器的功能,能夠阻止分布式系統(tǒng)中出現(xiàn)級聯(lián)故障。
服務(wù)雪崩
如下發(fā)的圖所示:
- 用戶去請求A服務(wù),A服務(wù)web服務(wù)器會分配一個線程支持用戶的訪問
A發(fā)現(xiàn)需要完成用戶的操作血藥去調(diào)用B服務(wù)。 - A去請求B服務(wù),B的web服務(wù)器也會分配一個線程支持A的訪問
B發(fā)現(xiàn)需要完成A的操作需要去調(diào)用C。 - B去調(diào)用C但是C宕機了,B并不知道,還是一如既往的去調(diào)用C
- 導(dǎo)致A和B的線程都沒有回收
但是此時又有大量請求進入A服務(wù)或者B服務(wù),當(dāng)將web容器的線程資源會被消耗完,服務(wù)就會癱瘓。服務(wù)A和服務(wù)B也就癱瘓了
這種由于一個服務(wù)出現(xiàn)故障,導(dǎo)致其余服務(wù)跟著產(chǎn)生故障,最后導(dǎo)致整個系統(tǒng)出現(xiàn)嚴(yán)重故障的現(xiàn)象就是服務(wù)雪崩。
服務(wù)容錯的相關(guān)概念
服務(wù)容錯是確保系統(tǒng)在出現(xiàn)故障或異常情況繼續(xù)保持可用性的一系列技術(shù)和方法。
熔斷器
熔斷器是一種防止故障蔓延的模式,在一個系統(tǒng)中,當(dāng)其中一個服務(wù)發(fā)送故障的時候,熔斷器會中斷對該服務(wù)的請求,并返回預(yù)先設(shè)定的降級措施,從而防止對整個系統(tǒng)造成影響。
降級
降級是一種在服務(wù)發(fā)生故障時提供的備選響應(yīng)的策略,確保用戶在服務(wù)故障時依然能夠得到響應(yīng)。
超時控制
一個服務(wù)的響應(yīng)時間可能因網(wǎng)絡(luò)、資源等問題而增加。超時控制是指在發(fā)起請求后設(shè)置一個合理的超時時間,如果服務(wù)在規(guī)定時間內(nèi)沒有響應(yīng),就人物請求超時,并根據(jù)預(yù)定策略進行處理,如降級響應(yīng)。
限流
限流是一種用于控制系統(tǒng)訪問速率的策略,防止過多的請求同時涌入系統(tǒng),導(dǎo)致系統(tǒng)過載而崩潰。通過限制請求的速率,可以保證系統(tǒng)在承受能力范圍內(nèi)穩(wěn)定運行。
二、會什么要有Hystrix
為什么要有Hystrix,Hystrix解決了什么問題?
Hystrix在分布式系統(tǒng)中解決了故障容錯、延遲容錯、請求合并與緩存、實時監(jiān)控、異常處理等問題,保證系統(tǒng)在出現(xiàn)故障是一眼能夠保持可用性,防止級聯(lián)故障的發(fā)生。
三、如何使用Hystrix進行熔斷處理整體
整個系統(tǒng)中有三個服務(wù),Eureka服務(wù),一個服務(wù)提供者,一個服務(wù)消費者
如何搭建Eurka服務(wù)可以訪問這篇博客【Spring Cloud 三】Eureka服務(wù)注冊與服務(wù)發(fā)現(xiàn)
補充:Hystrix是通過隔離服務(wù)的訪問點阻止級聯(lián)故障(熔斷器Circuit Breaker),并提供了故障的解決方案(服務(wù)降級FallBack),從而提高整個分布式系統(tǒng)的高可用。
項目代碼
服務(wù)提供者
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wangwei</groupId><artifactId>rent-car-service</artifactId><version>0.0.1-SNAPSHOT</version><name>rent-car-service</name><description>rent-car-service</description><properties><java.version>8</java.version><spring-cloud.version>Hoxton.SR12</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-netflix-eureka-client</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>
yml配置文件
server:port: 8080spring:application:name: rent-car-service
eureka:client:service-url: defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #設(shè)置為fasle 不往eureka-server注冊fetch-registry: true #應(yīng)用是否拉取服務(wù)列表到本地registry-fetch-interval-seconds: 10 #為了緩解服務(wù)列表的臟讀問題,時間越短臟讀越少 性能相應(yīng)的消耗回答instance: #實例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主機名稱或者服務(wù)ipprefer-ip-address: true #以ip的形式顯示具體的服務(wù)信息lease-renewal-interval-in-seconds: 10 #服務(wù)實例的續(xù)約時間間隔
啟動類
@SpringBootApplication
@EnableEurekaClient
public class RentCarServiceApplication {public static void main(String[] args) {SpringApplication.run(RentCarServiceApplication.class, args);}}
controller
@RestController
public class RentCarController {@GetMapping("rent")public String rent(){return "租車成功";}
}
服務(wù)消費者
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wangwei</groupId><artifactId>customer-service</artifactId><version>0.0.1-SNAPSHOT</version><name>customer-service</name><description>customer-service</description><properties><java.version>8</java.version><spring-cloud.version>Hoxton.SR12</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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></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>
yml配置文件
server:port: 8081spring:application:name: customer-service
eureka:client:service-url:defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #設(shè)置為fasle 不往eureka-server注冊fetch-registry: true #應(yīng)用是否拉取服務(wù)列表到本地registry-fetch-interval-seconds: 10 #為了緩解服務(wù)列表的臟讀問題,時間越短臟讀越少 性能相應(yīng)的消耗回答instance: #實例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主機名稱或者服務(wù)ipprefer-ip-address: true #以ip的形式顯示具體的服務(wù)信息lease-renewal-interval-in-seconds: 10 #服務(wù)實例的續(xù)約時間間隔
feign:hystrix:enabled: true # 開啟熔斷
啟動類
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker //開啟斷路器
public class CustomerServiceApplication {public static void main(String[] args) {SpringApplication.run(CustomerServiceApplication.class, args);}}
feign
/*** 這里需要制定熔斷類*/
@FeignClient(value = "rent-car-service",fallback = CustomerRentFeignHystrix.class)
public interface CustomerRentFeign {@GetMapping("rent")String rent();}
hystrix
@Component
public class CustomerRentFeignHystrix implements CustomerRentFeign {/*** 這個方法是備選方案* @return*/@Overridepublic String rent() {return "備選方案";}
}
controller
@RestController
public class CustomerController {@Autowiredprivate CustomerRentFeign customerRentFeign;@GetMapping("customerRent")public String CustomerRent(){System.out.println("客戶來租車了");String rent = customerRentFeign.rent();return rent;}}
服務(wù)啟動
先啟動Eureka服務(wù)端再啟動服務(wù)提供者最后啟動服務(wù)消費者
當(dāng)我們將服務(wù)提供者進行下線之后,服務(wù)消費者再調(diào)用服務(wù)提供者的時候,先進行了熔斷,再執(zhí)行了降級處理(執(zhí)行備選方案)。
總結(jié)
- 以上就是Hystrix的基本使用,當(dāng)服務(wù)出現(xiàn)故障時,開啟熔斷器執(zhí)行降級方案。
- 后序博主會手動實現(xiàn)熔斷器來加強對熔斷器的理解。
- 由于Netflix已經(jīng)宣布停止維護Hystrix,推薦使用Resilience4j或Sentinel等其他替代方案來處理容錯和熔斷問題。