Skip to content

Commit 5417186

Browse files
committed
Merge branch 'agent'
2 parents 073bc6d + 959a476 commit 5417186

File tree

4 files changed

+78
-203
lines changed

4 files changed

+78
-203
lines changed

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

Lines changed: 31 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.alibabacloud.mse.demo.a;
22

3+
import com.alibaba.fastjson.JSON;
34
import com.alibabacloud.mse.demo.a.service.FeignClientTest;
45
import com.alibabacloud.mse.demo.b.service.HelloServiceB;
5-
import com.alibaba.fastjson.JSON;
66
import com.alibabacloud.mse.demo.b.service.HelloServiceBTwo;
77
import io.swagger.annotations.Api;
88
import io.swagger.annotations.ApiOperation;
99
import io.swagger.annotations.ApiParam;
10-
import org.apache.commons.io.IOUtils;
1110
import org.apache.dubbo.config.annotation.Reference;
1211
import org.apache.http.HttpResponse;
1312
import org.apache.http.client.HttpClient;
@@ -26,10 +25,10 @@
2625
import org.springframework.web.client.RestTemplate;
2726

2827
import javax.annotation.PostConstruct;
29-
import javax.servlet.http.HttpServletRequest;
30-
import java.nio.charset.Charset;
31-
import java.util.Enumeration;
28+
import java.io.UnsupportedEncodingException;
29+
import java.net.URLEncoder;
3230
import java.util.List;
31+
import java.util.Map;
3332
import java.util.concurrent.ExecutionException;
3433

