1515import org .springframework .beans .factory .annotation .Qualifier ;
1616import org .springframework .beans .factory .annotation .Value ;
1717import org .springframework .cloud .commons .util .InetUtils ;
18+ import org .springframework .http .HttpStatus ;
19+ import org .springframework .http .ResponseEntity ;
1820import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
1921import org .springframework .web .bind .annotation .GetMapping ;
22+ import org .springframework .web .bind .annotation .PathVariable ;
2023import org .springframework .web .bind .annotation .RestController ;
2124import org .springframework .web .client .RestTemplate ;
2225
2326import javax .annotation .PostConstruct ;
2427import javax .servlet .http .HttpServletRequest ;
2528import java .util .Enumeration ;
2629import java .util .List ;
30+ import java .util .Random ;
2731import java .util .concurrent .ExecutionException ;
32+ import java .util .concurrent .TimeUnit ;
2833
2934@ Api (value = "/" , tags = {"入口应用" })
3035@ RestController
@@ -41,7 +46,7 @@ class AController {
4146 @ Autowired
4247 InetUtils inetUtils ;
4348
44- @ Reference (application = "${dubbo.application.id}" , version = "1.1 .0" )
49+ @ Reference (application = "${dubbo.application.id}" , version = "1.2 .0" )
4550 private HelloServiceB helloServiceB ;
4651
4752 @ Autowired
@@ -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}
0 commit comments