SpringCloud之Sentinel高可用流量框架簡單搭建
該微服務名稱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管理頁面,服務就出來了,然後可以新增各種規則測試
相關文章
- SpringCloud微服務實戰——搭建企業級開發框架(十四):整合Sentinel高可用流量管理框架【限流】SpringGCCloud微服務框架
- SpringCloud微服務實戰——搭建企業級開發框架(十五):整合Sentinel高可用流量管理框架【熔斷降級】SpringGCCloud微服務框架
- SpringCloud微服務實戰——搭建企業級開發框架(十六):整合Sentinel高可用流量管理框架【自定義返回訊息】SpringGCCloud微服務框架
- Redis高可用 SentinelRedis
- Redis 哨兵高可用(Sentinel)Redis
- springcloud-高可用部署SpringGCCloud
- (13) SpringCloud-使用Eureka叢集搭建實現高可用SpringGCCloud
- (15) SpringCloud-使用Eureka叢集搭建實現高可用SpringGCCloud
- 分散式流量控制框架sentinel初探分散式框架
- springcloud~SentinelSpringGCCloud
- Gin 框架的簡單搭建框架
- SpringCloud註冊中心高可用搭建SpringGCCloud
- 【SpringCloud】微服務Eureka高可用配置SpringGCCloud微服務
- Sentinel簡單使用(1)
- SpringCloud-Alibaba-SentinelSpringGCCloud
- SpringCloud——Sentinel入門SpringGCCloud
- SpringCloud學習系列之一 ----- 搭建一個高可用的註冊中心(Eureka)SpringGCCloud
- SpringCloud使用Sentinel,Sentinel持久化,Sentinel使用nacos持久化SpringGCCloud持久化
- 流量治理神器-Sentinel 究竟是怎麼做到讓業務方接入簡單?
- 高可用之SkybilityHA簡單介紹-行雲管家
- zookeeper 高可用叢集搭建
- MongoDB高可用叢集搭建MongoDB
- 搭建 Kubernetes 高可用叢集
- mysql高可用衡搭建(Keepalived)MySql
- SpringCloud使用Sentinel 代替 HystrixSpringGCCloud
- SpringCloud11 -- Alibaba SentinelSpringGCCloud
- 搭建高併發、高可用的系統
- [Rust]使用Rocket框架搭建簡單Web服務Rust框架Web
- .Net Core Web Api 框架搭建簡單步驟WebAPI框架
- mysql高可用架構MHA搭建MySql架構
- 搭建高可用k8sK8S
- SpringCloud-Alibaba-Sentinel(1)初探SpringGCCloud
- springcloud alibaba sentinel降級 @SentinelResourceSpringGCCloud
- SpringCloud實踐(三) 高可用的Eureka註冊中心SpringGCCloud
- 理解vue ssr原理,自己搭建簡單的ssr框架Vue框架
- Redis sentinel搭建Redis
- Hadoop框架:HDFS高可用環境配置Hadoop框架
- 使用 Docker Compose 本地部署基於 Sentinel 的高可用 Redis 叢集DockerRedis