Skip to content

Commit

Permalink
add circuitbreaker example (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: fredrikliu <[email protected]>
  • Loading branch information
fredrikliu and fredrikliu authored May 14, 2024
1 parent 216a5f0 commit e90addd
Show file tree
Hide file tree
Showing 37 changed files with 2,206 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM alpine:3.19.1

ARG file_name
ARG java_version

COPY ./target/${file_name} /app/main.jar

WORKDIR /app

RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories

RUN set -eux && \
apk add openjdk${java_version} && \
apk add bind-tools && \
apk add busybox-extras && \
apk add findutils && \
apk add tcpdump && \
apk add tzdata && \
apk add curl && \
apk add bash && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
date

RUN chmod 777 /app/

RUN ls -la /app/

ENTRYPOINT ["java", "-jar", "/app/main.jar"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: hoxton-callee-a
name: hoxton-callee-a
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: hoxton-callee-a
template:
metadata:
labels:
app: hoxton-callee-a
annotations:
polarismesh.cn/javaagent: "true"
polarismesh.cn/javaagentVersion: "1.7.0-RC2"
polarismesh.cn/javaagentFrameworkName: "spring-cloud"
polarismesh.cn/javaagentFrameworkVersion: "hoxton"
spec:
containers:
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-callee-a:1.7.0-java8
imagePullPolicy: Always
name: provider
resources:
limits:
cpu: "500m"
memory: 1000Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command:
- /bin/bash
- -c
- cd /app && java -Dserver.port=65007 -jar main.jar
lifecycle:
preStop:
exec:
command: ["curl","-X","PUT","http://127.0.0.1:28080/offline","&&","sleep","30"]
readinessProbe:
httpGet:
path: /online
port: 28080
initialDelaySeconds: 3
periodSeconds: 3
restartPolicy: Always
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quickstart-example</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>1.14.0-Hoxton.SR12-SNAPSHOT</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quickstart-callee-service-a</artifactId>
<name>Quickstart Callee Service A</name>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-all</artifactId>
<version>1.14.0-Hoxton.SR12-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.cloud.quickstart.callee;

import java.util.HashMap;
import java.util.Map;

import com.tencent.cloud.common.spi.InstanceMetadataProvider;

import org.springframework.stereotype.Component;

/**
* Custom metadata for instance.
*
* @author Haotian Zhang
*/
@Component
public class CustomMetadata implements InstanceMetadataProvider {

@Override
public Map<String, String> getMetadata() {
Map<String, String> metadata = new HashMap<>();
metadata.put("k1", "v1");
return metadata;
}

@Override
public String getZone() {
return "shenzhen-zone-1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.cloud.quickstart.callee;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.quickstart.callee.config.DataSourceProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;

/**
* Quickstart callee controller.
*
* @author Haotian Zhang
*/
@RestController
@RequestMapping("/quickstart/callee")
public class QuickstartCalleeController {

private static final Logger LOG = LoggerFactory.getLogger(QuickstartCalleeController.class);

@Value("${server.port:0}")
private int port;

@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;

@Value("${appName:${spring.application.name}}")
private String appName;

@Autowired
private DataSourceProperties dataSourceProperties;

/**
* Get sum of two value.
* @param value1 value 1
* @param value2 value 2
* @return sum
*/
@GetMapping("/sum")
public String sum(@RequestParam int value1, @RequestParam int value2) {
LOG.info("Quickstart Callee Service [{}:{}] is called and sum is [{}].", ip, port, value1 + value2);
return String.format("Quickstart Callee Service [%s:%s] is called and sum is [%s].", ip, port, value1 + value2);
}

/**
* Get information of callee.
* @return information of callee
*/
@GetMapping("/info")
public String info() {
LOG.info("Quickstart [{}] Service [{}:{}] is called. datasource = [{}].", appName, ip, port, dataSourceProperties);
return String.format("Quickstart [%s] Service [%s:%s] is called. datasource = [%s].", appName, ip, port, dataSourceProperties);
}

/**
* Mock post save value.
* @return true
*/
@PostMapping("/saveValue")
public Boolean saveValue(@RequestParam int value) {
LOG.info("Quickstart [{}] Service [{}:{}] is called. Mock save value = [{}].", appName, ip, port, value);
return true;
}

/**
* Get path echo of callee.
* @return information of callee
*/
@GetMapping("/path/echo/{param}")
public String pathEcho(@PathVariable String param) {
LOG.info("Quickstart [{}] Service [{}:{}] is called. param = [{}].", appName, ip, port, param);
return String.format("Quickstart [%s] Service [%s:%s] is called. datasource = [%s].", appName, ip, port, param);
}

/**
* Get metadata in HTTP header.
*
* @param metadataStr metadata string
* @return metadata in HTTP header
* @throws UnsupportedEncodingException encoding exception
*/
@RequestMapping("/echo")
public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
throws UnsupportedEncodingException {
LOG.info(URLDecoder.decode(metadataStr, UTF_8));
metadataStr = URLDecoder.decode(metadataStr, UTF_8);
return metadataStr;
}

/**
* Check circuit break.
*
* @return circuit break info
*/
@GetMapping("/circuitBreak")
public String circuitBreak() {
LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port);
return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port);
}

@GetMapping("/faultDetect")
public String health() {
LOG.info("Quickstart Callee Service [{}:{}] is detected right.", ip, port);
return String.format("Quickstart Callee Service [%s:%s] is detected right.", ip, port);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.cloud.quickstart.callee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Quickstart callee application.
*
* @author Haotian Zhang
*/
@SpringBootApplication
public class QuickstartCalleeServiceA {

public static void main(String[] args) {
SpringApplication.run(QuickstartCalleeServiceA.class, args);
}
}
Loading

0 comments on commit e90addd

Please sign in to comment.