springBoot整合Sentinel實現降級限流熔斷


由於hystrix的停止更新,以及阿里Sentinel在歷年雙十一的貢獻。項目中使用了Sentinel,今天我們來講講Sentinel的入門教程,本文使用1.6.3版本進行講解

本文通過Sentinel_dashBoard進行講解,當然不引入監控看板也能實現限流熔斷降級功能,但是監控看板能夠直觀的看到請求的QPS,成功率等等,同時可以實時的進行降級限流策略的修改與新建。

1.sentinel_dashboard的引入

  https://github.com/alibaba/Sentinel/releases,下載sentinel-dashboard-1.6.3.jar

由於dashboard是springboot的項目,在CMD模式下使用命令

  java -Dserver.port=8080

    -Dcsp.sentinel.dashboard.server=localhost:8080

    -Dproject.name=sentinel-dashboard

    -jar sentinel-dashboard-1.6.3.jar

進行控制看板服務的啟動。

  其中,-Dserver.port=8080 代表看板項目的端口號,-Dcsp.sentinel.dashboard.server=localhost:8080代表本看板服務將會注冊到自己的看板上,-Dproject.name=sentinel-dashboard代表本看板服務的項目名稱。

訪問localhost:8080;輸入用戶名,密碼,均是sentinel,如果要自定義用戶名和密碼,在啟動命令加上-Dsentinel.dashboard.auth.username=sentinel,
-Dsentinel.dashboard.auth.password=123456即可。

我們可以看到控制台自身的服務已經注冊到了控制台上。

2. 接下來,引入需要接入sentinel功能的項目。

3.maven依賴

<?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>

    <groupId>spring.sentinel</groupId>
    <artifactId>spring-sentinel</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.6.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.6.3</version>
        </dependency>
    </dependencies>
</project>

4. 編寫啟動類

package sentile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SentileApp {

    public static void main(String[] args) {
        SpringApplication.run(SentileApp.class, args);
    }
}

5. 編寫配置類

package sentile.config;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SentileConfig {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

    @PostConstruct
    private void initRules() throws Exception {
        FlowRule rule1 = new FlowRule();
        rule1.setResource("test.hello");
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule1.setCount(1);   // 每秒調用最大次數為 1 次

        List<FlowRule> rules = new ArrayList<>();
        rules.add(rule1);

        // 將控制規則載入到 Sentinel
        com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager.loadRules(rules);
    }
}

6. 編寫測試入口

package sentile.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class TestController {

    @GetMapping("hello")
    @SentinelResource(value = "test.hello", fallback = "helloError")
    public String hello(String name){
        return "hello,"+name;
    }

    public String helloError(String name, Throwable e){
        return "error,"+name;
    }

}

7. 添加啟動參數,啟動服務

-Dproject.name=app1
-Dcsp.sentinel.dashboard.server=localhost:8080
-Dserver.port=9090

(由於8080已經被控制台服務占據,我們修改客戶端服務端口為9090)

由於注冊是懶加載的,所以我們先訪問一下服務,再去看控制台看板

服務正常訪問

通過同

 

通過觀察控制台,我們發現我們的項目名為app1的項目已經注冊到了控制台

接下來,我們就可以在卒簇族鏈路進行規則設置啦

以上的方法適合非springboot,springcloud的項目實現,如果項目本身是springboot,springcloud項目,可以直接引入

spring-cloud-starter-alibaba-sentinel

添加配置文件application.yml:
spring:
application:
name: baobanserver
cloud:
sentinel:
transport:
dashboard: localhost:9999
#eager: true


往后甚至不用寫@SentinelResource注解,直接按照正常的springboot寫法即可實現。然后再Sentinel的控制台進行鏈路規則設置即可!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM