Skip to content

Commit 39f073a

Browse files
committed
serviceTag is a bean
1 parent c481810 commit 39f073a

File tree

6 files changed

+94
-71
lines changed

6 files changed

+94
-71
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AController {
3939
private HelloServiceB helloServiceB;
4040

4141
@Autowired
42-
String servcieTag;
42+
String serviceTag;
4343

4444
@Autowired
4545
ThreadPoolTaskExecutor taskExecutor;
@@ -86,7 +86,7 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
8686
// restTemplate.getForObject("http://sc-B/b", String.class)
8787
// ).get();
8888

89-
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + "[config=" + configValue + "]" + " -> " +
89+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + "[config=" + configValue + "]" + " -> " +
9090
result;
9191
}
9292

@@ -103,7 +103,7 @@ public String aZone(HttpServletRequest request) {
103103
headerSb.append(headerName + ":" + headerVal + ",");
104104
}
105105
}
106-
return "A" + servcieTag + "[" + currentZone + "]" + " -> " +
106+
return "A" + serviceTag + "[" + currentZone + "]" + " -> " +
107107
restTemplate.getForObject("http://sc-B/b-zone", String.class);
108108
}
109109

@@ -120,7 +120,7 @@ public String dubbo(HttpServletRequest request) {
120120
headerSb.append(headerName + ":" + headerVal + ",");
121121
}
122122
}
123-
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
123+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
124124
helloServiceB.hello("A");
125125
}
126126

mse-simple-demo/B/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<version>4.5.13</version>
6262
</dependency>
6363

64+
<dependency>
65+
<groupId>org.apache.commons</groupId>
66+
<artifactId>commons-lang3</artifactId>
67+
<version>3.12.0</version>
68+
</dependency>
69+
6470
</dependencies>
6571

6672
<dependencyManagement>
Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11

22
package com.alibabacloud.mse.demo;
33

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;
4+
import org.apache.commons.lang3.StringUtils;
115
import org.springframework.boot.SpringApplication;
126
import org.springframework.boot.autoconfigure.SpringBootApplication;
137
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
14-
import org.springframework.cloud.commons.util.InetUtils;
158
import org.springframework.context.annotation.Bean;
16-
import org.springframework.web.bind.annotation.GetMapping;
17-
import org.springframework.web.bind.annotation.RestController;
189
import org.springframework.web.client.RestTemplate;
1910

20-
import javax.annotation.PostConstruct;
21-
import javax.servlet.http.HttpServletRequest;
2211
import java.io.File;
2312
import java.io.FileReader;
2413
import java.io.IOException;
@@ -38,56 +27,20 @@ RestTemplate restTemplate() {
3827
return new RestTemplate();
3928
}
4029

