fix:整理

上级 beab06ac
......@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* 限流测试
* http://localhost:18088/sentinel/nameInfo
*
* @author : qinyingjie
......@@ -19,6 +20,9 @@ import java.util.concurrent.TimeUnit;
public class FlowLimitController {
/**
* http://127.0.0.1:8086/flowLimit/testA
*/
@GetMapping("/testA")
public String testA() {
System.out.println("------ testA ------");
......@@ -58,13 +62,13 @@ public class FlowLimitController {
}
@SentinelResource
@GetMapping("/testDD")
@GetMapping("/testE")
public String testDD() {
try {
TimeUnit.SECONDS.sleep(1);
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "------testD";
return "------testE";
}
}
\ 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.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 org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class LocalController {
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
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;
......@@ -16,10 +11,6 @@ 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
*
......@@ -30,7 +21,6 @@ import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/sentinel")
public class SentinelController {
@Autowired
private FeignUserService feignUserService;
......@@ -48,7 +38,6 @@ public class SentinelController {
return Result.ok(feignUserService.selectOne(id));
}
/**
* 获取配置的变量
* http://localhost:8086/sentinel/nameInfo
......@@ -58,18 +47,6 @@ public class SentinelController {
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
*
......@@ -90,42 +67,4 @@ public class SentinelController {
public String helloBlock(BlockException e) {
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);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册