Skip to content

Commit 5eaf50b

Browse files
committed
update sc-c
1 parent 301d0bc commit 5eaf50b

File tree

3 files changed

+88
-72
lines changed

3 files changed

+88
-72
lines changed
Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
21
package 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;
124
import org.springframework.boot.SpringApplication;
135
import org.springframework.boot.autoconfigure.SpringBootApplication;
146
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
15-
import org.springframework.cloud.commons.util.InetUtils;
167
import org.springframework.context.annotation.Bean;
17-
import org.springframework.web.bind.annotation.GetMapping;
18-
import org.springframework.web.bind.annotation.RestController;
198
import org.springframework.web.client.RestTemplate;
209

21-
import javax.annotation.PostConstruct;
22-
import javax.servlet.http.HttpServletRequest;
2310
import java.io.File;
2411
import java.io.FileReader;
2512
import 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
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.beans.factory.annotation.Value;
11+
import org.springframework.cloud.commons.util.InetUtils;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.client.RestTemplate;
15+
16+
import javax.annotation.PostConstruct;
17+
import javax.servlet.http.HttpServletRequest;
18+
19+
@RestController
20+
class CController {
21+
22+
@Autowired
23+
RestTemplate restTemplate;
24+
25+
@Autowired
26+
InetUtils inetUtils;
27+
28+
@Autowired
29+
String servcieTag;
30+
31+
@Value("${throwException:false}")
32+
boolean throwException;
33+
34+
private String currentZone;
35+
36+
@PostConstruct
37+
private void init() {
38+
try {
39+
HttpClient client = HttpClientBuilder.create().build();
40+
RequestConfig requestConfig = RequestConfig.custom()
41+
.setConnectionRequestTimeout(1000)
42+
.setConnectTimeout(1000)
43+
.setSocketTimeout(1000)
44+
.build();
45+
HttpGet req = new HttpGet("http://100.100.100.200/latest/meta-data/zone-id");
46+
req.setConfig(requestConfig);
47+
HttpResponse response = client.execute(req);
48+
currentZone = EntityUtils.toString(response.getEntity());
49+
} catch (Exception e) {
50+
currentZone = e.getMessage();
51+
}
52+
}
53+
54+
@GetMapping("/c")
55+
public String c(HttpServletRequest request) {
56+
if (throwException) {
57+
throw new RuntimeException();
58+
}
59+
return "C" + servcieTag+ "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
60+
}
61+
62+
@GetMapping("/c-zone")
63+
public String cZone(HttpServletRequest request) {
64+
if (throwException) {
65+
throw new RuntimeException();
66+
}
67+
return "C" + servcieTag + "[" + currentZone + "]";
68+
}
69+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import java.nio.charset.StandardCharsets;
1515

16-
import static com.alibabacloud.mse.demo.CApplication.SERVICE_TAG;
17-
1816
@Slf4j
1917
@DubboService(version = "1.0.0")
2018
@RequiredArgsConstructor
@@ -28,6 +26,8 @@ public class HelloServiceCImpl implements HelloServiceC {
2826
@Value("${rocketmq.consumer.topic}")
2927
private String topic;
3028

29+
@Autowired
30+
String servcieTag;
3131

3232
@Value("${throwException:false}")
3333
boolean throwException;
@@ -39,7 +39,7 @@ public String hello(String name) {
3939
throw new RuntimeException();
4040
}
4141

42-
String value = "C" + SERVICE_TAG + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
42+
String value = "C" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
4343
String invokerTag="";
4444
String userData = RpcContext.getContext().getAttachment("__microservice_tag__");
4545
if (!StringUtils.isEmpty(userData)) {

0 commit comments

Comments
 (0)