11
22package com .alibabacloud .mse .demo ;
33
4- import java .io .File ;
5- import java .io .FileReader ;
6- import java .io .IOException ;
7- import java .util .Enumeration ;
8- import java .util .List ;
9- import java .util .Properties ;
10-
11- import javax .annotation .PostConstruct ;
12- import javax .servlet .http .HttpServletRequest ;
13-
14- import com .alibabacloud .mse .demo .service .HelloServiceB ;
15- import io .swagger .annotations .Api ;
16- import io .swagger .annotations .ApiOperation ;
17- import io .swagger .annotations .ApiParam ;
18- import org .apache .dubbo .config .annotation .Reference ;
19- import org .apache .http .HttpResponse ;
20- import org .apache .http .client .HttpClient ;
21- import org .apache .http .client .config .RequestConfig ;
22- import org .apache .http .client .methods .HttpGet ;
23- import org .apache .http .impl .client .HttpClientBuilder ;
24- import org .apache .http .util .EntityUtils ;
25- import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .apache .commons .lang3 .StringUtils ;
265import org .springframework .boot .SpringApplication ;
276import org .springframework .boot .autoconfigure .SpringBootApplication ;
287import org .springframework .cloud .client .loadbalancer .LoadBalanced ;
29- import org .springframework .cloud .commons .util .InetUtils ;
308import org .springframework .context .annotation .Bean ;
31- import org .springframework .web .bind .annotation .GetMapping ;
32- import org .springframework .web .bind .annotation .RestController ;
9+ import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
3310import org .springframework .web .client .RestTemplate ;
3411
12+ import java .io .File ;
13+ import java .io .FileReader ;
14+ import java .io .IOException ;
15+ import java .util .Properties ;
16+
3517/**
3618 * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
3719 */
@@ -48,106 +30,20 @@ RestTemplate restTemplate() {
4830 return new RestTemplate ();
4931 }
5032
51- @ Reference (application = "${dubbo.application.id}" , version = "1.0.0" )
52- private HelloServiceB helloServiceB ;
53-
54- @ Api (value = "/" , tags = {"入口应用" })
55- @ RestController
56- class AController {
57-
58- @ Autowired
59- RestTemplate restTemplate ;
60-
61- @ Autowired
62- InetUtils inetUtils ;
63-
64- private String currentZone ;
65-
66- @ PostConstruct
67- private void init () {
68- try {
69- HttpClient client = HttpClientBuilder .create ().build ();
70- RequestConfig requestConfig = RequestConfig .custom ()
71- .setConnectionRequestTimeout (1000 )
72- .setConnectTimeout (1000 )
73- .setSocketTimeout (1000 )
74- .build ();
75- HttpGet req = new HttpGet ("http://100.100.100.200/latest/meta-data/zone-id" );
76- req .setConfig (requestConfig );
77- HttpResponse response = client .execute (req );
78- currentZone = EntityUtils .toString (response .getEntity ());
79- } catch (Exception e ) {
80- currentZone = e .getMessage ();
81- }
82- }
83-
84- @ ApiOperation (value = "HTTP 全链路灰度入口" , tags = {"入口应用" })
85- @ GetMapping ("/a" )
86- public String a (HttpServletRequest request ) {
87- StringBuilder headerSb = new StringBuilder ();
88- Enumeration <String > enumeration = request .getHeaderNames ();
89- while (enumeration .hasMoreElements ()) {
90- String headerName = enumeration .nextElement ();
91- Enumeration <String > val = request .getHeaders (headerName );
92- while (val .hasMoreElements ()) {
93- String headerVal = val .nextElement ();
94- headerSb .append (headerName + ":" + headerVal + "," );
95- }
96- }
97- return "A" +SERVICE_TAG +"[" + inetUtils .findFirstNonLoopbackAddress ().getHostAddress () + "]" + " -> " +
98- restTemplate .getForObject ("http://sc-B/b" , String .class );
99- }
100-
101- @ ApiOperation (value = "HTTP 全链路灰度入口" , tags = {"入口应用" })
102- @ GetMapping ("/a-zone" )
103- public String aZone (HttpServletRequest request ) {
104- StringBuilder headerSb = new StringBuilder ();
105- Enumeration <String > enumeration = request .getHeaderNames ();
106- while (enumeration .hasMoreElements ()) {
107- String headerName = enumeration .nextElement ();
108- Enumeration <String > val = request .getHeaders (headerName );
109- while (val .hasMoreElements ()) {
110- String headerVal = val .nextElement ();
111- headerSb .append (headerName + ":" + headerVal + "," );
112- }
113- }
114- return "A" +SERVICE_TAG +"[" + currentZone + "]" + " -> " +
115- restTemplate .getForObject ("http://sc-B/b-zone" , String .class );
116- }
117-
118- @ ApiOperation (value = "Dubbo 全链路灰度入口" , tags = {"入口应用" })
119- @ GetMapping ("/dubbo" )
120- public String dubbo (HttpServletRequest request ) {
121- StringBuilder headerSb = new StringBuilder ();
122- Enumeration <String > enumeration = request .getHeaderNames ();
123- while (enumeration .hasMoreElements ()) {
124- String headerName = enumeration .nextElement ();
125- Enumeration <String > val = request .getHeaders (headerName );
126- while (val .hasMoreElements ()) {
127- String headerVal = val .nextElement ();
128- headerSb .append (headerName + ":" + headerVal + "," );
129- }
130- }
131- return "A" +SERVICE_TAG +"[" + inetUtils .findFirstNonLoopbackAddress ().getHostAddress () + "]" + " -> " +
132- helloServiceB .hello ("A" );
133- }
134-
135- @ GetMapping ("swagger-demo" )
136- @ ApiOperation (value = "这是一个演示swagger的接口 " , tags = {"首页操作页面" })
137- public String swagger (@ ApiParam (name = "name" , value = "我是姓名" , required = true ) String name ,
138- @ ApiParam (name = "age" , value = "我是年龄" , required = true )int age ,
139- @ ApiParam (name = "aliware-products" , value = "我是购买阿里云原生产品列表" , required = true ) List <String > aliwareProducts ) {
140- return "hello swagger" ;
33+ @ Bean (name = "serviceTag" )
34+ String serviceTag () {
35+ String tag = parseServiceTag ("/etc/podinfo/labels" );
36+ if (StringUtils .isNotEmpty (tag )) {
37+ return tag ;
14138 }
39+ return parseServiceTag ("/etc/podinfo/annotations" );
14240 }
14341
144- public static String SERVICE_TAG ="" ;
145- static {
146-
42+ private String parseServiceTag (String path ) {
43+ String tag = null ;
14744 try {
148- File file = new File ("/etc/podinfo/annotations" );
45+ File file = new File (path );
14946 if (file .exists ()) {
150-
15147 Properties properties = new Properties ();
15248 FileReader fr = null ;
15349 try {
@@ -158,19 +54,32 @@ public String swagger(@ApiParam(name = "name", value = "我是姓名", required
15854 if (fr != null ) {
15955 try {
16056 fr .close ();
161- } catch (Throwable ignore ) {}
57+ } catch (Throwable ignore ) {
58+ }
16259 }
16360 }
164- SERVICE_TAG = properties .getProperty ("alicloud.service.tag" ).replace ("\" " ,"" );
61+ tag = properties .getProperty ("alicloud.service.tag" ).replace ("\" " , "" );
16562 } else {
166- SERVICE_TAG = System .getProperty ("alicloud.service.tag" );
63+ tag = System .getProperty ("alicloud.service.tag" );
16764 }
168- } catch (Throwable ignore ) {}
65+ } catch (Throwable ignore ) {
66+ }
16967
170- if ("null" .equalsIgnoreCase (SERVICE_TAG ) || null == SERVICE_TAG ) {
171- SERVICE_TAG = "" ;
68+ if ("null" .equalsIgnoreCase (tag ) || null == tag ) {
69+ tag = "" ;
17270 }
71+ return tag ;
72+ }
17373
74+ @ Bean (name = "taskExecutor" )
75+ ThreadPoolTaskExecutor taskExecutor () {
76+ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
77+ executor .setThreadNamePrefix ("taskExecutor-default-" );
78+ executor .setCorePoolSize (5 );
79+ executor .setKeepAliveSeconds (30000 );
80+ executor .setMaxPoolSize (10 );
81+ executor .setQueueCapacity (10 );
82+ return executor ;
17483 }
17584
17685}
0 commit comments