Skip to content

Commit e90addd

Browse files
fredrikliufredrikliu
andauthored
add circuitbreaker example (#179)
Co-authored-by: fredrikliu <[email protected]>
1 parent 216a5f0 commit e90addd

37 files changed

+2206
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM alpine:3.19.1
2+
3+
ARG file_name
4+
ARG java_version
5+
6+
COPY ./target/${file_name} /app/main.jar
7+
8+
WORKDIR /app
9+
10+
RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories
11+
12+
RUN set -eux && \
13+
apk add openjdk${java_version} && \
14+
apk add bind-tools && \
15+
apk add busybox-extras && \
16+
apk add findutils && \
17+
apk add tcpdump && \
18+
apk add tzdata && \
19+
apk add curl && \
20+
apk add bash && \
21+
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
22+
echo "Asia/Shanghai" > /etc/timezone && \
23+
date
24+
25+
RUN chmod 777 /app/
26+
27+
RUN ls -la /app/
28+
29+
ENTRYPOINT ["java", "-jar", "/app/main.jar"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
labels:
6+
app: hoxton-callee-a
7+
name: hoxton-callee-a
8+
namespace: default
9+
spec:
10+
replicas: 1
11+
selector:
12+
matchLabels:
13+
app: hoxton-callee-a
14+
template:
15+
metadata:
16+
labels:
17+
app: hoxton-callee-a
18+
annotations:
19+
polarismesh.cn/javaagent: "true"
20+
polarismesh.cn/javaagentVersion: "1.7.0-RC2"
21+
polarismesh.cn/javaagentFrameworkName: "spring-cloud"
22+
polarismesh.cn/javaagentFrameworkVersion: "hoxton"
23+
spec:
24+
containers:
25+
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-callee-a:1.7.0-java8
26+
imagePullPolicy: Always
27+
name: provider
28+
resources:
29+
limits:
30+
cpu: "500m"
31+
memory: 1000Mi
32+
terminationMessagePath: /dev/termination-log
33+
terminationMessagePolicy: File
34+
command:
35+
- /bin/bash
36+
- -c
37+
- cd /app && java -Dserver.port=65007 -jar main.jar
38+
lifecycle:
39+
preStop:
40+
exec:
41+
command: ["curl","-X","PUT","http://127.0.0.1:28080/offline","&&","sleep","30"]
42+
readinessProbe:
43+
httpGet:
44+
path: /online
45+
port: 28080
46+
initialDelaySeconds: 3
47+
periodSeconds: 3
48+
restartPolicy: Always
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>quickstart-example</artifactId>
7+
<groupId>com.tencent.cloud</groupId>
8+
<version>1.14.0-Hoxton.SR12-SNAPSHOT</version>
9+
<relativePath/>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>quickstart-callee-service-a</artifactId>
14+
<name>Quickstart Callee Service A</name>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.alibaba.cloud</groupId>
20+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
21+
<version>2.2.1.RELEASE</version>
22+
<type>pom</type>
23+
<scope>import</scope>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.cloud</groupId>
28+
<artifactId>spring-cloud-dependencies</artifactId>
29+
<version>Hoxton.SR12</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.tencent.cloud</groupId>
39+
<artifactId>spring-cloud-starter-tencent-all</artifactId>
40+
<version>1.14.0-Hoxton.SR12-SNAPSHOT</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-actuator</artifactId>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
<executions>
60+
<execution>
61+
<goals>
62+
<goal>repackage</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-source-plugin</artifactId>
70+
<version>3.2.0</version>
71+
<executions>
72+
<execution>
73+
<id>attach-sources</id>
74+
<goals>
75+
<goal>jar</goal>
76+
</goals>
77+
</execution>
78+
</executions>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.cloud.quickstart.callee;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
import com.tencent.cloud.common.spi.InstanceMetadataProvider;
24+
25+
import org.springframework.stereotype.Component;
26+
27+
/**
28+
* Custom metadata for instance.
29+
*
30+
* @author Haotian Zhang
31+
*/
32+
@Component
33+
public class CustomMetadata implements InstanceMetadataProvider {
34+
35+
@Override
36+
public Map<String, String> getMetadata() {
37+
Map<String, String> metadata = new HashMap<>();
38+
metadata.put("k1", "v1");
39+
return metadata;
40+
}
41+
42+
@Override
43+
public String getZone() {
44+
return "shenzhen-zone-1";
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.cloud.quickstart.callee;
19+
20+
import java.io.UnsupportedEncodingException;
21+
import java.net.URLDecoder;
22+
23+
import com.tencent.cloud.common.constant.MetadataConstant;
24+
import com.tencent.cloud.quickstart.callee.config.DataSourceProperties;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.beans.factory.annotation.Value;
30+
import org.springframework.web.bind.annotation.GetMapping;
31+
import org.springframework.web.bind.annotation.PathVariable;
32+
import org.springframework.web.bind.annotation.PostMapping;
33+
import org.springframework.web.bind.annotation.RequestHeader;
34+
import org.springframework.web.bind.annotation.RequestMapping;
35+
import org.springframework.web.bind.annotation.RequestParam;
36+
import org.springframework.web.bind.annotation.RestController;
37+
38+
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
39+
40+
/**
41+
* Quickstart callee controller.
42+
*
43+
* @author Haotian Zhang
44+
*/
45+
@RestController
46+
@RequestMapping("/quickstart/callee")
47+
public class QuickstartCalleeController {
48+
49+
private static final Logger LOG = LoggerFactory.getLogger(QuickstartCalleeController.class);
50+
51+
@Value("${server.port:0}")
52+
private int port;
53+
54+
@Value("${spring.cloud.client.ip-address:127.0.0.1}")
55+
private String ip;
56+
57+
@Value("${appName:${spring.application.name}}")
58+
private String appName;
59+
60+
@Autowired
61+
private DataSourceProperties dataSourceProperties;
62+
63+
/**
64+
* Get sum of two value.
65+
* @param value1 value 1
66+
* @param value2 value 2
67+
* @return sum
68+
*/
69+
@GetMapping("/sum")
70+
public String sum(@RequestParam int value1, @RequestParam int value2) {
71+
LOG.info("Quickstart Callee Service [{}:{}] is called and sum is [{}].", ip, port, value1 + value2);
72+
return String.format("Quickstart Callee Service [%s:%s] is called and sum is [%s].", ip, port, value1 + value2);
73+
}
74+
75+
/**
76+
* Get information of callee.
77+
* @return information of callee
78+
*/
79+
@GetMapping("/info")
80+
public String info() {
81+
LOG.info("Quickstart [{}] Service [{}:{}] is called. datasource = [{}].", appName, ip, port, dataSourceProperties);
82+
return String.format("Quickstart [%s] Service [%s:%s] is called. datasource = [%s].", appName, ip, port, dataSourceProperties);
83+
}
84+
85+
/**
86+
* Mock post save value.
87+
* @return true
88+
*/
89+
@PostMapping("/saveValue")
90+
public Boolean saveValue(@RequestParam int value) {
91+
LOG.info("Quickstart [{}] Service [{}:{}] is called. Mock save value = [{}].", appName, ip, port, value);
92+
return true;
93+
}
94+
95+
/**
96+
* Get path echo of callee.
97+
* @return information of callee
98+
*/
99+
@GetMapping("/path/echo/{param}")
100+
public String pathEcho(@PathVariable String param) {
101+
LOG.info("Quickstart [{}] Service [{}:{}] is called. param = [{}].", appName, ip, port, param);
102+
return String.format("Quickstart [%s] Service [%s:%s] is called. datasource = [%s].", appName, ip, port, param);
103+
}
104+
105+
/**
106+
* Get metadata in HTTP header.
107+
*
108+
* @param metadataStr metadata string
109+
* @return metadata in HTTP header
110+
* @throws UnsupportedEncodingException encoding exception
111+
*/
112+
@RequestMapping("/echo")
113+
public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
114+
throws UnsupportedEncodingException {
115+
LOG.info(URLDecoder.decode(metadataStr, UTF_8));
116+
metadataStr = URLDecoder.decode(metadataStr, UTF_8);
117+
return metadataStr;
118+
}
119+
120+
/**
121+
* Check circuit break.
122+
*
123+
* @return circuit break info
124+
*/
125+
@GetMapping("/circuitBreak")
126+
public String circuitBreak() {
127+
LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port);
128+
return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port);
129+
}
130+
131+
@GetMapping("/faultDetect")
132+
public String health() {
133+
LOG.info("Quickstart Callee Service [{}:{}] is detected right.", ip, port);
134+
return String.format("Quickstart Callee Service [%s:%s] is detected right.", ip, port);
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.cloud.quickstart.callee;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* Quickstart callee application.
25+
*
26+
* @author Haotian Zhang
27+
*/
28+
@SpringBootApplication
29+
public class QuickstartCalleeServiceA {
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(QuickstartCalleeServiceA.class, args);
33+
}
34+
}

0 commit comments

Comments
 (0)