1515
1616import javax .annotation .PostConstruct ;
1717import javax .servlet .http .HttpServletRequest ;
18+ import java .time .LocalDateTime ;
1819import java .util .concurrent .Executors ;
1920import java .util .concurrent .ScheduledExecutorService ;
2021import 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