Skip to content

Commit 5b54218

Browse files
authored
add mse quick start demo (#160)
1 parent 6a93ef5 commit 5b54218

File tree

21 files changed

+602
-0
lines changed

21 files changed

+602
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM openjdk:8-jre-alpine
2+
WORKDIR /app
3+
COPY /target/consumer-1.0.0.jar /app
4+
5+
EXPOSE 20001
6+
ENTRYPOINT ["sh", "-c"]
7+
CMD ["java -jar /app/consumer-1.0.0.jar"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
mvn clean package
4+
docker build . -t consumer:1.0.0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: consumer
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: consumer
9+
template:
10+
metadata:
11+
labels:
12+
app: consumer
13+
spec:
14+
containers:
15+
- env:
16+
- name: JAVA_HOME
17+
value: /usr/lib/jvm/java-1.8-openjdk/jre
18+
- name: dubbo.registry.address
19+
value: nacos-server:8848
20+
- name: spring.cloud.nacos.discovery.server-addr
21+
value: nacos-server:8848
22+
- name: spring.cloud.nacos.config.server-addr
23+
value: nacos-server:8848
24+
image: registry
25+
imagePullPolicy: Always
26+
name: consumer
27+
ports:
28+
- containerPort: 20001
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: consumer-gray
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: consumer-gray
9+
template:
10+
metadata:
11+
labels:
12+
app: consumer-gray
13+
alicloud.service.tag: gray
14+
msePilotCreateAppName: consumer
15+
msePilotAutoEnable: 'on'
16+
spec:
17+
containers:
18+
- env:
19+
- name: JAVA_HOME
20+
value: /usr/lib/jvm/java-1.8-openjdk/jre
21+
- name: dubbo.registry.address
22+
value: nacos-server:8848
23+
- name: spring.cloud.nacos.discovery.server-addr
24+
value: nacos-server:8848
25+
- name: spring.cloud.nacos.config.server-addr
26+
value: nacos-server:8848
27+
image: registry
28+
imagePullPolicy: Always
29+
name: consumer-gray
30+
ports:
31+
- containerPort: 20001
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.alibabacloud.mse.demo</groupId>
7+
<artifactId>consumer</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>consumer</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.alibaba.cloud</groupId>
31+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
32+
<version>2.1.1.RELEASE</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.alibaba.cloud</groupId>
36+
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
37+
<version>2.1.1.RELEASE</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
44+
<!-- Dubbo -->
45+
<dependency>
46+
<groupId>org.apache.dubbo</groupId>
47+
<artifactId>dubbo</artifactId>
48+
<version>2.7.7</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.apache.dubbo</groupId>
53+
<artifactId>dubbo-spring-boot-starter</artifactId>
54+
<version>2.7.7</version>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.apache.dubbo</groupId>
59+
<artifactId>dubbo-registry-nacos</artifactId>
60+
<version>2.7.7</version>
61+
</dependency>
62+
63+
64+
</dependencies>
65+
66+
<dependencyManagement>
67+
<dependencies>
68+
<dependency>
69+
<groupId>org.springframework.cloud</groupId>
70+
<artifactId>spring-cloud-dependencies</artifactId>
71+
<version>${spring-cloud.version}</version>
72+
<type>pom</type>
73+
<scope>import</scope>
74+
</dependency>
75+
</dependencies>
76+
</dependencyManagement>
77+
78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-maven-plugin</artifactId>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
87+
88+
</project>
89+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.alibabacloud.mse.demo;
2+
3+
import com.alibabacloud.mse.demo.service.HelloServiceB;
4+
import org.apache.dubbo.config.annotation.Reference;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
import javax.annotation.PostConstruct;
12+
import javax.servlet.http.HttpServletRequest;
13+
14+
import java.util.concurrent.Executors;
15+
import java.util.concurrent.ScheduledExecutorService;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import static com.alibabacloud.mse.demo.ConsumerApplication.SERVICE_TAG;
19+
20+
@RestController
21+
public class AController {
22+
23+
24+
private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
25+
26+
@Reference(application = "${dubbo.application.id}", version = "1.0.0")
27+
private HelloServiceB helloServiceB;
28+
29+
@Value("${name:123}")
30+
private String name;
31+
32+
33+
@Autowired
34+
RestTemplate restTemplate;
35+
36+
@GetMapping("/a")
37+
public String a(HttpServletRequest request) {
38+
return "A"+SERVICE_TAG+"[" + request.getLocalAddr() + "]" + " -> " +
39+
restTemplate.getForObject("http://provider/b", String.class);
40+
}
41+
42+
@GetMapping("/dubbo")
43+
public String dubbo(HttpServletRequest request) {
44+
return "A"+SERVICE_TAG+"[" + request.getLocalAddr() + "]" + " -> " +
45+
helloServiceB.hello("A");
46+
}
47+
48+
@PostConstruct
49+
private void printConfig() {
50+
EXECUTOR_SERVICE.scheduleWithFixedDelay(new Runnable() {
51+
@Override
52+
public void run() {
53+
System.out.println(name);
54+
}
55+
},0,2, TimeUnit.SECONDS);
56+
}
57+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.alibabacloud.mse.demo;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.util.Properties;
7+
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
@SpringBootApplication
15+
public class ConsumerApplication {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(ConsumerApplication.class, args);
19+
}
20+
21+
@Bean
22+
@LoadBalanced
23+
RestTemplate restTemplate() {
24+
return new RestTemplate();
25+
}
26+
27+
public static String SERVICE_TAG="";
28+
static {
29+
30+
try {
31+
File file = new File("/etc/podinfo/labels");
32+
if (file.exists()) {
33+
34+
Properties properties = new Properties();
35+
FileReader fr = null;
36+
try {
37+
fr = new FileReader(file);
38+
properties.load(fr);
39+
} catch (IOException e) {
40+
} finally {
41+
if (fr != null) {
42+
try {
43+
fr.close();
44+
} catch (Throwable ignore) {}
45+
}
46+
}
47+
SERVICE_TAG = properties.getProperty("alicloud.service.tag").replace("\"","");
48+
}else {
49+
SERVICE_TAG = "";
50+
}
51+
} catch (Throwable ignore) {}
52+
53+
if ("null".equalsIgnoreCase(SERVICE_TAG) || null == SERVICE_TAG) {
54+
SERVICE_TAG = "";
55+
}
56+
57+
}
58+
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.alibabacloud.mse.demo.service;
2+
3+
public interface HelloServiceB {
4+
String hello(String name);
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
server.port=20001
2+
spring.cloud.nacos.discovery.server-addr=nacos-server:8848
3+
dubbo.registry.address=nacos://nacos-server:8848
4+
dubbo.consumer.check=false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.application.name=consumer
2+
spring.cloud.nacos.config.server-addr=nacos-server:8848

0 commit comments

Comments
 (0)