diff --git a/springcloudalibaba-server-sentinel/pom.xml b/springcloudalibaba-server-sentinel/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..e7aecd8878336f05d5d9335d68941a67e47c43b0 --- /dev/null +++ b/springcloudalibaba-server-sentinel/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + com.kwan.springcloudalibaba + springcloudalibaba-server-sentinel + springcloudalibaba-server-sentinel + springcloudalibaba-server-sentinel + 1.0.0 + + 1.8 + UTF-8 + UTF-8 + 2.3.0.RELEASE + 2.2.5.RELEASE + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + org.springframework.boot + spring-boot-starter-actuator + + + org.projectlombok + lombok + + + com.alibaba.csp + sentinel-core + + + + com.alibaba.csp + sentinel-transport-simple-http + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + com.alibaba.csp + sentinel-datasource-nacos + + + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + \ No newline at end of file diff --git a/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/SentinelApplication.java b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/SentinelApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..6b1aebe0332e359c67fb54dd7d3479033515754d --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/SentinelApplication.java @@ -0,0 +1,20 @@ +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); + } +} diff --git a/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/common/Result.java b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/common/Result.java new file mode 100644 index 0000000000000000000000000000000000000000..caab8b7923a1ae21d26d202276ed0cc87320ed01 --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/common/Result.java @@ -0,0 +1,119 @@ +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 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 success(String message) { + this.message = message; + this.code = CommonConstant.SC_OK_200; + this.success = true; + return this; + } + + public Result good(T t) { + this.setResult(t); + this.code = CommonConstant.SC_OK_200; + this.success = true; + return this; + } + + public Result good() { + this.code = CommonConstant.SC_OK_200; + this.success = true; + this.setMessage("成功"); + return this; + } + + public Result fail(String msg) { + this.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500); + this.setMessage(msg); + this.setSuccess(false); + return this; + } + + public static Result ok() { + Result r = new Result<>(); + r.setSuccess(true); + r.setCode(CommonConstant.SC_OK_200); + r.setMessage("成功"); + return r; + } + + public static Result ok(String msg) { + Result r = new Result<>(); + r.setSuccess(true); + r.setCode(CommonConstant.SC_OK_200); + r.setMessage(msg); + return r; + } + + public static Result ok(Object data) { + Result r = new Result(); + r.setSuccess(true); + r.setCode(CommonConstant.SC_OK_200); + r.setResult(data); + return r; + } + + public static Result error(String msg) { + return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg); + } + + public static Result error(int code, String msg) { + Result r = new Result(); + r.setCode(code); + r.setMessage(msg); + r.setSuccess(false); + return r; + } + + public Result error500(String message) { + this.message = message; + this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500; + this.success = false; + return this; + } + + /** + * 无权限访问返回结果 + */ + public static Result noauth(String msg) { + return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg); + } +} \ No newline at end of file diff --git a/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/constant/CommonConstant.java b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/constant/CommonConstant.java new file mode 100644 index 0000000000000000000000000000000000000000..762e49b791c967af74bebfc4077bc2d376ace371 --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/constant/CommonConstant.java @@ -0,0 +1,16 @@ +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 diff --git a/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java new file mode 100644 index 0000000000000000000000000000000000000000..a70350a6655dbf3f52c061a33e5c46ea42a79b84 --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java @@ -0,0 +1,51 @@ +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 "你已被流控"; + } + +} + diff --git a/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java new file mode 100644 index 0000000000000000000000000000000000000000..a6c8824e106c38bfa3f7588971a8d5aa15db7991 --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java @@ -0,0 +1,55 @@ +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 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 diff --git a/springcloudalibaba-server-sentinel/src/main/resources/bootstrap.yml b/springcloudalibaba-server-sentinel/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000000000000000000000000000000000..25b25820383d0b44d8627c45f1ac777ed256a6f6 --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/main/resources/bootstrap.yml @@ -0,0 +1,35 @@ +#端口号 +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 diff --git a/springcloudalibaba-server-sentinel/src/test/java/com/kwan/springcloudalibaba/SentinelApplicationTests.java b/springcloudalibaba-server-sentinel/src/test/java/com/kwan/springcloudalibaba/SentinelApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..bbcae7c50e04c87cce77d692d1b82182d3df71cf --- /dev/null +++ b/springcloudalibaba-server-sentinel/src/test/java/com/kwan/springcloudalibaba/SentinelApplicationTests.java @@ -0,0 +1,13 @@ +package com.kwan.springcloudalibaba; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SentinelApplicationTests { + + @Test + void contextLoads() { + } + +}