41-
@RestController
42-
class AController {
43-
44-
@Autowired
45-
RestTemplate restTemplate;
46-
47-
@Autowired
48-
InetUtils inetUtils;
49-
50-
private String currentZone;
51-
52-
@PostConstruct
53-
private void init() {
54-
try {
55-
HttpClient client = HttpClientBuilder.create().build();
56-
RequestConfig requestConfig = RequestConfig.custom()
57-
.setConnectionRequestTimeout(1000)
58-
.setConnectTimeout(1000)
59-
.setSocketTimeout(1000)
60-
.build();
61-
HttpGet req = new HttpGet("http://100.100.100.200/latest/meta-data/zone-id");
62-
req.setConfig(requestConfig);
63-
HttpResponse response = client.execute(req);
64-
currentZone = EntityUtils.toString(response.getEntity());
65-
} catch (Exception e) {
66-
currentZone = e.getMessage();
67-
}
68-
}
69-
70-
@GetMapping("/b")
71-
public String b(HttpServletRequest request) {
72-
return "B" + SERVICE_TAG + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
73-
restTemplate.getForObject("http://sc-C/c", String.class);
74-
}
75-
76-
@GetMapping("/b-zone")
77-
public String bZone(HttpServletRequest request) {
78-
return "B"+SERVICE_TAG+"[" + currentZone + "]" + " -> " +
79-
restTemplate.getForObject("http://sc-C/c-zone", String.class);
30+
@Bean(name = "serviceTag")
31+
String serviceTag() {
32+
String tag = parseServiceTag("/etc/podinfo/labels");
33+
if (StringUtils.isNotEmpty(tag)) {
34+
return tag;
8035
}
36+
return parseServiceTag("/etc/podinfo/annotations");
8137
}
8238

83-
public static String SERVICE_TAG = "";
84-
85-
static {
86-
39+
private String parseServiceTag(String path) {
40+
String tag = null;
8741
try {
88-
File file = new File("/etc/podinfo/annotations");
42+
File file = new File(path);
8943
if (file.exists()) {
90-
9144
Properties properties = new Properties();
9245
FileReader fr = null;
9346
try {
@@ -102,14 +55,16 @@ public String bZone(HttpServletRequest request) {
10255
}
10356
}
10457
}
105-
SERVICE_TAG = properties.getProperty("alicloud.service.tag").replace("\"", "");
58+
tag = properties.getProperty("alicloud.service.tag").replace("\"", "");
10659
} else {
107-
SERVICE_TAG = System.getProperty("alicloud.service.tag");
60+
tag = System.getProperty("alicloud.service.tag");
10861
}
10962
} catch (Throwable ignore) {
11063
}
111-
if ("null".equalsIgnoreCase(SERVICE_TAG) || null == SERVICE_TAG) {
112-
SERVICE_TAG = "";
64+
65+
if ("null".equalsIgnoreCase(tag) || null == tag) {
66+
tag = "";
11367
}
68+
return tag;
11469
}
11570
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.alibabacloud.mse.demo;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.config.RequestConfig;
6+
import org.apache.http.client.methods.HttpGet;
7+
import org.apache.http.impl.client.HttpClientBuilder;
8+
import org.apache.http.util.EntityUtils;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.cloud.commons.util.InetUtils;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
import javax.annotation.PostConstruct;
16+
import javax.servlet.http.HttpServletRequest;
17+
18+
@RestController
19+
class BController {
20+
21+
@Autowired
22+
RestTemplate restTemplate;
23+
24+
@Autowired
25+
InetUtils inetUtils;
26+
27+
@Autowired
28+
String serviceTag;
29+
30+
private String currentZone;
31+
32+
@PostConstruct
33+
private void init() {
34+
try {
35+
HttpClient client = HttpClientBuilder.create().build();
36+
RequestConfig requestConfig = RequestConfig.custom()
37+
.setConnectionRequestTimeout(1000)
38+
.setConnectTimeout(1000)
39+
.setSocketTimeout(1000)
40+
.build();
41+
HttpGet req = new HttpGet("http://100.100.100.200/latest/meta-data/zone-id");
42+
req.setConfig(requestConfig);
43+
HttpResponse response = client.execute(req);
44+
currentZone = EntityUtils.toString(response.getEntity());
45+
} catch (Exception e) {
46+
currentZone = e.getMessage();
47+
}
48+
}
49+
50+
@GetMapping("/b")
51+
public String b(HttpServletRequest request) {
52+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
53+
restTemplate.getForObject("http://sc-C/c", String.class);
54+
}
55+
56+
@GetMapping("/b-zone")
57+
public String bZone(HttpServletRequest request) {
58+
return "B" + serviceTag + "[" + currentZone + "]" + " -> " +
59+
restTemplate.getForObject("http://sc-C/c-zone", String.class);
60+
}
61+
}

mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/service/HelloServiceBImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.cloud.commons.util.InetUtils;
77

8-
import static com.alibabacloud.mse.demo.BApplication.SERVICE_TAG;
9-
108
@Service(version = "1.0.0")
119
public class HelloServiceBImpl implements HelloServiceB {
1210

1311
@Autowired
1412
InetUtils inetUtils;
1513

14+
@Autowired
15+
String serviceTag;
16+
1617
@Reference(application = "${dubbo.application.id}", version = "1.0.0")
1718
private HelloServiceC helloServiceC;
1819

1920
@Override
2021
public String hello(String name) {
21-
return "B" + SERVICE_TAG + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
22+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
2223
helloServiceC.hello(name);
2324
}
2425

mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/CController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CController {
2626
InetUtils inetUtils;
2727

2828
@Autowired
29-
String servcieTag;
29+
String serviceTag;
3030

3131
@Value("${throwException:false}")
3232
boolean throwException;
@@ -56,14 +56,14 @@ public String c(HttpServletRequest request) {
5656
if (throwException) {
5757
throw new RuntimeException();
5858
}
59-
return "C" + servcieTag+ "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
59+
return "C" + serviceTag+ "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
6060
}
6161

6262
@GetMapping("/c-zone")
6363
public String cZone(HttpServletRequest request) {
6464
if (throwException) {
6565
throw new RuntimeException();
6666
}
67-
return "C" + servcieTag + "[" + currentZone + "]";
67+
return "C" + serviceTag + "[" + currentZone + "]";
6868
}
6969
}

0 commit comments

Comments
 (0)