Skip to content

Commit d406a50

Browse files
committed
限流降级
1 parent f4adfb8 commit d406a50

File tree

2 files changed

+32
-51
lines changed

2 files changed

+32
-51
lines changed

mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public String params(HttpServletRequest request,@PathVariable("hot") String hot)
8383

8484
@GetMapping("/isolate")
8585
public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException {
86-
long sleepTime = 20 + RANDOM.nextInt(5);
86+
long sleepTime = 500 + RANDOM.nextInt(5);
8787
silentSleep(sleepTime);
8888
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime;
8989
}

mse-simple-demo/gateway/src/main/java/com/alibabacloud/mse/demo/DemoController.java

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import javax.annotation.PostConstruct;
1717
import javax.servlet.http.HttpServletRequest;
18+
import java.time.LocalDateTime;
1819
import java.util.concurrent.Executors;
1920
import java.util.concurrent.ScheduledExecutorService;
2021
import java.util.concurrent.ThreadFactory;
@@ -36,9 +37,8 @@ public class DemoController {
3637
@Value("${background.color:white}")
3738
private String backgroundColor;
3839

39-
private static final ScheduledExecutorService FLOW_EXECUTOR = Executors.newScheduledThreadPool(6,
40+
private static final ScheduledExecutorService FLOW_EXECUTOR = Executors.newScheduledThreadPool(16,
4041
new ThreadFactory() {
41-
4242
@Override
4343
public Thread newThread(Runnable r) {
4444
Thread thread = new Thread(r);
@@ -106,12 +106,10 @@ public void run() {
106106
}
107107
}, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS);
108108

109-
110-
109+
// 限流降级的流量发起
111110
FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() {
112111
@Override
113112
public void run() {
114-
115113
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
116114
HttpGet httpGet = new HttpGet("http://localhost:20000/A/flow");
117115
httpClient.execute(httpGet);
@@ -127,34 +125,6 @@ public void run() {
127125
} catch (Exception ignore) {
128126
}
129127

130-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
131-
HttpGet httpGet = new HttpGet("http://localhost:20000/A/isolate");
132-
httpClient.execute(httpGet);
133-
134-
} catch (Exception ignore) {
135-
}
136-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
137-
HttpGet httpGet = new HttpGet("http://localhost:20000/A/isolate");
138-
httpGet.addHeader("x-mse-tag", "gray");
139-
httpClient.execute(httpGet);
140-
141-
} catch (Exception ignore) {
142-
}
143-
144-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
145-
HttpGet httpGet = new HttpGet("http://localhost:20000/A/params/hot");
146-
httpClient.execute(httpGet);
147-
148-
} catch (Exception ignore) {
149-
}
150-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
151-
HttpGet httpGet = new HttpGet("http://localhost:20000/A/params/hot");
152-
httpGet.addHeader("x-mse-tag", "gray");
153-
httpClient.execute(httpGet);
154-
155-
} catch (Exception ignore) {
156-
}
157-
158128
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
159129
HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-flow");
160130
httpClient.execute(httpGet);
@@ -183,20 +153,6 @@ public void run() {
183153
} catch (Exception ignore) {
184154
}
185155

186-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
187-
HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-params/hot");
188-
httpClient.execute(httpGet);
189-
190-
} catch (Exception ignore) {
191-
}
192-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
193-
HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-params/hot");
194-
httpGet.addHeader("x-mse-tag", "gray");
195-
httpClient.execute(httpGet);
196-
197-
} catch (Exception ignore) {
198-
}
199-
200156
//
201157
// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
202158
// HttpGet httpGet = new HttpGet("http://localhost:20000/B/flow-c");
@@ -239,12 +195,37 @@ public void run() {
239195
//
240196
// } catch (Exception ignore) {
241197
// }
242-
243-
244198
}
245199
}, 100, 1000000 / qps, TimeUnit.MICROSECONDS);
246200

247-
201+
// region 热点限流
202+
FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() {
203+
@Override
204+
public void run() {
205+
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
206+
HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-params/hot");
207+
httpClient.execute(httpGet);
208+
} catch (Exception ignore) {
209+
}
210+
}
211+
}, 100, 1000000 / qps, TimeUnit.MICROSECONDS);
212+
// endregion 热点限流
213+
214+
// region 隔离规则
215+
for (int i = 0; i < 8; i++) {
216+
int finalI = i;
217+
FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() {
218+
@Override
219+
public void run() {
220+
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
221+
HttpGet httpGet = new HttpGet("http://localhost:20000/A/isolate?i_id=" + finalI);
222+
httpClient.execute(httpGet);
223+
} catch (Exception ignore) {
224+
}
225+
}
226+
}, 100, 1000000 / qps, TimeUnit.MICROSECONDS);
227+
}
228+
// endregion 隔离规则
248229
}
249230

250231

0 commit comments

Comments
 (0)