<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          輕松兩步,在 SpringBoot 服務(wù)上實(shí)現(xiàn)接口限流

          共 3606字,需瀏覽 8分鐘

           ·

          2020-11-11 04:15

          程序員的成長(zhǎng)之路
          互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享?
          關(guān)注


          閱讀本文大概需要 4.2 分鐘。

          來(lái)自:https://urlify.cn/YjY322

          Sentinel是阿里巴巴開源的限流器熔斷器,并且?guī)в锌梢暬僮鹘缑妗?/section>

          在日常開發(fā)中,限流功能時(shí)常被使用,用于對(duì)某些接口進(jìn)行限流熔斷,譬如限制單位時(shí)間內(nèi)接口訪問(wèn)次數(shù);或者按照某種規(guī)則進(jìn)行限流,如限制ip的單位時(shí)間訪問(wèn)次數(shù)等。

          之前我們已經(jīng)講過(guò)接口限流的工具類ratelimter可以實(shí)現(xiàn)令牌桶的限流,很明顯sentinel的功能更為全面和完善。

          來(lái)看一下sentinel的簡(jiǎn)介:https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel


          Sentinel 介紹


          隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來(lái)越重要。Sentinel 以流量為切入點(diǎn),從流量控制、熔斷降級(jí)、系統(tǒng)負(fù)載保護(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)行情況。

          • 廣泛的開源生態(tài):Sentinel 提供開箱即用的與其它開源框架/庫(kù)的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應(yīng)的依賴并進(jìn)行簡(jiǎn)單的配置即可快速地接入 Sentinel。

          • 完善的 SPI 擴(kuò)展點(diǎn):Sentinel 提供簡(jiǎn)單易用、完善的 SPI 擴(kuò)展點(diǎn)。您可以通過(guò)實(shí)現(xiàn)擴(kuò)展點(diǎn),快速的定制邏輯。例如定制規(guī)則管理、適配數(shù)據(jù)源等。

          來(lái)簡(jiǎn)單使用一下Sentinel。

          Sentinel包括服務(wù)端和客戶端,服務(wù)端有可視化界面,客戶端需引入jar后即可和服務(wù)端通信并完成限流功能。

          啟動(dòng)服務(wù)端的jar


          https://github.com/alibaba/Sentinel/releases 在這個(gè)地址,下載release的jar,然后啟動(dòng)即可。

          這個(gè)jar是個(gè)標(biāo)準(zhǔn)的Springboot應(yīng)用,可以通過(guò)
          java -jar sentinel-dashboard-1.6.0.jar來(lái)啟動(dòng),這樣就是默認(rèn)的設(shè)置,啟動(dòng)在8080端口。也可以加上一些自定義配置來(lái)啟動(dòng)

          java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
          具體配置的解釋,可以到GitHub上看一下文檔。

          這里我們直接使用默認(rèn)java -jar sentinel-dashboard-1.6.0.jar來(lái)啟動(dòng),之后訪問(wèn)localhost:8080。可以看到界面:


          輸入賬號(hào)密碼sentinel后進(jìn)入主界面


          此時(shí)因?yàn)槲覀儾](méi)有啟動(dòng)客戶端,所以界面是空的。

          啟動(dòng)客戶端


          新建一個(gè)Springboot項(xiàng)目,pom如下:
          <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.0modelVersion>    <parent>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-parentartifactId>        <version>2.0.5.RELEASEversion>        <relativePath/>     parent>        <groupId>com.maimeng.baobanqgroupId>    <artifactId>baobanserverartifactId>    <version>0.0.1-SNAPSHOTversion>    <packaging>jarpackaging>    <name>baobanservername>    <description>Demo project for Spring Bootdescription>     <properties>        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>        <java.version>1.8java.version>        <spring-cloud.version>Finchley.SR1spring-cloud.version>    properties>     <dependencies>               <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-webartifactId>        dependency>                       <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>        dependency>                       <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-testartifactId>            <scope>testscope>        dependency>    dependencies>     <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloudgroupId>                <artifactId>spring-cloud-dependenciesartifactId>                <version>${spring-cloud.version}version>                <type>pomtype>                <scope>importscope>            dependency>             <dependency>                <groupId>org.springframework.cloudgroupId>                <artifactId>spring-cloud-alibaba-dependenciesartifactId>                <version>0.2.2.RELEASEversion>                <type>pomtype>                <scope>importscope>            dependency>        dependencies>    dependencyManagement>     <build>        <plugins>            <plugin>                <groupId>org.springframework.bootgroupId>                <artifactId>spring-boot-maven-pluginartifactId>            plugin>        plugins>    build> project>
          需要注意引用的SpringCloud-alibaba的版本是0.2.2,當(dāng)前的最新版,如果是Springboot2.x的項(xiàng)目,需要引0.2.x的。Springboot1.x的引0.1.x的。

          Sentinel的客戶端依賴也很簡(jiǎn)單,spring-cloud-starter-alibaba-sentinel加這一個(gè)引用即可。

          之后在application.yml里添加server的地址配置:
          spring: application:   name: baobanserver cloud:  sentinel:    transport:      dashboard: localhost:8080    #eager: true
          另外由于8080端口已被占用,自行設(shè)置一個(gè)端口,如8888。

          做完這些,新建一個(gè)controller,

          @RestControllerpublic class TestController { @GetMapping(value = "/hello") public String hello() { return "Hello Sentinel"; }}
          就是一個(gè)普通的controller接口。

          之后啟動(dòng)該項(xiàng)目。啟動(dòng)后回到server的控制臺(tái)界面


          發(fā)現(xiàn)并沒(méi)有什么變化。然后我們調(diào)用一下hello接口。之后再次刷新server控制臺(tái)。


          界面已經(jīng)出現(xiàn)了我們的項(xiàng)目,并且有一堆規(guī)則。


          因?yàn)镾entinel采用延遲加載,只有在主動(dòng)發(fā)起一次請(qǐng)求后,才會(huì)被攔截并發(fā)送給服務(wù)端。如果想關(guān)閉這個(gè)延遲,就在上面的yml里把eager的注釋放掉。

          然后在簇點(diǎn)鏈路里hello接口的流控那里設(shè)置限流規(guī)則,將單機(jī)閾值設(shè)為1.就代表一秒內(nèi)最多只能通過(guò)1次請(qǐng)求到達(dá)該hello接口。


          之后再次連續(xù)訪問(wèn)hello接口。


          發(fā)現(xiàn)已經(jīng)被攔截了,限流已經(jīng)生效。

          這樣就完成了一次簡(jiǎn)單的限流操作,并且能看到各接口的QPS的統(tǒng)計(jì)。
          推薦閱讀:

          推薦一個(gè)碼云 1w+ 星標(biāo)的 Spring Cloud 微服務(wù)項(xiàng)目

          RabbitMQ 線上事故!慌的一批,腦袋一片空白

          5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「2048」,即可免費(fèi)獲取!!

          微信掃描二維碼,關(guān)注我的公眾號(hào)

          朕已閱?

          瀏覽 63
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  好逼天天有| 欧美成人网站视频 | 爱情岛论坛www成人网站 | 中,日韩欧美中文字幕 | 国产精品久久久久久久久久久久久久久久久 |