Skip to content

Commit b5e938e

Browse files
authored
Merge pull request #90 from robberphex/fix-demo
Fix demo
2 parents 203ee60 + c1fa7c1 commit b5e938e

File tree

18 files changed

+319
-273
lines changed

18 files changed

+319
-273
lines changed

mse-simple-demo/A/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FROM eclipse-temurin:8-jdk-alpine
22

3+
# copy arthas
4+
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas
5+
36
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
47
RUN apk add wget unzip tcpdump ngrep iproute2-ss bind-tools
58

mse-simple-demo/A/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@
5959
<dependency>
6060
<groupId>org.apache.dubbo</groupId>
6161
<artifactId>dubbo-spring-boot-starter</artifactId>
62-
<version>2.7.7</version>
62+
<version>2.7.15</version>
6363
</dependency>
6464

6565
<dependency>
6666
<groupId>org.apache.dubbo</groupId>
6767
<artifactId>dubbo-registry-nacos</artifactId>
68-
<version>2.7.7</version>
68+
<version>2.7.15</version>
6969
</dependency>
7070

7171
<dependency>
Lines changed: 35 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11

22
package 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;
265
import org.springframework.boot.SpringApplication;
276
import org.springframework.boot.autoconfigure.SpringBootApplication;
287
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
29-
import org.springframework.cloud.commons.util.InetUtils;
308
import 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;
3310
import 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
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.alibabacloud.mse.demo;
2+
3+
import com.alibabacloud.mse.demo.service.HelloServiceB;
4+
import io.swagger.annotations.Api;
5+
import io.swagger.annotations.ApiOperation;
6+
import io.swagger.annotations.ApiParam;
7+
import org.apache.dubbo.config.annotation.Reference;
8+
import org.apache.http.HttpResponse;
9+
import org.apache.http.client.HttpClient;
10+
import org.apache.http.client.config.RequestConfig;
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.http.impl.client.HttpClientBuilder;
13+
import org.apache.http.util.EntityUtils;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.cloud.commons.util.InetUtils;
16+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
17+
import org.springframework.web.bind.annotation.GetMapping;
18+
import org.springframework.web.bind.annotation.RestController;
19+
import org.springframework.web.client.RestTemplate;
20+
21+
import javax.annotation.PostConstruct;
22+
import javax.servlet.http.HttpServletRequest;
23+
import java.util.Enumeration;
24+
import java.util.List;
25+
import java.util.concurrent.ExecutionException;
26+
27+
@Api(value = "/", tags = {"入口应用"})
28+
@RestController
29+
class AController {
30+
31+
@Autowired
32+
RestTemplate restTemplate;
33+
34+
@Autowired
35+
InetUtils inetUtils;
36+
37+
@Reference(application = "${dubbo.application.id}", version = "1.0.0")
38+
private HelloServiceB helloServiceB;
39+
40+
@Autowired
41+
String servcieTag;
42+
43+
@Autowired
44+
ThreadPoolTaskExecutor taskExecutor;
45+
46+
private String currentZone;
47+
48+
@PostConstruct
49+
private void init() {
50+
try {
51+
HttpClient client = HttpClientBuilder.create().build();
52+
RequestConfig requestConfig = RequestConfig.custom()
53+
.setConnectionRequestTimeout(1000)
54+
.setConnectTimeout(1000)
55+
.setSocketTimeout(1000)
56+
.build();
57+
HttpGet req = new HttpGet("http://100.100.100.200/latest/meta-data/zone-id");
58+
req.setConfig(requestConfig);
59+
HttpResponse response = client.execute(req);
60+
currentZone = EntityUtils.toString(response.getEntity());
61+
} catch (Exception e) {
62+
currentZone = e.getMessage();
63+
}
64+
}
65+
66+
@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
67+
@GetMapping("/a")
68+
public String a(HttpServletRequest request) throws ExecutionException, InterruptedException {
69+
StringBuilder headerSb = new StringBuilder();
70+
Enumeration<String> enumeration = request.getHeaderNames();
71+
while (enumeration.hasMoreElements()) {
72+
String headerName = enumeration.nextElement();
73+
Enumeration<String> val = request.getHeaders(headerName);
74+
while (val.hasMoreElements()) {
75+
String headerVal = val.nextElement();
76+
headerSb.append(headerName + ":" + headerVal + ",");
77+
}
78+
}
79+
80+
String result=restTemplate.getForObject("http://sc-B/b", String.class);
81+
// String result = taskExecutor.submit(() ->
82+
// restTemplate.getForObject("http://sc-B/b", String.class)
83+
// ).get();
84+
85+
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
86+
result;
87+
}
88+
89+
@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
90+
@GetMapping("/a-zone")
91+
public String aZone(HttpServletRequest request) {
92+
StringBuilder headerSb = new StringBuilder();
93+
Enumeration<String> enumeration = request.getHeaderNames();
94+
while (enumeration.hasMoreElements()) {
95+
String headerName = enumeration.nextElement();
96+
Enumeration<String> val = request.getHeaders(headerName);
97+
while (val.hasMoreElements()) {
98+
String headerVal = val.nextElement();
99+
headerSb.append(headerName + ":" + headerVal + ",");
100+
}
101+
}
102+
return "A" + servcieTag + "[" + currentZone + "]" + " -> " +
103+
restTemplate.getForObject("http://sc-B/b-zone", String.class);
104+
}
105+
106+
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
107+
@GetMapping("/dubbo")
108+
public String dubbo(HttpServletRequest request) {
109+
StringBuilder headerSb = new StringBuilder();
110+
Enumeration<String> enumeration = request.getHeaderNames();
111+
while (enumeration.hasMoreElements()) {
112+
String headerName = enumeration.nextElement();
113+
Enumeration<String> val = request.getHeaders(headerName);
114+
while (val.hasMoreElements()) {
115+
String headerVal = val.nextElement();
116+
headerSb.append(headerName + ":" + headerVal + ",");
117+
}
118+
}
119+
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
120+
helloServiceB.hello("A");
121+
}
122+
123+
@GetMapping("swagger-demo")
124+
@ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"})
125+
public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name,
126+
@ApiParam(name = "age", value = "我是年龄", required = true) int age,
127+
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
128+
return "hello swagger";
129+
}
130+
}

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

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

9-
import static com.alibabacloud.mse.demo.AApplication.SERVICE_TAG;
10-
119
@Service(version = "1.0.0")
12-
public class HelloServiceAImpl implements HelloServiceA{
10+
public class HelloServiceAImpl implements HelloServiceA {
1311

1412
@Autowired
1513
InetUtils inetUtils;
1614

1715
@Reference(application = "${dubbo.application.id}", version = "1.0.0")
1816
private HelloServiceB helloServiceB;
1917

18+
@Autowired
19+
String servcieTag;
20+
2021
@Override
2122
public String hello(String name) {
22-
return "A"+SERVICE_TAG+"[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
23+
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
2324
helloServiceB.hello(name);
2425
}
2526
}

0 commit comments

Comments
 (0)