Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringCloudAlibaba
提交
45b0927a
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看板
提交
45b0927a
编写于
2月 13, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:熔断
上级
0e0aa7fd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
128 addition
and
58 deletion
+128
-58
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/SentinelConsumerApplication.java
.../kwan/springcloudalibaba/SentinelConsumerApplication.java
+1
-0
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java
...an/springcloudalibaba/controller/FlowLimitController.java
+70
-0
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
...wan/springcloudalibaba/controller/SentinelController.java
+57
-3
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java
...om/kwan/springcloudalibaba/controller/TestController.java
+0
-55
未找到文件。
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/SentinelConsumerApplication.java
浏览文件 @
45b0927a
...
@@ -39,5 +39,6 @@ public class SentinelConsumerApplication {
...
@@ -39,5 +39,6 @@ public class SentinelConsumerApplication {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
SentinelConsumerApplication
.
class
,
args
);
SpringApplication
.
run
(
SentinelConsumerApplication
.
class
,
args
);
System
.
out
.
println
(
"-------------------------------sentinel消费端启动成功-------------------------------"
);
}
}
}
}
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java
0 → 100644
浏览文件 @
45b0927a
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
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
浏览文件 @
45b0927a
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
;
...
@@ -11,6 +16,10 @@ import org.springframework.web.bind.annotation.PathVariable;
...
@@ -11,6 +16,10 @@ 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
*
*
...
@@ -44,14 +53,23 @@ public class SentinelController {
...
@@ -44,14 +53,23 @@ public class SentinelController {
* 获取配置的变量
* 获取配置的变量
* http://localhost:8086/sentinel/nameInfo
* http://localhost:8086/sentinel/nameInfo
*/
*/
// @SentinelResource(value = "SentinelExceptionHandler",
// blockHandlerClass = SentinelExceptionHandler.class,
// blockHandler = "handleRRException")
@GetMapping
(
value
=
"/nameInfo"
,
produces
=
MediaType
.
APPLICATION_PROBLEM_JSON_VALUE
)
@GetMapping
(
value
=
"/nameInfo"
,
produces
=
MediaType
.
APPLICATION_PROBLEM_JSON_VALUE
)
public
Result
nameInfo
()
{
public
Result
nameInfo
()
{
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
*
*
...
@@ -73,5 +91,41 @@ public class SentinelController {
...
@@ -73,5 +91,41 @@ public class SentinelController {
return
"你已被流控"
;
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
);
}
}
}
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java
已删除
100644 → 0
浏览文件 @
0e0aa7fd
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录