|
15 | 15 | import org.springframework.beans.factory.annotation.Qualifier; |
16 | 16 | import org.springframework.beans.factory.annotation.Value; |
17 | 17 | import org.springframework.cloud.commons.util.InetUtils; |
| 18 | +import org.springframework.http.HttpStatus; |
| 19 | +import org.springframework.http.ResponseEntity; |
18 | 20 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
19 | 21 | import org.springframework.web.bind.annotation.GetMapping; |
| 22 | +import org.springframework.web.bind.annotation.PathVariable; |
20 | 23 | import org.springframework.web.bind.annotation.RestController; |
21 | 24 | import org.springframework.web.client.RestTemplate; |
22 | 25 |
|
23 | 26 | import javax.annotation.PostConstruct; |
24 | 27 | import javax.servlet.http.HttpServletRequest; |
25 | 28 | import java.util.Enumeration; |
26 | 29 | import java.util.List; |
| 30 | +import java.util.Random; |
27 | 31 | import java.util.concurrent.ExecutionException; |
| 32 | +import java.util.concurrent.TimeUnit; |
28 | 33 |
|
29 | 34 | @Api(value = "/", tags = {"入口应用"}) |
30 | 35 | @RestController |
@@ -55,6 +60,7 @@ class AController { |
55 | 60 |
|
56 | 61 | private String currentZone; |
57 | 62 |
|
| 63 | + |
58 | 64 | @PostConstruct |
59 | 65 | private void init() { |
60 | 66 | try { |
@@ -96,6 +102,44 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt |
96 | 102 | "[config=" + configValue + "]" + " -> " + result; |
97 | 103 | } |
98 | 104 |
|
| 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 | + |
99 | 143 | @GetMapping("/spring_boot") |
100 | 144 | public String spring_boot(HttpServletRequest request) { |
101 | 145 | String result = restTemplate.getForObject("http://sc-b:20002/spring_boot", String.class); |
@@ -138,11 +182,65 @@ public String dubbo(HttpServletRequest request) { |
138 | 182 | helloServiceB.hello("A"); |
139 | 183 | } |
140 | 184 |
|
| 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 | + |
141 | 237 | @GetMapping("swagger-demo") |
142 | 238 | @ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"}) |
143 | 239 | public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name, |
144 | 240 | @ApiParam(name = "age", value = "我是年龄", required = true) int age, |
145 | 241 | @ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) { |
146 | 242 | return "hello swagger"; |
147 | 243 | } |
| 244 | + |
| 245 | + |
148 | 246 | } |
0 commit comments