3534
@Api(value = "/", tags = {"入口应用"})
@@ -81,24 +80,7 @@ private void init() {
8180

8281
@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
8382
@RequestMapping("/a")
84-
public String a(HttpServletRequest request) throws ExecutionException, InterruptedException {
85-
try {
86-
String body = IOUtils.toString(request.getInputStream(), Charset.defaultCharset());
87-
log.debug("body is {}", body);
88-
} catch (Throwable e) {
89-
log.warn("get body error", e);
90-
}
91-
StringBuilder headerSb = new StringBuilder();
92-
//枚举创建完后无法更改
93-
Enumeration<String> enumeration = request.getHeaderNames();
94-
while (enumeration.hasMoreElements()) {
95-
String headerName = enumeration.nextElement();
96-
Enumeration<String> val = request.getHeaders(headerName);
97-
while (val.hasMoreElements()) {
98-
String headerVal = val.nextElement();
99-
headerSb.append(headerName + ":" + headerVal + ",");
100-
}
101-
}
83+
public String a() throws ExecutionException, InterruptedException {
10284
//这是rpc调用的方式
10385
String result = restTemplate.getForObject("http://sc-B/b", String.class);
10486

@@ -108,17 +90,7 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
10890

10991
@ApiOperation(value = "HTTP 全链路灰度入口 a调用b和c", tags = {"入口应用"})
11092
@GetMapping("/a2bc")
111-
public String a2bc(HttpServletRequest request) throws ExecutionException, InterruptedException {
112-
StringBuilder headerSb = new StringBuilder();
113-
Enumeration<String> enumeration = request.getHeaderNames();
114-
while (enumeration.hasMoreElements()) {
115-
String headerName = enumeration.nextElement();
116-
Enumeration<String> val = request.getHeaders(headerName);
117-
while (val.hasMoreElements()) {
118-
String headerVal = val.nextElement();
119-
headerSb.append(headerName + ":" + headerVal + ",");
120-
}
121-
}
93+
public String a2bc() throws ExecutionException, InterruptedException {
12294

12395
String resultB = "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
12496
"[config=" + configValue + "]" + " -> " + restTemplate.getForObject("http://sc-B/b", String.class);
@@ -130,17 +102,7 @@ public String a2bc(HttpServletRequest request) throws ExecutionException, Interr
130102

131103
@ApiOperation(value = "HTTP 全链路灰度入口 feign", tags = {"入口应用"})
132104
@GetMapping("/aByFeign")
133-
public String aByFeign(HttpServletRequest request) throws ExecutionException, InterruptedException {
134-
StringBuilder headerSb = new StringBuilder();
135-
Enumeration<String> enumeration = request.getHeaderNames();
136-
while (enumeration.hasMoreElements()) {
137-
String headerName = enumeration.nextElement();
138-
Enumeration<String> val = request.getHeaders(headerName);
139-
while (val.hasMoreElements()) {
140-
String headerVal = val.nextElement();
141-
headerSb.append(headerName + ":" + headerVal + ",");
142-
}
143-
}
105+
public String aByFeign() throws ExecutionException, InterruptedException {
144106

145107
String result = feignClient.bByFeign("test");
146108

@@ -150,7 +112,7 @@ public String aByFeign(HttpServletRequest request) throws ExecutionException, In
150112

151113
@ApiOperation(value = "测试防护规则" , tags = {"流量防护"})
152114
@GetMapping("/flow")
153-
public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException {
115+
public String flow() throws ExecutionException, InterruptedException {
154116

155117
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://sc-B/flow", String.class);
156118
HttpStatus status = responseEntity.getStatusCode();
@@ -163,7 +125,7 @@ public String flow(HttpServletRequest request) throws ExecutionException, Interr
163125

164126
@ApiOperation(value = "测试热点规则", tags = {"流量防护"})
165127
@GetMapping("/params/{hot}")
166-
public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
128+
public String params(@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
167129
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://sc-B/params/" + hot, String.class);
168130

169131
HttpStatus status = responseEntity.getStatusCode();
@@ -175,7 +137,7 @@ public String params(HttpServletRequest request,@PathVariable("hot") String hot)
175137

176138
@ApiOperation(value = "测试隔离规则", tags = { "流量防护"})
177139
@GetMapping("/isolate")
178-
public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException {
140+
public String isolate() throws ExecutionException, InterruptedException {
179141
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://sc-B/isolate", String.class);
180142

181143
HttpStatus status = responseEntity.getStatusCode();
@@ -186,130 +148,66 @@ public String isolate(HttpServletRequest request) throws ExecutionException, Int
186148
}
187149

188150
@GetMapping("/sql")
189-
public String sql(HttpServletRequest request) {
190-
191-
String url = "http://sc-B/sql?" + request.getQueryString();
192-
String result = restTemplate.getForObject(url, String.class);
151+
public String sql(@RequestParam Map<String, String> allRequestParams) throws UnsupportedEncodingException {
152+
StringBuilder url = new StringBuilder("http://sc-B/sql?");
153+
String enc = "UTF-8";
154+
for (Map.Entry<String, String> entry : allRequestParams.entrySet()) {
155+
url.append(URLEncoder.encode(entry.getKey(), enc));
156+
url.append("=");
157+
url.append(URLEncoder.encode(entry.getValue(), enc));
158+
url.append("&");
159+
}
160+
String result = restTemplate.getForObject(url.toString(), String.class);
193161
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
194162
"[config=" + configValue + "]" + " -> " + result;
195163
}
196164

197165
@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
198166
@GetMapping("/a-zone")
199-
public String aZone(HttpServletRequest request) {
200-
StringBuilder headerSb = new StringBuilder();
201-
Enumeration<String> enumeration = request.getHeaderNames();
202-
while (enumeration.hasMoreElements()) {
203-
String headerName = enumeration.nextElement();
204-
Enumeration<String> val = request.getHeaders(headerName);
205-
while (val.hasMoreElements()) {
206-
String headerVal = val.nextElement();
207-
headerSb.append(headerName + ":" + headerVal + ",");
208-
}
209-
}
167+
public String aZone() {
210168
return "A" + serviceTag + "[" + currentZone + "]" + " -> " +
211169
restTemplate.getForObject("http://sc-B/b-zone", String.class);
212170
}
213171

214172
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
215173
@GetMapping("/dubbo")
216-
public String dubbo(HttpServletRequest request, @RequestParam(required = false) String param) {
217-
StringBuilder headerSb = new StringBuilder();
218-
Enumeration<String> enumeration = request.getHeaderNames();
219-
while (enumeration.hasMoreElements()) {
220-
String headerName = enumeration.nextElement();
221-
Enumeration<String> val = request.getHeaders(headerName);
222-
while (val.hasMoreElements()) {
223-
String headerVal = val.nextElement();
224-
headerSb.append(headerName + ":" + headerVal + ",");
225-
}
226-
}
174+
public String dubbo(@RequestParam(required = false) String param) {
227175
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
228176
helloServiceB.hello(param);
229177
}
230178

231179

232180
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
233181
@GetMapping("/dubbo2")
234-
public String dubbo2(HttpServletRequest request) {
235-
StringBuilder headerSb = new StringBuilder();
236-
Enumeration<String> enumeration = request.getHeaderNames();
237-
while (enumeration.hasMoreElements()) {
238-
String headerName = enumeration.nextElement();
239-
Enumeration<String> val = request.getHeaders(headerName);
240-
while (val.hasMoreElements()) {
241-
String headerVal = val.nextElement();
242-
headerSb.append(headerName + ":" + headerVal + ",");
243-
}
244-
}
182+
public String dubbo2(@RequestParam Map<String,String> allRequestParams) {
245183
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
246-
helloServiceBTwo.hello2(JSON.toJSONString(request.getParameterMap()));
184+
helloServiceBTwo.hello2(JSON.toJSONString(allRequestParams));
247185
}
248186

249187
@ApiOperation(value = "Dubbo 限流测试", tags = {"入口应用"})
250188
@GetMapping("/dubbo-flow")
251-
public String dubbo_flow(HttpServletRequest request) {
252-
StringBuilder headerSb = new StringBuilder();
253-
Enumeration<String> enumeration = request.getHeaderNames();
254-
while (enumeration.hasMoreElements()) {
255-
String headerName = enumeration.nextElement();
256-
Enumeration<String> val = request.getHeaders(headerName);
257-
while (val.hasMoreElements()) {
258-
String headerVal = val.nextElement();
259-
headerSb.append(headerName + ":" + headerVal + ",");
260-
}
261-
}
189+
public String dubbo_flow() {
262190
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
263191
helloServiceB.hello("A");
264192
}
265193

266194
@ApiOperation(value = "Dubbo 热点测试", tags = {"入口应用"})
267195
@GetMapping("/dubbo-params/{hot}")
268-
public String dubbo_params(HttpServletRequest request, @PathVariable("hot") String hot) {
269-
StringBuilder headerSb = new StringBuilder();
270-
Enumeration<String> enumeration = request.getHeaderNames();
271-
while (enumeration.hasMoreElements()) {
272-
String headerName = enumeration.nextElement();
273-
Enumeration<String> val = request.getHeaders(headerName);
274-
while (val.hasMoreElements()) {
275-
String headerVal = val.nextElement();
276-
headerSb.append(headerName + ":" + headerVal + ",");
277-
}
278-
}
196+
public String dubbo_params(@PathVariable("hot") String hot) {
279197
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " params:" + hot + " -> " +
280198
helloServiceB.hello(hot);
281199
}
282200

283201
@ApiOperation(value = "Dubbo 隔离测试", tags = {"入口应用"})
284202
@GetMapping("/dubbo-isolate")
285-
public String dubbo_isolate(HttpServletRequest request) {
286-
StringBuilder headerSb = new StringBuilder();
287-
Enumeration<String> enumeration = request.getHeaderNames();
288-
while (enumeration.hasMoreElements()) {
289-
String headerName = enumeration.nextElement();
290-
Enumeration<String> val = request.getHeaders(headerName);
291-
while (val.hasMoreElements()) {
292-
String headerVal = val.nextElement();
293-
headerSb.append(headerName + ":" + headerVal + ",");
294-
}
295-
}
203+
public String dubbo_isolate() {
296204
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
297205
helloServiceB.hello("isolate");
298206
}
299207

300208
@ApiOperation(value = "熔断 rt 测试", tags = {"流量防护"})
301209
@GetMapping("/circuit-breaker-rt")
302-
public String circuit_breaker_rt(HttpServletRequest request) throws ExecutionException, InterruptedException {
303-
StringBuilder headerSb = new StringBuilder();
304-
Enumeration<String> enumeration = request.getHeaderNames();
305-
while (enumeration.hasMoreElements()) {
306-
String headerName = enumeration.nextElement();
307-
Enumeration<String> val = request.getHeaders(headerName);
308-
while (val.hasMoreElements()) {
309-
String headerVal = val.nextElement();
310-
headerSb.append(headerName + ":" + headerVal + ",");
311-
}
312-
}
210+
public String circuit_breaker_rt() throws ExecutionException, InterruptedException {
313211

314212
String result = feignClient.circuit_breaker_rt_b();
315213

@@ -319,18 +217,7 @@ public String circuit_breaker_rt(HttpServletRequest request) throws ExecutionExc
319217

320218
@ApiOperation(value = "熔断异常测试" , tags = {"流量防护"})
321219
@GetMapping("/circuit-breaker-exception")
322-
public String circuit_breaker_exception(HttpServletRequest request) throws ExecutionException, InterruptedException {
323-
StringBuilder headerSb = new StringBuilder();
324-
Enumeration<String> enumeration = request.getHeaderNames();
325-
while (enumeration.hasMoreElements()) {
326-
String headerName = enumeration.nextElement();
327-
Enumeration<String> val = request.getHeaders(headerName);
328-
while (val.hasMoreElements()) {
329-
String headerVal = val.nextElement();
330-
headerSb.append(headerName + ":" + headerVal + ",");
331-
}
332-
}
333-
220+
public String circuit_breaker_exception() throws ExecutionException, InterruptedException {
334221
String result = feignClient.circuit_breaker_exception_b();
335222

336223
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
@@ -347,36 +234,14 @@ public String swagger(@ApiParam(name = "name", value = "我是姓名", required
347234

348235
@ApiOperation(value = "Dubbo rt 熔断测试", tags = {"入口应用"})
349236
@GetMapping("/dubbo-circuit-breaker-rt")
350-
public String dubbo_circuit_breaker_rt(HttpServletRequest request) {
351-
StringBuilder headerSb = new StringBuilder();
352-
Enumeration<String> enumeration = request.getHeaderNames();
353-
while (enumeration.hasMoreElements()) {
354-
String headerName = enumeration.nextElement();
355-
Enumeration<String> val = request.getHeaders(headerName);
356-
while (val.hasMoreElements()) {
357-
String headerVal = val.nextElement();
358-
headerSb.append(headerName + ":" + headerVal + ",");
359-
}
360-
}
361-
237+
public String dubbo_circuit_breaker_rt() {
362238
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
363239
helloServiceB.slow();
364240
}
365241

366242
@ApiOperation(value = "Dubbo 异常熔断测试", tags = {"入口应用"})
367243
@GetMapping("/dubbo-circuit-breaker-exception")
368-
public String dubbo_circuit_breaker_exception(HttpServletRequest request) {
369-
370-
StringBuilder headerSb = new StringBuilder();
371-
Enumeration<String> enumeration = request.getHeaderNames();
372-
while (enumeration.hasMoreElements()) {
373-
String headerName = enumeration.nextElement();
374-
Enumeration<String> val = request.getHeaders(headerName);
375-
while (val.hasMoreElements()) {
376-
String headerVal = val.nextElement();
377-
headerSb.append(headerName + ":" + headerVal + ",");
378-
}
379-
}
244+
public String dubbo_circuit_breaker_exception() {
380245
String response = "";
381246
try {
382247
response = helloServiceB.exception();

0 commit comments

Comments
 (0)