提交 ead9e181 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:整理

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