這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)SpringBoot中怎么利用Sentinel實現(xiàn)接口流量控制,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都網(wǎng)站設(shè)計、做網(wǎng)站的開發(fā),更需要了解用戶,從用戶角度來建設(shè)網(wǎng)站,獲得較好的用戶體驗。創(chuàng)新互聯(lián)多年互聯(lián)網(wǎng)經(jīng)驗,見的多,溝通容易、能幫助客戶提出的運營建議。作為成都一家網(wǎng)絡(luò)公司,打造的就是網(wǎng)站建設(shè)產(chǎn)品直銷的概念。選擇創(chuàng)新互聯(lián),不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來訪用戶感受到浩方產(chǎn)品的價值服務(wù)。
首先我們來創(chuàng)建一個測試項目,這里初始化項目的url建議大家填寫阿里云的地址,會有驚喜?
http://start.aliyun.com

接下來就是常規(guī)操作,一路next,在下圖的位置稍微注意一下

說明:
同大家以前創(chuàng)建項目一樣,只需要在這里勾選Sentinel就可以啦?
項目創(chuàng)建好以后,我們發(fā)現(xiàn)pom文件中引入了下面的依賴

有的小伙伴看網(wǎng)上博客,也會有下面的方式,指定版本號
<!-- sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.1.0.RELEASE</version> </dependency>
如果你使用我推薦的阿里云的Url,會發(fā)現(xiàn)Sentinel的版本號都定義父工程,Cloud的各個組件的兼容性就不要大家操心了
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>打開項目配置文件,會發(fā)現(xiàn)它已經(jīng)為我們自動加好了配置,真的超級方便?
server.port=8083 # 應(yīng)用名稱 spring.application.name=springcloud-sentinel # Sentinel 控制臺地址 spring.cloud.sentinel.transport.dashboard=localhost:8080 # 取消Sentinel控制臺懶加載 # 默認情況下 Sentinel 會在客戶端首次調(diào)用的時候進行初始化,開始向控制臺發(fā)送心跳包 # 配置 sentinel.eager=true 時,取消Sentinel控制臺懶加載功能 spring.cloud.sentinel.eager=true # 如果有多套網(wǎng)絡(luò),又無法正確獲取本機IP,則需要使用下面的參數(shù)設(shè)置當前機器可被外部訪問的IP地址,供admin控制臺使用 # spring.cloud.sentinel.transport.client-ip=# sentinel 配置 spring.application.name=frms spring.cloud.sentinel.transport.dashboard=localhost:8080 spring.cloud.sentinel.transport.heartbeat-interval-ms=500
官網(wǎng)提供的demo
package com.milo.sentinel; import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.ArrayList; import java.util.List; /** * 項目入口 * @author Milo Lee * @date 2021-3-20 19:07 * */ @SpringBootApplication public class SentinelApplication { public static void main(String[] args) { SpringApplication.run(SentinelApplication.class, args); // 配置規(guī)則. initFlowRules(); while (true) { // 1.5.0 版本開始可以直接利用 try-with-resources 特性 try (Entry entry = SphU.entry("HelloWorld")) { // 被保護的邏輯 Thread.sleep(300); System.out.println("hello world"); } catch (BlockException | InterruptedException ex) { // 處理被流控的邏輯 System.out.println("blocked!"); } } } private static void initFlowRules(){ List<FlowRule> rules = new ArrayList<>(); FlowRule rule = new FlowRule(); rule.setResource("HelloWorld"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // Set limit QPS to 20. rule.setCount(20); rules.add(rule); FlowRuleManager.loadRules(rules); } }注解式定義
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } } @Service public class TestService { @SentinelResource(value = "sayHello") public String sayHello(String name) { return "Hello, " + name; } } @RestController public class TestController { @Autowired private TestService service; @GetMapping(value = "/hello/{name}") public String apiHello(@PathVariable String name) { return service.sayHello(name); } }@SentinelResource 注解用來標識資源是否被限流、降級。上述例子上該注解的屬性 sayHello 表示資源名。
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar

控制臺的操作我們用編程式定義的例子來演示,大家啟動我們的服務(wù)

我們會發(fā)現(xiàn)除了sentinel-dashboard之外,多了一個milolee-sentinel,這個就是我們的服務(wù),它的名稱其實對應(yīng)我們配置文件定義的應(yīng)用名稱:
# 應(yīng)用名稱 spring.application.name=milolee-sentinel
點擊機器列表,這這里如果能發(fā)現(xiàn)你的機器,那就是成功上線了



給我們的資源HelloWorld配置流控規(guī)則,它的QPS(每秒請求數(shù))為1,如圖:

通過查看實時監(jiān)控,我們發(fā)現(xiàn)已經(jīng)生效

給我們的資源HelloWorld添加一個降級規(guī)則配置,如果QPS大于1,且平均響應(yīng)時間大于20ms,則接口下來接口在2秒鐘無法訪問,之后自動恢復(fù)。

目前這些規(guī)則僅在內(nèi)存態(tài)生效,應(yīng)用重啟之后,該規(guī)則會丟失。后續(xù)文章我們會繼續(xù)學(xué)習(xí)動態(tài)規(guī)則

上述就是小編為大家分享的SpringBoot中怎么利用Sentinel實現(xiàn)接口流量控制了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:SpringBoot中怎么利用Sentinel實現(xiàn)接口流量控制
文章源于:http://chinadenli.net/article0/goioio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站導(dǎo)航、小程序開發(fā)、商城網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)