Skip to content

Commit 4ffdfd7

Browse files
author
karl
committed
流量防护demo
1 parent b2fb8c1 commit 4ffdfd7

File tree

15 files changed

+655
-3
lines changed

15 files changed

+655
-3
lines changed

mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/AController.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@
1515
import org.springframework.beans.factory.annotation.Qualifier;
1616
import org.springframework.beans.factory.annotation.Value;
1717
import org.springframework.cloud.commons.util.InetUtils;
18+
import org.springframework.http.HttpStatus;
19+
import org.springframework.http.ResponseEntity;
1820
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1921
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PathVariable;
2023
import org.springframework.web.bind.annotation.RestController;
2124
import org.springframework.web.client.RestTemplate;
2225

2326
import javax.annotation.PostConstruct;
2427
import javax.servlet.http.HttpServletRequest;
2528
import java.util.Enumeration;
2629
import java.util.List;
30+
import java.util.Random;
2731
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.TimeUnit;
2833

2934
@Api(value = "/", tags = {"入口应用"})
3035
@RestController
@@ -55,6 +60,7 @@ class AController {
5560

5661
private String currentZone;
5762

63+
5864
@PostConstruct
5965
private void init() {
6066
try {
@@ -96,6 +102,44 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
96102
"[config=" + configValue + "]" + " -> " + result;
97103
}
98104

105+
@ApiOperation(value = "测试防护规则" , tags = {"流量防护"})
106+
@GetMapping("/flow")
107+
public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException {
108+
109+
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/flow", String.class);
110+
HttpStatus status = responseEntity.getStatusCode();
111+
String result = responseEntity.getBody() + status.value();
112+
113+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
114+
"[config=" + configValue + "]" + " -> " + result;
115+
}
116+
117+
118+
@ApiOperation(value = "测试热点规则", tags = {"流量防护"})
119+
@GetMapping("/params/{hot}")
120+
public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
121+
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/params/"+hot, String.class);
122+
123+
HttpStatus status = responseEntity.getStatusCode();
124+
String result = responseEntity.getBody() + status.value();
125+
126+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
127+
"[config=" + configValue + "]" + " -> " + result;
128+
}
129+
130+
@ApiOperation(value = "测试隔离规则", tags = { "流量防护"})
131+
@GetMapping("/isolate")
132+
public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException {
133+
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/isolate", String.class);
134+
135+
HttpStatus status = responseEntity.getStatusCode();
136+
String result = responseEntity.getBody() + status.value();
137+
138+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
139+
"[config=" + configValue + "]" + " -> " + result;
140+
}
141+
142+
99143
@GetMapping("/spring_boot")
100144
public String spring_boot(HttpServletRequest request) {
101145
String result = restTemplate.getForObject("http://sc-b:20002/spring_boot", String.class);
@@ -138,11 +182,65 @@ public String dubbo(HttpServletRequest request) {
138182
helloServiceB.hello("A");
139183
}
140184

185+
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
186+
@GetMapping("/dubbo-flow")
187+
public String dubbo_flow(HttpServletRequest request) {
188+
StringBuilder headerSb = new StringBuilder();
189+
Enumeration<String> enumeration = request.getHeaderNames();
190+
while (enumeration.hasMoreElements()) {
191+
String headerName = enumeration.nextElement();
192+
Enumeration<String> val = request.getHeaders(headerName);
193+
while (val.hasMoreElements()) {
194+
String headerVal = val.nextElement();
195+
headerSb.append(headerName + ":" + headerVal + ",");
196+
}
197+
}
198+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
199+
helloServiceB.hello("A");
200+
}
201+
202+
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
203+
@GetMapping("/dubbo-params/{hot}")
204+
public String dubbo_params(HttpServletRequest request, @PathVariable("hot") String hot) {
205+
StringBuilder headerSb = new StringBuilder();
206+
Enumeration<String> enumeration = request.getHeaderNames();
207+
while (enumeration.hasMoreElements()) {
208+
String headerName = enumeration.nextElement();
209+
Enumeration<String> val = request.getHeaders(headerName);
210+
while (val.hasMoreElements()) {
211+
String headerVal = val.nextElement();
212+
headerSb.append(headerName + ":" + headerVal + ",");
213+
}
214+
}
215+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
216+
helloServiceB.hello(hot);
217+
}
218+
219+
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
220+
@GetMapping("/dubbo-isolate")
221+
public String dubbo_isolate(HttpServletRequest request) {
222+
StringBuilder headerSb = new StringBuilder();
223+
Enumeration<String> enumeration = request.getHeaderNames();
224+
while (enumeration.hasMoreElements()) {
225+
String headerName = enumeration.nextElement();
226+
Enumeration<String> val = request.getHeaders(headerName);
227+
while (val.hasMoreElements()) {
228+
String headerVal = val.nextElement();
229+
headerSb.append(headerName + ":" + headerVal + ",");
230+
}
231+
}
232+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
233+
helloServiceB.hello("isolate");
234+
}
235+
236+
141237
@GetMapping("swagger-demo")
142238
@ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"})
143239
public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name,
144240
@ApiParam(name = "age", value = "我是年龄", required = true) int age,
145241
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
146242
return "hello swagger";
147243
}
244+
245+
148246
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.alibabacloud.mse.demo;
22

3+
import com.alibabacloud.mse.demo.service.HelloServiceC;
4+
import org.apache.dubbo.config.annotation.Reference;
35
import org.apache.http.HttpResponse;
46
import org.apache.http.client.HttpClient;
57
import org.apache.http.client.config.RequestConfig;
@@ -10,11 +12,15 @@
1012
import org.springframework.beans.factory.annotation.Qualifier;
1113
import org.springframework.cloud.commons.util.InetUtils;
1214
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.PathVariable;
1316
import org.springframework.web.bind.annotation.RestController;
1417
import org.springframework.web.client.RestTemplate;
1518

1619
import javax.annotation.PostConstruct;
1720
import javax.servlet.http.HttpServletRequest;
21+
import java.util.Random;
22+
import java.util.concurrent.ExecutionException;
23+
import java.util.concurrent.TimeUnit;
1824

1925
@RestController
2026
class BController {
@@ -27,6 +33,9 @@ class BController {
2733
@Qualifier("restTemplate")
2834
private RestTemplate restTemplate;
2935

36+
@Reference(application = "${dubbo.application.id}", version = "1.1.0")
37+
private HelloServiceC helloServiceC;
38+
3039
@Autowired
3140
InetUtils inetUtils;
3241

@@ -35,6 +44,8 @@ class BController {
3544

3645
private String currentZone;
3746

47+
private static final Random RANDOM = new Random();
48+
3849
@PostConstruct
3950
private void init() {
4051
try {
@@ -53,6 +64,53 @@ private void init() {
5364
}
5465
}
5566

67+
68+
@GetMapping("/flow")
69+
public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException {
70+
long sleepTime = 5 + RANDOM.nextInt(5);
71+
silentSleep(sleepTime);
72+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime;
73+
}
74+
75+
@GetMapping("/params/{hot}")
76+
public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
77+
long sleepTime = 5 + RANDOM.nextInt(5);
78+
silentSleep(sleepTime);
79+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime+":"+hot;
80+
}
81+
82+
@GetMapping("/isolate")
83+
public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException {
84+
long sleepTime = 20 + RANDOM.nextInt(5);
85+
silentSleep(sleepTime);
86+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime;
87+
}
88+
89+
90+
@GetMapping("/flow-c")
91+
public String flow_c(HttpServletRequest request) throws ExecutionException, InterruptedException {
92+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
93+
helloServiceC.hello("B");
94+
}
95+
96+
@GetMapping("/params-c/{hot}")
97+
public String params_c(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
98+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
99+
helloServiceC.hello(hot);
100+
}
101+
102+
@GetMapping("/isolate-c")
103+
public String isolate_c(HttpServletRequest request) throws ExecutionException, InterruptedException {
104+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
105+
helloServiceC.hello("B");
106+
}
107+
108+
@GetMapping("/rpc-c")
109+
public String rpc_c(HttpServletRequest request) throws ExecutionException, InterruptedException {
110+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
111+
helloServiceC.world("B");
112+
}
113+
56114
@GetMapping("/b")
57115
public String b(HttpServletRequest request) {
58116
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
@@ -70,4 +128,13 @@ public String spring_boot(HttpServletRequest request) {
70128
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
71129
restTemplate.getForObject("http://sc-c:20003/spring_boot", String.class);
72130
}
131+
132+
133+
private void silentSleep(long ms) {
134+
try {
135+
TimeUnit.MILLISECONDS.sleep(ms);
136+
} catch (InterruptedException ignored) {
137+
}
138+
}
139+
73140
}

mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/service/HelloServiceC.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
public interface HelloServiceC {
44
String hello(String name);
5+
String world(String name);
56
}

mse-simple-demo/C/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,31 @@
7676
<version>4.5.13</version>
7777
</dependency>
7878

79+
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
80+
<dependency>
81+
<groupId>org.mybatis.spring.boot</groupId>
82+
<artifactId>mybatis-spring-boot-starter</artifactId>
83+
<version>2.1.0</version>
84+
</dependency>
85+
86+
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
87+
<dependency>
88+
<groupId>com.h2database</groupId>
89+
<artifactId>h2</artifactId>
90+
<version>1.4.199</version>
91+
</dependency>
92+
93+
<dependency>
94+
<groupId>com.alibaba.csp</groupId>
95+
<artifactId>spring-boot-starter-ahas-sentinel-client</artifactId>
96+
<version>1.10.11</version>
97+
</dependency>
98+
99+
<dependency>
100+
<groupId>mysql</groupId>
101+
<artifactId>mysql-connector-java</artifactId>
102+
<version>8.0.31</version>
103+
</dependency>
79104
</dependencies>
80105

81106
<dependencyManagement>

0 commit comments

Comments
 (0)