Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringCloudAlibaba
提交
ead9e181
S
SpringCloudAlibaba
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringCloudAlibaba
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringCloudAlibaba
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ead9e181
编写于
2月 15, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:整理
上级
beab06ac
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
65 deletion
+60
-65
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java
...an/springcloudalibaba/controller/FlowLimitController.java
+7
-3
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/LocalController.java
...m/kwan/springcloudalibaba/controller/LocalController.java
+52
-0
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
...wan/springcloudalibaba/controller/SentinelController.java
+1
-62
未找到文件。
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java
浏览文件 @
ead9e181
...
...
@@ -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
(
"/test
DD
"
)
@GetMapping
(
"/test
E
"
)
public
String
testDD
()
{
try
{
TimeUnit
.
SECONDS
.
sleep
(
1
);
TimeUnit
.
SECONDS
.
sleep
(
1
00
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
return
"------test
D
"
;
return
"------test
E
"
;
}
}
\ No newline at end of file
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/LocalController.java
0 → 100644
浏览文件 @
ead9e181
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
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
浏览文件 @
ead9e181
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录