Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
springcloud-sentinel
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
senthink-project-origin
springcloud-sentinel
Commits
51f18fd4
Commit
51f18fd4
authored
Apr 02, 2020
by
李根
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加多线程测试模块
parent
5e268211
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
163 additions
and
0 deletions
+163
-0
Test/pom.xml
+29
-0
Test/src/main/java/com/demo/senthinel/Main.java
+80
-0
Test/src/main/java/com/demo/senthinel/Test.java
+54
-0
No files found.
Test/pom.xml
0 → 100644
View file @
51f18fd4
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.demo.sentinel
</groupId>
<artifactId>
Test
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-core
</artifactId>
<version>
1.7.1
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ning/async-http-client -->
<dependency>
<groupId>
com.ning
</groupId>
<artifactId>
async-http-client
</artifactId>
<version>
1.9.40
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
Test/src/main/java/com/demo/senthinel/Main.java
0 → 100644
View file @
51f18fd4
package
com
.
demo
.
senthinel
;
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
java.util.ArrayList
;
import
java.util.List
;
/**
* @author ligen
* @title: Main
* @projectName sentinel
* @description:
* @date 2020/3/1911:00
*/
public
class
Main
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
// 配置规则.
initFlowRules
();
while
(
true
)
{
// 1.5.0 版本开始可以直接利用 try-with-resources 特性,自动 exit entry
try
(
Entry
entry
=
SphU
.
entry
(
"HelloWorld"
))
{
// 被保护的逻辑
// System.out.println("hello world");
say
();
}
catch
(
BlockException
ex
)
{
// 处理被流控的逻辑
Thread
.
sleep
(
10000
);
System
.
out
.
println
(
"blocked!"
);
}
// 逻辑 2
try
(
Entry
entry
=
SphU
.
entry
(
"HelloWorld2"
))
{
// 被保护的逻辑
// System.out.println("hello world");
say2
();
}
catch
(
BlockException
ex
)
{
// 处理被流控的逻辑
Thread
.
sleep
(
10000
);
System
.
out
.
println
(
"blocked!"
);
}
}
}
private
static
void
initFlowRules
()
{
List
<
FlowRule
>
rules
=
new
ArrayList
<
FlowRule
>();
// 规则1
FlowRule
rule
=
new
FlowRule
();
rule
.
setResource
(
"HelloWorld"
);
rule
.
setGrade
(
RuleConstant
.
FLOW_GRADE_QPS
);
// Set limit QPS to 20.
rule
.
setCount
(
20
);
rules
.
add
(
rule
);
// 规则2,3,.....
FlowRuleManager
.
loadRules
(
rules
);
}
@SentinelResource
(
"HelloWorld"
)
public
static
void
say
()
{
System
.
out
.
println
(
"hello world"
);
}
@SentinelResource
(
"HelloWorld"
)
public
static
void
say2
()
{
System
.
out
.
println
(
"hello world----2"
);
}
}
Test/src/main/java/com/demo/senthinel/Test.java
0 → 100644
View file @
51f18fd4
package
com
.
demo
.
senthinel
;
import
com.ning.http.client.AsyncHttpClient
;
import
com.ning.http.client.ListenableFuture
;
import
com.ning.http.client.Response
;
import
java.util.Random
;
/**
* @author ligen
* @title: Test
* @projectName sentinel
* @description:
* @date 2020/3/2411:48
*/
public
class
Test
{
public
static
void
main
(
String
[]
args
)
{
// 测试限流
test
(
25
,
"/test2"
);
// 测试降级
test
(
5
,
"/test3"
);
}
/**
* @ desc : 测试限流
* @ params
* @ return
* @ date 2020/3/31
* @ author ligen
*/
public
static
void
test
(
int
threadCnt
,
String
resource
)
{
for
(
int
i1
=
0
;
i1
<
threadCnt
;
i1
++)
{
AsyncHttpClient
asyncHttpClient
=
new
AsyncHttpClient
();
Random
random
=
new
Random
();
Thread
thread
=
new
Thread
(
()
->
{
while
(
true
)
{
ListenableFuture
<
Response
>
execute
=
asyncHttpClient
.
prepareGet
(
"http://localhost:19966"
+
resource
).
execute
();
System
.
out
.
println
(
"resources:"
+
resource
+
"-----"
+
Thread
.
currentThread
().
getName
());
try
{
Thread
.
sleep
(
random
.
nextInt
(
1000
));
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
);
thread
.
start
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment