SpringCloud之Sentinel高可用流量框架簡單搭建

思齊大叔發表於2020-07-10

該微服務名稱cloudalibaba-sentinel-service8401,原始碼地址

1.sentinel jar包下載並啟動

 

啟動:cmd視窗啟動命令,java -jar jar包名

訪問:http://localhost:8080/

2 由於該微服務需要註冊進nacos,前提安裝nacos.

3 微服務程式碼

預覽專案結構

3.1 pom檔案

<?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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.budi.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-sentinel-service8401</artifactId>


    <dependencies>
        <!--springcloud alibaba nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- spring-cloud-starter-alibaba-sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--alibaba sentinel-datasource-nacos 用於資料持久化-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

        <!--api 通用包-->
        <dependency>
            <groupId>com.budi.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

3.2yam檔案

server:
  port: 8401
spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      #配置sentinel dashboard地址
      transport:

        dashboard: localhost:8080
        #預設8719埠 假如被佔用,依次掃描+1埠,直到找到未被佔用埠
        port: 8719
        datasource:
                ds1:
                  nacos:
                    server-addr: localhost:8848
                    dataId: ${spring.application.name}
                    groupId: DEFAULT_GROUP
                    data-type: json
                    rule-type: flow


management:
  endpoints:
    web:
      exposure:
        include: '*'

3.3 自定義限流兜底檔案

package com.budi.springcloud.myhandler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.budi.springcloud.entities.CommonResult;

public class CustomerBlockHandler {
    public CommonResult handelException(BlockException exception)
    {
        return new CommonResult(444,"客戶自定義,global--e1");
    }
    public CommonResult handelException2(BlockException exception)
    {
        return new CommonResult(444,"客戶自定義,global--e2");
    }
}

3.4controller檔案

package com.budi.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testa")
    public String testA(){
        return "+++=testA";
    }
    @GetMapping("/testb")
    public String testB()
    {
        return "-----testB";
    }
    @GetMapping("/testd")
    public String testD()
    {
        log.info("測試testd RT");
        try {
            Thread.sleep(1000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "testd";
    }
    @GetMapping("/teste")
    public String testE()
    {
        int a=10/0;
        return "teste測試異常數";
    }

    /**
     * 熱點規則
     * @param p1
     * @param p2
     * @return
     */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2)
    {
        return "test Hot key";
    }
    public String deal_testHotKey(String p1, String p2, BlockException exception)
    {
        return "deal_testHotKey";
    }
}
package com.budi.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.budi.springcloud.entities.CommonResult;
import com.budi.springcloud.myhandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handelException")
    public CommonResult byResource()
    {
        return new CommonResult(202,"按照資源名稱限流測試ok");
    }
    public CommonResult handelException(BlockException exception)
    {
        return new CommonResult(500,exception.getClass().getCanonicalName()+"服務不可用");
    }
    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl",blockHandlerClass = CustomerBlockHandler.class
    ,blockHandler = "handelException")
    public CommonResult byUrl()
    {
        return new CommonResult(200,"按照url限流ok");
    }

    //customerBlockHandler
    @GetMapping("/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler")
    public CommonResult customerBlockHandler()
    {
        return new CommonResult(200,"按客戶自定義");
    }
}

3.5 main主啟動檔案

package com.budi.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
    public static void main(String[] args) {
        SpringApplication.run(SentinelServiceMain8401.class,args);
    }
}

4 測試

因為sentinel是懶載入模式,所以先傳送請求,在重新整理sentinel管理頁面,服務就出來了,然後可以新增各種規則測試

相關文章