Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mse-simple-demo/A/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM eclipse-temurin:8-jdk-alpine

# copy arthas
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add wget unzip tcpdump ngrep iproute2-ss bind-tools

Expand Down
4 changes: 2 additions & 2 deletions mse-simple-demo/A/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
<version>2.7.15</version>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.7</version>
<version>2.7.15</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@

package com.alibabacloud.mse.demo;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import com.alibabacloud.mse.demo.service.HelloServiceB;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
Expand All @@ -48,106 +30,20 @@ RestTemplate restTemplate() {
return new RestTemplate();
}

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

@Api(value = "/", tags = {"入口应用"})
@RestController
class AController {

@Autowired
RestTemplate restTemplate;

@Autowired
InetUtils inetUtils;

private String currentZone;

@PostConstruct
private void init() {
try {
HttpClient client = HttpClientBuilder.create().build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000)
.setConnectTimeout(1000)
.setSocketTimeout(1000)
.build();
HttpGet req = new HttpGet("http://100.100.100.200/latest/meta-data/zone-id");
req.setConfig(requestConfig);
HttpResponse response = client.execute(req);
currentZone = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
currentZone = e.getMessage();
}
}

@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/a")
public String a(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A"+SERVICE_TAG+"[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
restTemplate.getForObject("http://sc-B/b", String.class);
}

@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/a-zone")
public String aZone(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A"+SERVICE_TAG+"[" + currentZone + "]" + " -> " +
restTemplate.getForObject("http://sc-B/b-zone", String.class);
}

@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/dubbo")
public String dubbo(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A"+SERVICE_TAG+"[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello("A");
}

@GetMapping("swagger-demo")
@ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"})
public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name,
@ApiParam(name = "age", value = "我是年龄", required = true)int age,
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
return "hello swagger";
@Bean(name = "serviceTag")
String serviceTag() {
String tag = parseServiceTag("/etc/podinfo/labels");
if (StringUtils.isNotEmpty(tag)) {
return tag;
}
return parseServiceTag("/etc/podinfo/annotations");
}

public static String SERVICE_TAG="";
static {

private String parseServiceTag(String path) {
String tag = null;
try {
File file = new File("/etc/podinfo/annotations");
File file = new File(path);
if (file.exists()) {

Properties properties = new Properties();
FileReader fr = null;
try {
Expand All @@ -158,19 +54,32 @@ public String swagger(@ApiParam(name = "name", value = "我是姓名", required
if (fr != null) {
try {
fr.close();
} catch (Throwable ignore) {}
} catch (Throwable ignore) {
}
}
}
SERVICE_TAG = properties.getProperty("alicloud.service.tag").replace("\"","");
tag = properties.getProperty("alicloud.service.tag").replace("\"", "");
} else {
SERVICE_TAG = System.getProperty("alicloud.service.tag");
tag = System.getProperty("alicloud.service.tag");
}
} catch (Throwable ignore) {}
} catch (Throwable ignore) {
}

if ("null".equalsIgnoreCase(SERVICE_TAG) || null == SERVICE_TAG) {
SERVICE_TAG = "";
if ("null".equalsIgnoreCase(tag) || null == tag) {
tag = "";
}
return tag;
}

@Bean(name = "taskExecutor")
ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("taskExecutor-default-");
executor.setCorePoolSize(5);
executor.setKeepAliveSeconds(30000);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(10);
return executor;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.alibabacloud.mse.demo;

import com.alibabacloud.mse.demo.service.HelloServiceB;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Api(value = "/", tags = {"入口应用"})
@RestController
class AController {

@Autowired
RestTemplate restTemplate;

@Autowired
InetUtils inetUtils;

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

@Autowired
String servcieTag;

@Autowired
ThreadPoolTaskExecutor taskExecutor;

private String currentZone;

@PostConstruct
private void init() {
try {
HttpClient client = HttpClientBuilder.create().build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000)
.setConnectTimeout(1000)
.setSocketTimeout(1000)
.build();
HttpGet req = new HttpGet("http://100.100.100.200/latest/meta-data/zone-id");
req.setConfig(requestConfig);
HttpResponse response = client.execute(req);
currentZone = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
currentZone = e.getMessage();
}
}

@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/a")
public String a(HttpServletRequest request) throws ExecutionException, InterruptedException {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}

String result=restTemplate.getForObject("http://sc-B/b", String.class);
// String result = taskExecutor.submit(() ->
// restTemplate.getForObject("http://sc-B/b", String.class)
// ).get();

return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
result;
}

@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/a-zone")
public String aZone(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A" + servcieTag + "[" + currentZone + "]" + " -> " +
restTemplate.getForObject("http://sc-B/b-zone", String.class);
}

@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/dubbo")
public String dubbo(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello("A");
}

@GetMapping("swagger-demo")
@ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"})
public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name,
@ApiParam(name = "age", value = "我是年龄", required = true) int age,
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
return "hello swagger";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.InetUtils;

import static com.alibabacloud.mse.demo.AApplication.SERVICE_TAG;

@Service(version = "1.0.0")
public class HelloServiceAImpl implements HelloServiceA{
public class HelloServiceAImpl implements HelloServiceA {

@Autowired
InetUtils inetUtils;

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

@Autowired
String servcieTag;

@Override
public String hello(String name) {
return "A"+SERVICE_TAG+"[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
return "A" + servcieTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello(name);
}
}
Loading