SpringCloud之Sentinel高可用流量框架简单搭建
程序员文章站
2022-05-07 18:54:21
该微服务名称cloudalibaba-sentinel-service8401,源码地址1.sentinel jar包下载并启动启动:cmd窗口启动命令,java -jar jar包名访问:http://localhost:8080/2 由于该微服务需要注册进nacos,前提安装nacos.3 微服务代码预览项目结构3.1 pom文件
该微服务名称cloudalibaba-sentinel-service8401,源码地址
1.sentinel jar包下载并启动
启动:cmd窗口启动命令,java -jar jar包名
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管理页面,服务就出来了,然后可以添加各种规则测试
本文地址:https://blog.csdn.net/u014377123/article/details/107243195
上一篇: 定制型网站具有什么特点?有哪些优势?
下一篇: 28.C++- 单例类模板(详解)