fix:熔断

上级 0e0aa7fd
......@@ -39,5 +39,6 @@ public class SentinelConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelConsumerApplication.class, args);
System.out.println("-------------------------------sentinel消费端启动成功-------------------------------");
}
}
package com.kwan.springcloudalibaba.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* http://localhost:18088/sentinel/nameInfo
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/2 17:13
*/
@RestController
@RequestMapping("/flowLimit")
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
System.out.println("------ testA ------");
return "------testA";
}
@GetMapping("/testB")
public String testB() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------ testB ------");
return "------testB";
}
@GetMapping("/testC")
public String testC() {
try {
int a = 1 / 0;
System.out.println("------ testC ------");
} catch (Exception e) {
e.printStackTrace();
}
return "------testC";
}
@GetMapping("/testD")
public String testD() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "------testD";
}
@SentinelResource
@GetMapping("/testDD")
public String testDD() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "------testD";
}
}
\ No newline at end of file
package com.kwan.springcloudalibaba.controller;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
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 com.kwan.springcloudalibaba.api.FeignUserService;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -11,6 +16,10 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* http://localhost:18088/sentinel/nameInfo
*
......@@ -44,14 +53,23 @@ public class SentinelController {
* 获取配置的变量
* http://localhost:8086/sentinel/nameInfo
*/
// @SentinelResource(value = "SentinelExceptionHandler",
// blockHandlerClass = SentinelExceptionHandler.class,
// blockHandler = "handleRRException")
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
return Result.ok();
}
@GetMapping("/testB")
public String testB() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------ testB ------");
return "------testB";
}
/**
* http://localhost:8086/sentinel/world
*
......@@ -73,5 +91,41 @@ public class SentinelController {
return "你已被流控";
}
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);
}
}
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册