1-
21package com .alibabacloud .mse .demo ;
32
4- import org .apache .http .HttpResponse ;
5- import org .apache .http .client .HttpClient ;
6- import org .apache .http .client .config .RequestConfig ;
7- import org .apache .http .client .methods .HttpGet ;
8- import org .apache .http .impl .client .HttpClientBuilder ;
9- import org .apache .http .util .EntityUtils ;
10- import org .springframework .beans .factory .annotation .Autowired ;
11- import org .springframework .beans .factory .annotation .Value ;
3+ import org .apache .commons .lang3 .StringUtils ;
124import org .springframework .boot .SpringApplication ;
135import org .springframework .boot .autoconfigure .SpringBootApplication ;
146import org .springframework .cloud .client .loadbalancer .LoadBalanced ;
15- import org .springframework .cloud .commons .util .InetUtils ;
167import org .springframework .context .annotation .Bean ;
17- import org .springframework .web .bind .annotation .GetMapping ;
18- import org .springframework .web .bind .annotation .RestController ;
198import org .springframework .web .client .RestTemplate ;
209
21- import javax .annotation .PostConstruct ;
22- import javax .servlet .http .HttpServletRequest ;
2310import java .io .File ;
2411import java .io .FileReader ;
2512import java .io .IOException ;
@@ -38,63 +25,21 @@ RestTemplate restTemplate() {
3825 return new RestTemplate ();
3926 }
4027
41- @ RestController
42- class AController {
43-
44- @ Autowired
45- RestTemplate restTemplate ;
46-
47- @ Autowired
48- InetUtils inetUtils ;
49-
50- @ Value ("${throwException:false}" )
51- boolean throwException ;
52-
53- private String currentZone ;
54-
55- @ PostConstruct
56- private void init () {
57- try {
58- HttpClient client = HttpClientBuilder .create ().build ();
59- RequestConfig requestConfig = RequestConfig .custom ()
60- .setConnectionRequestTimeout (1000 )
61- .setConnectTimeout (1000 )
62- .setSocketTimeout (1000 )
63- .build ();
64- HttpGet req = new HttpGet ("http://100.100.100.200/latest/meta-data/zone-id" );
65- req .setConfig (requestConfig );
66- HttpResponse response = client .execute (req );
67- currentZone = EntityUtils .toString (response .getEntity ());
68- } catch (Exception e ) {
69- currentZone = e .getMessage ();
70- }
71- }
72-
73- @ GetMapping ("/c" )
74- public String c (HttpServletRequest request ) {
75- if (throwException ) {
76- throw new RuntimeException ();
77- }
78- return "C" + SERVICE_TAG + "[" + inetUtils .findFirstNonLoopbackAddress ().getHostAddress () + "]" ;
79- }
8028
81- @ GetMapping ("/c-zone" )
82- public String cZone (HttpServletRequest request ) {
83- if (throwException ) {
84- throw new RuntimeException ();
85- }
86- return "C" + SERVICE_TAG + "[" + currentZone + "]" ;
29+ @ Bean (name = "serviceTag" )
30+ String serviceTag () {
31+ String tag = parseServiceTag ("/etc/podinfo/labels" );
32+ if (StringUtils .isNotEmpty (tag )) {
33+ return tag ;
8734 }
35+ return parseServiceTag ("/etc/podinfo/annotations" );
8836 }
8937
90- public static String SERVICE_TAG = "" ;
91-
92- static {
93-
38+ private String parseServiceTag (String path ) {
39+ String tag = null ;
9440 try {
95- File file = new File ("/etc/podinfo/annotations" );
41+ File file = new File (path );
9642 if (file .exists ()) {
97-
9843 Properties properties = new Properties ();
9944 FileReader fr = null ;
10045 try {
@@ -109,14 +54,16 @@ public String cZone(HttpServletRequest request) {
10954 }
11055 }
11156 }
112- SERVICE_TAG = properties .getProperty ("alicloud.service.tag" ).replace ("\" " , "" );
57+ tag = properties .getProperty ("alicloud.service.tag" ).replace ("\" " , "" );
11358 } else {
114- SERVICE_TAG = System .getProperty ("alicloud.service.tag" ); ;
59+ tag = System .getProperty ("alicloud.service.tag" );
11560 }
11661 } catch (Throwable ignore ) {
11762 }
118- if ("null" .equalsIgnoreCase (SERVICE_TAG ) || null == SERVICE_TAG ) {
119- SERVICE_TAG = "" ;
63+
64+ if ("null" .equalsIgnoreCase (tag ) || null == tag ) {
65+ tag = "" ;
12066 }
67+ return tag ;
12168 }
12269}
0 commit comments