華為云速建站可以做英文網(wǎng)站百度怎么做自己的網(wǎng)頁(yè)
目錄
- 一、簡(jiǎn)介
- Sentinel 是什么
- 如何引入Sentinel
- 二、服務(wù)搭建
- 1.安裝Sentinel控制臺(tái)
- 1.1 下載
- 1.2 啟動(dòng)
- 1.3 訪問(wèn)
- 2.改造服務(wù)提供者cloud-provider服務(wù)
- 2.1 引入依賴
- 2.2 添加API
- 2.3 添加配置文件
- 3.改造cloud-consumer-feign服務(wù)
- 3.1 引入依賴
- 3.2 添加Feign接口
- 3.3 添加服務(wù)降級(jí)類(lèi)
- 3.4 改造FeignService
- 3.5 修改配置文件
- 三、運(yùn)行測(cè)試
- 1.啟動(dòng)項(xiàng)目
- 1.1 啟動(dòng)服務(wù)提供者
- 1.2 啟動(dòng)服務(wù)消費(fèi)者
- 2.調(diào)用測(cè)試接口
- 2.1 OpenFeign服務(wù)降級(jí)測(cè)試
- 2.2 Sentinel流控規(guī)則測(cè)試
溫馨提示:全套教程請(qǐng)查看 教程總覽
一、簡(jiǎn)介
Sentinel 是什么
Sentinel是分布式系統(tǒng)的流量防衛(wèi)兵。
隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來(lái)越重要。Sentinel 以流量為切入點(diǎn),從流量控制、流量路由、熔斷降級(jí)、系統(tǒng)自適應(yīng)過(guò)載保護(hù)、熱點(diǎn)流量防護(hù)等多個(gè)維度保護(hù)服務(wù)的穩(wěn)定性。
Sentinel 具有以下特征:
- 豐富的應(yīng)用場(chǎng)景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場(chǎng)景,例如秒殺(即突發(fā)流量控制在系統(tǒng)容量可以承受的范圍)、消息削峰填谷、集群流量控制、實(shí)時(shí)熔斷下游不可用應(yīng)用等。
- 完備的實(shí)時(shí)監(jiān)控:Sentinel 同時(shí)提供實(shí)時(shí)的監(jiān)控功能。我們可以在控制臺(tái)中看到接入應(yīng)用的單臺(tái)機(jī)器秒級(jí)數(shù)據(jù),甚至 500 臺(tái)以下規(guī)模的集群的匯總運(yùn)行情況。
- 廣泛的開(kāi)源生態(tài):Sentinel 提供開(kāi)箱即用的與其它開(kāi)源框架/庫(kù)的整合模塊,例如與 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。我們只需要引入相應(yīng)的依賴并進(jìn)行簡(jiǎn)單的配置即可快速地接入 Sentinel。同時(shí) Sentinel 提供Java/Go/C++ 等多語(yǔ)言的原生實(shí)現(xiàn)。
- 完善的 SPI 擴(kuò)展機(jī)制:Sentinel 提供簡(jiǎn)單易用、完善的 SPI 擴(kuò)展接口。我們可以通過(guò)實(shí)現(xiàn)擴(kuò)展接口來(lái)快速地定制邏輯。例如定制規(guī)則管理、適配動(dòng)態(tài)數(shù)據(jù)源等。
Sentinel 分為兩個(gè)部分:
- 核心庫(kù)(Java 客戶端)不依賴任何框架/庫(kù),能夠運(yùn)行于所有 Java 運(yùn)行時(shí)環(huán)境,同時(shí)對(duì) Dubbo / Spring Cloud 等框架也有較好的支持。
- 控制臺(tái)(Dashboard)基于 Spring Boot 開(kāi)發(fā),打包后可以直接運(yùn)行,不需要額外的 Tomcat 等應(yīng)用容器。
如何引入Sentinel
如果要在我們的項(xiàng)目中引入 Sentinel,使用 group ID 為 com.alibaba.cloud
和 artifact ID 為 spring-cloud-starter-alibaba-sentinel
的 starter。
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
參考文檔 請(qǐng)查看 官網(wǎng)
二、服務(wù)搭建
1.安裝Sentinel控制臺(tái)
1.1 下載
Sentinel提供了多種安裝方式,包括:
- 下載源碼通過(guò)Maven構(gòu)建
- 下載發(fā)行包解壓安裝
這里我們通過(guò)下載 最新穩(wěn)定版本 發(fā)行包的方式安裝。
1.2 啟動(dòng)
注意:啟動(dòng) Sentinel 控制臺(tái)需要 JDK 版本為 1.8 及以上版本。
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
其中 -Dserver.port=8080
用于指定 Sentinel 控制臺(tái)端口為 8080
。
1.3 訪問(wèn)
打開(kāi)瀏覽器訪問(wèn):http://localhost:8080/
默認(rèn)用戶名和密碼都是 sentinel
。
2.改造服務(wù)提供者cloud-provider服務(wù)
有關(guān) cloud-provider
服務(wù)的搭建過(guò)程,請(qǐng)參考之前的章節(jié) 基于Spring Boot 3.x 搭建教程
2.1 引入依賴
pom.xml
內(nèi)容如下:
<?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>org.ash</groupId><artifactId>spring-cloud-alibaba-demo</artifactId><version>${revision}</version></parent><artifactId>cloud-provider</artifactId><description>服務(wù)提供者</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Nacos注冊(cè)中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- Nacos配置中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- bootstrap配置 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><!--alibaba-sentinel--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><!-- 打包插件 --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins><!-- 將源碼中的xml文件打包到j(luò)ar中 --><resources><resource><directory>src/main/java</directory><excludes><exclude>**/*.java</exclude></excludes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource></resources></build>
</project>
2.2 添加API
新建 TestSentinelController.java
,添加測(cè)試API,并且定義sentinel資源
Sentinel 提供了
@SentinelResource
注解用于定義資源,并提供了 AspectJ 的擴(kuò)展用于自動(dòng)定義資源、處理 BlockException 等。
value
:資源名稱(chēng),必需項(xiàng)(不能為空)blockHandler / blockHandlerClass
:blockHandler
對(duì)應(yīng)處理BlockException
的函數(shù)名稱(chēng),可選項(xiàng)。blockHandler
函數(shù)訪問(wèn)范圍需要是public
,返回類(lèi)型需要與原方法相匹配,參數(shù)類(lèi)型需要和原方法相匹配并且最后加一個(gè)額外的參數(shù),類(lèi)型為BlockException
。blockHandler
函數(shù)默認(rèn)需要和原方法在同一個(gè)類(lèi)中。若希望使用其他類(lèi)的函數(shù),則可以指定blockHandlerClass
為對(duì)應(yīng)的類(lèi)的Class
對(duì)象,注意對(duì)應(yīng)的函數(shù)必需為static
函數(shù),否則無(wú)法解析。
內(nèi)容如下:
package org.ash.provider.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestSentinelController {@GetMapping("/provider/sentinel/test/{message}")@SentinelResource(value = "providerSentinelTest", blockHandler = "handlerBlockHandler")public String providerSentinelTest(@PathVariable("message") String message) {return "sentinel測(cè)試:" + message;}public String handlerBlockHandler(@PathVariable("message") String message, BlockException exception) {return "providerSentinelTest服務(wù)不可用," + "觸發(fā)sentinel流控配置規(guī)則"+"\t"+"o(╥﹏╥)o";}
}
2.3 添加配置文件
resources目錄下新建 application.yml
配置文件,內(nèi)容如下:
spring:cloud:sentinel:transport:dashboard: localhost:8080 #配置Sentinel dashboard控制臺(tái)服務(wù)地址port: 8719 #默認(rèn)8719端口,假如被占用會(huì)自動(dòng)從8719開(kāi)始依次+1掃描,直至找到未被占用的端口
3.改造cloud-consumer-feign服務(wù)
有關(guān) cloud-consumer-feign
服務(wù)的搭建過(guò)程,請(qǐng)參考之前的章節(jié) 基于Spring Boot 3.x 搭建教程
3.1 引入依賴
pom.xml
內(nèi)容如下:
<?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>org.ash</groupId><artifactId>spring-cloud-alibaba-demo</artifactId><version>${revision}</version></parent><artifactId>cloud-consumer-feign</artifactId><description>服務(wù)消費(fèi)者-feign</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Nacos注冊(cè)中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- loadbalancer負(fù)載均衡 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><!-- openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><!-- 打包插件 --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins><!-- 將源碼中的xml文件打包到j(luò)ar中 --><resources><resource><directory>src/main/java</directory><excludes><exclude>**/*.java</exclude></excludes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource></resources></build>
</project>
3.2 添加Feign接口
在 FeignService.java
中新增調(diào)用服務(wù)提供者接口
內(nèi)容如下:
package org.ash.consumer.feign.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "cloud-provider")
public interface FeignService {@GetMapping("/provider/test/{message}")public String getProviderTest(@PathVariable("message") String message);@GetMapping("/provider/sentinel/test/{message}")public String providerSentinelTest(@PathVariable("message") String message);
}
3.3 添加服務(wù)降級(jí)類(lèi)
新建 FeignServiceFallback.java
服務(wù)降級(jí)類(lèi),實(shí)現(xiàn) FeignService.java
并實(shí)現(xiàn)方法
內(nèi)容如下:
package org.ash.consumer.feign.service.fallback;import org.ash.consumer.feign.service.FeignService;
import org.springframework.stereotype.Component;@Component
public class FeignServiceFallback implements FeignService {@Overridepublic String getProviderTest(String message) {return "對(duì)方服務(wù)不可用,開(kāi)始服務(wù)降級(jí)處理";}@Overridepublic String providerSentinelTest(String message) {return "對(duì)方服務(wù)不可用,開(kāi)始服務(wù)降級(jí)處理";}
}
3.4 改造FeignService
改造 FeignService.java
的 @FeignClient
注解,添加 fallback
屬性
內(nèi)容如下:
package org.ash.consumer.feign.service;import org.ash.consumer.feign.service.fallback.FeignServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "cloud-provider", fallback = FeignServiceFallback.class)
public interface FeignService {@GetMapping("/provider/test/{message}")public String getProviderTest(@PathVariable("message") String message);@GetMapping("/provider/sentinel/test/{message}")public String providerSentinelTest(@PathVariable("message") String message);
}
3.5 修改配置文件
默認(rèn) sentinel 對(duì) feign 的支持是關(guān)閉的,我們需要在配置文件中打開(kāi)。
內(nèi)容如下:
server:port: 9003spring:application:# 服務(wù)名稱(chēng)name: cloud-consumer-feigncloud:nacos:# nacos注冊(cè)中心discovery:# 服務(wù)ip:portserver-addr: 127.0.0.1:8848openfeign:client:config:default:#連接超時(shí)時(shí)間connectTimeout: 5000#讀取超時(shí)時(shí)間readTimeout: 5000# 開(kāi)啟feign集成sentinel服務(wù)降級(jí)
feign:sentinel:enabled: truemanagement:endpoints:web:exposure:include: '*'
三、運(yùn)行測(cè)試
1.啟動(dòng)項(xiàng)目
1.1 啟動(dòng)服務(wù)提供者
1.2 啟動(dòng)服務(wù)消費(fèi)者
2.調(diào)用測(cè)試接口
2.1 OpenFeign服務(wù)降級(jí)測(cè)試
打開(kāi)瀏覽器調(diào)用我們之前寫(xiě)的測(cè)試接口 http://localhost:9003/consumer/feign/sentinel/test/sentinel
,成功返回消息
這時(shí)我們停止服務(wù)提供者 cloud-provider
服務(wù)
然后再次通過(guò)瀏覽器調(diào)用測(cè)試接口 http://localhost:9003/consumer/feign/sentinel/test/sentinel
,看到返回服務(wù)降級(jí)的信息,表示服務(wù)降級(jí)成功
2.2 Sentinel流控規(guī)則測(cè)試
重新啟動(dòng) cloud-provider
服務(wù)提供者
再次通過(guò)瀏覽器調(diào)用測(cè)試接口 http://localhost:9003/consumer/feign/sentinel/test/sentinel
,然后打開(kāi)Sentinel 控制臺(tái),刷新頁(yè)面,找到測(cè)試接口添加流控規(guī)則
- 流控規(guī)則
資源名
對(duì)應(yīng)代碼@SentinelResource
的value
值。- 流控規(guī)則
單機(jī)閾值
為1
表示1秒鐘內(nèi)請(qǐng)求1次就是正常,若超過(guò)1次,就會(huì)失敗,執(zhí)行服務(wù)提供者的blockHandler
通過(guò)瀏覽器快速調(diào)用測(cè)試接口 http://localhost:9003/consumer/feign/sentinel/test/sentinel
,查看是否返回流控配置規(guī)則消息,如果返回流控配置規(guī)則消息,則表示 Sentinel 流控規(guī)則配置成功。
至此,OpenFeign集成Sentinel實(shí)現(xiàn)服務(wù)降級(jí)成功!!!
溫馨提示:全套教程請(qǐng)查看 教程總覽