Commit 51f18fd4 by 李根

添加多线程测试模块

parent 5e268211
<?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
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");
}
}
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();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment