提交 2177cfd0 编写于 作者: Q qinyingjie

fix:sentinel

上级 da1296d8
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kwan.springcloudalibaba</groupId>
<artifactId>springcloudalibaba-server-sentinel</artifactId>
<name>springcloudalibaba-server-sentinel</name>
<description>springcloudalibaba-server-sentinel</description>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
<!-- 客户端需要引入 Transport 模块来与 Sentinel 控制台进行通信-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<!--sentinel启动器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--sentinel持久化存储-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-dependencies</artifactId>-->
<!-- <version>${spring-cloud.version}</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.kwan.springcloudalibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* http://localhost:18088/sentinel/nameInfo
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/17 12:37
*/
@SpringBootApplication
public class SentinelApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelApplication.class, args);
}
}
package com.kwan.springcloudalibaba.common;
import com.kwan.springcloudalibaba.constant.CommonConstant;
import lombok.Data;
import java.io.Serializable;
/**
* 接口返回数据格式
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/8 10:48
*/
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功标志
*/
private boolean success = true;
/**
* 返回处理消息
*/
private String message = "操作成功!";
/**
* 返回代码
*/
private Integer code = 0;
/**
* 返回数据对象 data
*/
private T result;
/**
* 时间戳
*/
private long timestamp = System.currentTimeMillis();
public Result() {
}
public Result<T> success(String message) {
this.message = message;
this.code = CommonConstant.SC_OK_200;
this.success = true;
return this;
}
public Result<T> good(T t) {
this.setResult(t);
this.code = CommonConstant.SC_OK_200;
this.success = true;
return this;
}
public Result<T> good() {
this.code = CommonConstant.SC_OK_200;
this.success = true;
this.setMessage("成功");
return this;
}
public Result<T> fail(String msg) {
this.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
this.setMessage(msg);
this.setSuccess(false);
return this;
}
public static Result<Object> ok() {
Result<Object> r = new Result<>();
r.setSuccess(true);
r.setCode(CommonConstant.SC_OK_200);
r.setMessage("成功");
return r;
}
public static Result<Object> ok(String msg) {
Result<Object> r = new Result<>();
r.setSuccess(true);
r.setCode(CommonConstant.SC_OK_200);
r.setMessage(msg);
return r;
}
public static Result<Object> ok(Object data) {
Result<Object> r = new Result<Object>();
r.setSuccess(true);
r.setCode(CommonConstant.SC_OK_200);
r.setResult(data);
return r;
}
public static Result<Object> error(String msg) {
return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
}
public static Result<Object> error(int code, String msg) {
Result<Object> r = new Result<Object>();
r.setCode(code);
r.setMessage(msg);
r.setSuccess(false);
return r;
}
public Result<T> error500(String message) {
this.message = message;
this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
this.success = false;
return this;
}
/**
* 无权限访问返回结果
*/
public static Result<Object> noauth(String msg) {
return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
}
}
\ No newline at end of file
package com.kwan.springcloudalibaba.constant;
public class CommonConstant {
/**
* 成功
*/
public static final Integer SC_OK_200 = 200;
/**
* 服务器错误
*/
public static final Integer SC_INTERNAL_SERVER_ERROR_500 = 500;
/**
* 未认证
*/
public static final int SC_JEECG_NO_AUTHZ = 401;
}
\ No newline at end of file
package com.kwan.springcloudalibaba.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* http://localhost:18088/sentinel/nameInfo
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/2 17:13
*/
@RestController
@RequestMapping("/sentinel")
public class SentinelController {
@Value("${user.name}")
private String nameInfo;
/**
* 获取配置的变量
*/
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
return Result.ok(nameInfo);
}
/**
* http://localhost:18088/sentinel/world
*
* @return
*/
@GetMapping("/world")
@SentinelResource(value = "helloWorld", blockHandler = "helloBlock")
public String helloWorld() {
return "Hello world";
}
public String helloBlock(BlockException e) {
return "你已被流控";
}
}
package com.kwan.springcloudalibaba.controller;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.ArrayList;
import java.util.List;
/**
* 流量控制代码实现-侵入式
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/2 16:58
*/
public class TestController {
public static void main(String[] args) {
initFlowRules();
while (true) {
Entry entry = null;
try {
entry = SphU.entry("HelloWorld");
/*您的业务逻辑 - 开始*/
System.out.println("hello world");
/*您的业务逻辑 - 结束*/
} catch (BlockException e1) {
/*流控逻辑处理 - 开始*/
System.out.println("block!");
/*流控逻辑处理 - 结束*/
} finally {
if (entry != null) {
entry.exit();
}
}
}
}
/**
* 资源 HelloWorld 每秒最多只能通过 20 个请求。
*/
private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set limit QPS to 20.
rule.setCount(1);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
\ No newline at end of file
#端口号
server:
port: 18088
#断电打开
management:
endpoint:
health:
show-details: always
endpoints:
jmx:
exposure:
include: '*'
web:
exposure:
include: '*'
#spring配置
spring:
application:
name: sentinel-demo
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: http://120.79.36.53:8848 #服务注册地址
config:
server-addr: http://120.79.36.53:8848 #配置中心地址
file-extension: yaml #文件类型
group: DEV_GROUP #组别
namespace: e750dcd5-657b-489b-8d15-b7b71aa3e984 #命名空间
refresh-enabled: true #默认自动刷新
sentinel:
transport:
dashboard: 120.79.36.53:8181
package com.kwan.springcloudalibaba;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SentinelApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册