Skip to content

Commit 9e10fb4

Browse files
committed
feat: add heterogeneous demo
1 parent 549626c commit 9e10fb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2073
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM registry-cn-hangzhou.ack.aliyuncs.com/dev/alpine:3.18-update@sha256:f2f97bbc6f3173044292cbf8d306ca31b4356a5f2eb54701fc97f64065b412cf
2+
3+
RUN apk add --no-cache bash
4+
5+
WORKDIR /
6+
COPY gin-c /gin-c
7+
8+
EXPOSE 20003
9+
ENTRYPOINT ["/gin-c"]

mse-heterogeneous-demo/GinC/build.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
export REGISTRY=${REGISTRY}
4+
5+
export appName=gin-c
6+
export VERSION=${VERSION}
7+
8+
set -e
9+
10+
cd "$(dirname "$0")"
11+
12+
#CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .
13+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./aliyun-go-agent go build .
14+
chmod +x ./gin-c
15+
16+
docker build --platform linux/amd64 . -t ${REGISTRY}${appName}:${VERSION}
17+
18+
if [ -n "${REGISTRY}" ]; then
19+
docker push ${REGISTRY}${appName}:${VERSION}
20+
fi

mse-heterogeneous-demo/GinC/go.mod

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module github.com/aliyun/alibabacloud-microservice-demo/mse-heterogeneous/gin-c
2+
3+
go 1.19
4+
5+
require github.com/gin-gonic/gin v1.8.0
6+
7+
require (
8+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
9+
github.com/gin-contrib/sse v0.1.0 // indirect
10+
github.com/go-playground/locales v0.14.1 // indirect
11+
github.com/go-playground/universal-translator v0.18.1 // indirect
12+
github.com/go-playground/validator/v10 v10.14.0 // indirect
13+
github.com/goccy/go-json v0.10.0 // indirect
14+
github.com/google/go-cmp v0.6.0 // indirect
15+
github.com/json-iterator/go v1.1.12 // indirect
16+
github.com/kr/pretty v0.1.0 // indirect
17+
github.com/leodido/go-urn v1.2.4 // indirect
18+
github.com/mattn/go-isatty v0.0.19 // indirect
19+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
20+
github.com/modern-go/reflect2 v1.0.2 // indirect
21+
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
22+
github.com/stretchr/testify v1.9.0 // indirect
23+
github.com/ugorji/go/codec v1.2.11 // indirect
24+
golang.org/x/crypto v0.24.0 // indirect
25+
golang.org/x/net v0.21.0 // indirect
26+
golang.org/x/sys v0.21.0 // indirect
27+
golang.org/x/text v0.16.0 // indirect
28+
google.golang.org/protobuf v1.33.0 // indirect
29+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
30+
gopkg.in/yaml.v2 v2.4.0 // indirect
31+
)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/gin-gonic/gin"
7+
"io"
8+
"net"
9+
"net/http"
10+
"os"
11+
"time"
12+
)
13+
14+
var client *http.Client
15+
var tag string
16+
var ip string
17+
18+
func init() {
19+
tag = os.Getenv("MSE_ALICLOUD_SERVICE_TAG")
20+
if tag == "" {
21+
tag = "base"
22+
}
23+
ip = getLocalIp()
24+
client = &http.Client{Timeout: 5 * time.Second}
25+
}
26+
27+
func getLocalIp() string {
28+
addrs, err := net.InterfaceAddrs()
29+
if err != nil {
30+
return ""
31+
}
32+
for _, address := range addrs {
33+
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
34+
if ipnet.IP.To4() != nil {
35+
return ipnet.IP.String()
36+
}
37+
}
38+
}
39+
return ""
40+
}
41+
42+
func greet(c *gin.Context) {
43+
fmt.Printf("[GetGreet] called, header: %+v, context: %v\n", c.Request.Header, c.Request.Context())
44+
resp, err := doGreet(c)
45+
if err != nil {
46+
fmt.Printf("[GetGreet] doGreet failed, err: %v\n", err)
47+
c.String(http.StatusInternalServerError, err.Error())
48+
return
49+
}
50+
c.String(http.StatusOK, resp)
51+
}
52+
53+
func doGreet(ctx context.Context) (string, error) {
54+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://spring-cloud-d:20004/D/d"), nil)
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
resp, err := client.Do(req)
60+
fmt.Printf("[getGreet] resp: %v\n", resp)
61+
if err != nil {
62+
return "", err
63+
}
64+
if resp.StatusCode != http.StatusOK {
65+
return "", fmt.Errorf("get greet failed, status code: %d", resp.StatusCode)
66+
}
67+
defer resp.Body.Close()
68+
69+
b, err := io.ReadAll(resp.Body)
70+
if err != nil {
71+
fmt.Printf("[getGreet] read resp body failed, err: %v\n", err)
72+
return "", err
73+
}
74+
respData := string(b)
75+
respData = fmt.Sprintf("C:%s:%s", ip, tag) + " - " + respData
76+
fmt.Printf("[getGreet] respData: %v\n", respData)
77+
return respData, nil
78+
}

