SpringCloud?集成Sentinel的實戰(zhàn)教程
一、Seninel簡介


二、Sentinel和Hystrix的區(qū)別

三、sentinel可視化界面安裝

下載對應(yīng)版本的sentinel的jar包,通過終端命令:
java -jar jar包名
啟動

訪問對應(yīng)路徑:控制臺如下:

四、在springcloudalibaba中整合sentinel
(1)添加依賴
<!--sentinel啟動器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>(2)配置yml
server:
port: 8002
spring:
application:
name: WXL-DEV-SERVICE-2
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8080(3)啟動服務(wù),再訪問服務(wù)后,觀察控制臺:因為訪問接口以后才會注冊到sentinel當中。

五、流控規(guī)則
(1)實時監(jiān)控,可用于查看接口訪問情況

(2)簇點鏈路,可以對對應(yīng)的資源流控降級

可以設(shè)置閥值來流控:
(3)QPS流控

可以看到當每秒超過2次時被流控:

流控文字可自定義:
@GetMapping("/world")
@SentinelResource(value = "helloWorld",blockHandlerClass = TestController.class,blockHandler = "helloBlock")
public String helloWorld() {
return "Hello world";
}
public static String helloBlock(BlockException e){
return "你已被流控";
}value將該方法定義為sentinel的資源,blockHandlerClass指明流控處理的類,blockHandler是流控時調(diào)用的方法。
這里需要注意處理異常的方法必須是靜態(tài)方法添加static, 并需要添加sentinel的異常參數(shù)BlockException。

統(tǒng)一異??刂铺幚?/p>
上面通過注解實現(xiàn)流控靈活性更高,對于需要統(tǒng)一管理的流控處理,我們可以通過統(tǒng)一異常處理來實現(xiàn)??梢宰远x處理不同類型的限流。
只需實現(xiàn)對應(yīng)接口即可,案例代碼如下:
package com.dragonwu.exception;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Component
public class MyBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
System.out.println("BlockExceptioonHandler ++++++++++++++++++++++++++"+e.getRule());
Map<Integer,String> hashMap=new HashMap<>();
if(e instanceof FlowException){
hashMap.put(100,"接口限流了");
}else if(e instanceof DegradeException){
hashMap.put(101,"服務(wù)降級了");
}else if(e instanceof ParamFlowException){
hashMap.put(102,"熱點參數(shù)限流了");
}else if(e instanceof SystemBlockException){
hashMap.put(103,"觸發(fā)系統(tǒng)保護規(guī)則了");
}else if(e instanceof AuthorityException){
hashMap.put(104,"授權(quán)規(guī)則不通過");
}
//返回json數(shù)據(jù)
httpServletResponse.setStatus(500);
httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
new ObjectMapper().writeValue(httpServletResponse.getWriter(),hashMap);
}
}
(4)線程流控

(5)關(guān)聯(lián)限流

這里的意思是如果/hello/add接口一秒鐘之內(nèi)訪問超過2次,則/hello/query會被限流。
(6)熔斷降級

也要設(shè)置熔斷時長,熔斷時長過完之后會進入半開狀態(tài),即若下一次請求為慢請求則再次熔斷,直到第一次請求不是慢請求才會恢復(fù)正常狀態(tài)。
六、OpenFeign整合Sentinel
(1)導(dǎo)入依賴:
<!--OpenFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>(2)調(diào)用者開發(fā)整合配置:
feign:
sentinel:
enabled: true #開啟openFeign對sentinel的整合(3)添加openFeign調(diào)用接口
package com.wxl.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "WXL-DEV-SERVICE-2", path = "/hello",fallback = ServiceFailFeign.class)
public interface Service1HelloInterface {
@GetMapping("/world")
String helloWorld();
}并且這里添加參數(shù)fallback值為失敗時回調(diào)的實現(xiàn)類。
實現(xiàn)類如下:
package com.wxl.feign;
import org.springframework.stereotype.Component;
@Component
public class ServiceFailFeign implements Service1HelloInterface{
public String helloWorld() {
return "降級了?。。?;
}
}當接口請求失敗時便會調(diào)用失敗類里的該方法。
這里我們?yōu)榱耸褂眯Ч?,在服?wù)生產(chǎn)者的接口里故意寫入報錯代碼:
@GetMapping("/world")
public String helloWorld() {
int i=1/0;
return "Hello world";
}請求該消費者服務(wù)接口:

調(diào)用了失敗回調(diào)方法!
七、規(guī)則持久化
(1)引入依賴
<!--sentinel持久化存儲-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>(2)為nacos添加配置

更多見:
到此這篇關(guān)于SpringCloud 集成Sentinel的文章就介紹到這了,更多相關(guān)SpringCloud 集成Sentinel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在SpringBoot項目中使用Spring Cloud Sentinel實現(xiàn)流量控制
- Spring?Cloud?中使用?Sentinel?實現(xiàn)服務(wù)限流的兩種方式
- Spring?Cloud中Sentinel的兩種限流模式介紹
- springcloud3 Sentinel的搭建及案例操作方法
- Spring?Cloud微服務(wù)架構(gòu)Sentinel數(shù)據(jù)雙向同步
- Spring?Cloud?Alibaba微服務(wù)組件Sentinel實現(xiàn)熔斷限流
- Spring?Cloud?Gateway整合sentinel?實現(xiàn)流控熔斷的問題
- Java之SpringCloudAlibaba Sentinel組件案例講解
- Sentinel 斷路器在Spring Cloud使用詳解
相關(guān)文章
如何基于mybatis框架查詢數(shù)據(jù)庫表數(shù)據(jù)并打印
這篇文章主要介紹了如何基于mybatis框架查詢數(shù)據(jù)庫表數(shù)據(jù)并打印,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
SpringBoot入門實現(xiàn)第一個SpringBoot項目
今天我們一起來完成一個簡單的SpringBoot(Hello World)。就把他作為你的第一個SpringBoot項目。具有一定的參考價值,感興趣的可以了解一下2021-09-09
Java多線程實現(xiàn)簡易微信發(fā)紅包的方法實例
這篇文章主要給大家介紹了關(guān)于Java多線程實現(xiàn)簡易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
Elastic-Job在配置中提供了JobEventConfiguration,支持數(shù)據(jù)庫方式配置,會在數(shù)據(jù)庫中自動創(chuàng)建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG兩張表以及若干索引,來記錄作業(yè)的相關(guān)信息2023-02-02
解析SpringCloud簡介與微服務(wù)架構(gòu)
這篇文章主要介紹了SpringCloud簡介與微服務(wù)架構(gòu),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
解決springboot無法注入JpaRepository的問題
這篇文章主要介紹了解決springboot無法注入JpaRepository的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

