SpringCloud之Sentinel高可用流量框架简单搭建

该微服务名称cloudalibaba-sentinel-service8401,源码地址

1.sentinel jar包下载并启动

 

启动:cmd窗口启动命令,java -jar jar包名

访问:http://localhost:8080/

2 由于该微服务需要注册进nacos,前提安装nacos.

3 微服务代码

预览项目结构

SpringCloud之Sentinel高可用流量框架简单搭建_第1张图片

3.1 pom文件



    
        cloud2020
        com.budi.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloudalibaba-sentinel-service8401


    
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        

        
        
            com.budi.springcloud
            cloud-api-commons
            ${project.version}
        


        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            true
            runtime
        
        
            org.projectlombok
            lombok
            true
        
    

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管理页面,服务就出来了,然后可以添加各种规则测试

SpringCloud之Sentinel高可用流量框架简单搭建_第2张图片

你可能感兴趣的:(java,sentinel,阿里,java,spring,cloud,java)