mse-heterogeneous-demo/GinC/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
func main() {
8+
r := gin.Default()
9+
registerEngine(r)
10+
if err := r.Run(":20003"); err != nil { // listen and serve on 0.0.0.0:8080
11+
panic(err)
12+
}
13+
}
14+
15+
func registerEngine(r *gin.Engine) {
16+
r.GET("/C/c", greet)
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
FROM registry.cn-hangzhou.aliyuncs.com/mse-governance-demo/maven:3-eclipse-temurin-17 as build
2+
3+
COPY <<EOF /root/.m2/settings.xml
4+
<?xml version="1.0"?>
5+
<settings>
6+
<mirrors>
7+
<mirror>
8+
<id>alimaven</id>
9+
<name>aliyun maven</name>
10+
<url>https://maven.aliyun.com/repository/central</url>
11+
<mirrorOf>central</mirrorOf>
12+
</mirror>
13+
</mirrors>
14+
</settings>
15+
EOF
16+
17+
WORKDIR /app
18+
19+
COPY ./ ./
20+
21+
RUN --mount=type=cache,target=/root/.m2/repository/ \
22+
mvn clean package --batch-mode
23+
24+
25+
FROM registry.cn-hangzhou.aliyuncs.com/mse-governance-demo/eclipse-temurin:17-jdk
26+
27+
RUN apt-get update && apt-get install -y \
28+
vim unzip netcat-openbsd telnet tcpdump iproute2 lsof ngrep \
29+
&& rm -rf /var/lib/apt/lists/*
30+
31+
COPY --from=build /app/target/SpringBootB.jar /app/target/SpringBootB.jar
32+
33+
EXPOSE 20001
34+
ENTRYPOINT ["java","-jar","/app/target/SpringBootB.jar"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
export REGISTRY=${REGISTRY}
4+
5+
export appName=spring-boot-b
6+
export VERSION=${VERSION}
7+
8+
set -e
9+
10+
cd "$(dirname "$0")"
11+
12+
docker build --platform linux/amd64 . -t ${REGISTRY}${appName}:${VERSION}
13+
14+
if [ -n "${REGISTRY}" ]; then
15+
docker push ${REGISTRY}${appName}:${VERSION}
16+
fi
17+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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>SpringBootB</artifactId>
8+
<version>3.1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>SpringBootB</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
<java.version>17</java.version>
18+
<spring-boot.version>3.2.4</spring-boot.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.projectlombok</groupId>
33+
<artifactId>lombok</artifactId>
34+
<version>1.18.24</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.plugin</groupId>
39+
<artifactId>spring-plugin-core</artifactId>
40+
<version>2.0.0.RELEASE</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-webflux</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.apache.commons</groupId>
50+
<artifactId>commons-lang3</artifactId>
51+
<version>3.12.0</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.springdoc</groupId>
56+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
57+
<version>2.2.0</version>
58+
</dependency>
59+
</dependencies>
60+
61+
<dependencyManagement>
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-dependencies</artifactId>
66+
<version>${spring-boot.version}</version>
67+
<type>pom</type>
68+
<scope>import</scope>
69+
</dependency>
70+
</dependencies>
71+
</dependencyManagement>
72+
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-maven-plugin</artifactId>
78+
<version>${spring-boot.version}</version>
79+
<executions>
80+
<execution>
81+
<goals>
82+
<goal>repackage</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-compiler-plugin</artifactId>
90+
<version>3.10.1</version>
91+
<configuration>
92+
<compilerArgs>
93+
<arg>-parameters</arg>
94+
</compilerArgs>
95+
<source>17</source>
96+
<target>17</target>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
<finalName>${project.artifactId}</finalName>
101+
</build>
102+
103+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
package com.alibabacloud.mse.demo.b;
3+
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.web.reactive.function.client.WebClient;
9+
10+
import java.io.File;
11+
import java.io.FileReader;
12+
import java.io.IOException;
13+
import java.util.Objects;
14+
import java.util.Properties;
15+
16+
@SpringBootApplication
17+
public class BApplication {
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(BApplication.class, args);
21+
}
22+
23+
@Bean
24+
WebClient.Builder webClientBuilder() {
25+
return WebClient.builder();
26+
}
27+
28+
@Bean(name = "serviceTag")
29+
String serviceTag() {
30+
String tag = parseServiceTag("/etc/podinfo/labels");
31+
if (StringUtils.isNotEmpty(tag)) {
32+
return tag;
33+
}
34+
tag = parseServiceTag("/etc/podinfo/annotations");
35+
if (StringUtils.isNotEmpty(tag)) {
36+
return tag;
37+
}
38+
tag = System.getenv("MSE_ALICLOUD_SERVICE_TAG");
39+
if (StringUtils.isNotEmpty(tag)) {
40+
return tag;
41+
}
42+
43+
return "base";
44+
}
45+
46+
private String parseServiceTag(String path) {
47+
String tag = null;
48+
try {
49+
File file = new File(path);
50+
if (file.exists()) {
51+
Properties properties = new Properties();
52+
FileReader fr = null;
53+
try {
54+
fr = new FileReader(file);
55+
properties.load(fr);
56+
} catch (IOException e) {
57+
} finally {
58+
if (fr != null) {
59+
try {
60+
fr.close();
61+
} catch (Throwable ignore) {
62+
}
63+
}
64+
}
65+
tag = properties.getProperty("alicloud.service.tag").replace("\"", "");
66+
} else {
67+
tag = System.getProperty("alicloud.service.tag");
68+
}
69+
} catch (Throwable ignore) {
70+
}
71+
72+
if ("null".equalsIgnoreCase(tag) || null == tag) {
73+
tag = "";
74+
}
75+
return tag;
76+
}
77+
}

0 commit comments

Comments
 (0)