Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add circuitbreaker demo #184

Merged
merged 2 commits into from
May 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@

package cn.polarismesh.agent.examples.alibaba.cloud.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;


/**
* @author <a href="mailto:[email protected]">liaochuntao</a>
Expand All @@ -52,14 +59,67 @@ public EchoController(RestTemplate restTemplate) {
this.template = restTemplate;
}

@Autowired
@Qualifier("defaultRestTemplate")
private RestTemplate defaultRestTemplate;

@Autowired
private CircuitBreakerFactory circuitBreakerFactory;

@Bean
@LoadBalanced
public RestTemplate defaultRestTemplate() {
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://service-provider-hoxton");
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(uriBuilderFactory);
return restTemplate;
}

@GetMapping("/rest")
public String circuitBreakRestTemplate() {
return circuitBreakerFactory
.create("service-provider-hoxton#/circuitBreak")
.run(() -> defaultRestTemplate.getForObject("/circuitBreak", String.class),
throwable -> "trigger the refuse for service callee."
);
}

@GetMapping("/echo/{str}")
public ResponseEntity<String> rest(@PathVariable String str) {
ResponseEntity<String> response = template.getForEntity("http://service-provider-hoxton/echo/" + str,
String.class);
return response;
}
}
@FeignClient(name = "service-provider-hoxton", contextId = "fallback-from-polaris")
public interface CircuitBreakerQuickstartCalleeService {

/**
* Check circuit break.
*
* @return circuit break info
*/
@GetMapping("/circuitBreak")
String circuitBreak();
}
@Component
public class CircuitBreakerQuickstartCalleeServiceFallback implements CircuitBreakerQuickstartCalleeServiceWithFallback {

@Override
public String circuitBreak() {
return "fallback: trigger the refuse for service callee.";
}
}

@FeignClient(name = "service-provider-hoxton", contextId = "fallback-from-code", fallback = CircuitBreakerQuickstartCalleeServiceFallback.class)
public interface CircuitBreakerQuickstartCalleeServiceWithFallback {

/**
* Check circuit break.
*
* @return circuit break info
*/
@GetMapping("/circuitBreak")
String circuitBreak();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<artifactId>hoxton-quickstart-examples</artifactId>
<packaging>pom</packaging>
<modules>
<module>provider</module>
<module>provider-b</module>
<module>provider-a</module>
<module>consumer</module>
<module>scg</module>
</modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: service-provider-hoxton
name: service-provider-hoxton
app: service-provider-hoxton-a
name: service-provider-hoxton-a
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: service-provider-hoxton
app: service-provider-hoxton-a
template:
metadata:
labels:
app: service-provider-hoxton
app: service-provider-hoxton-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-provider:1.7.0-java8
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-provider-a:1.7.0-java8
imagePullPolicy: Always
name: provider
resources:
Expand All @@ -34,7 +34,7 @@ spec:
command:
- /bin/bash
- -c
- cd /app && java -Dserver.port=65101 -jar main.jar
- cd /app && java -Dserver.port=65001 -jar main.jar
lifecycle:
preStop:
exec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-javaagent-demo-sc-quickstart-hoxton-provider</artifactId>
<artifactId>polaris-javaagent-demo-sc-quickstart-hoxton-provider-a</artifactId>
<packaging>jar</packaging>
<version>1.7.0</version>

<name>hoxton-provider</name>
<name>hoxton-provider-a</name>
<description>Demo Provider Project For Spring Cloud Alibaba</description>

<dependencyManagement>
Expand Down Expand Up @@ -56,6 +56,36 @@
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

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

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

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -43,13 +45,23 @@ public static void main(String[] args) {
@RestController
public static class EchoController {

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

@Value("${name:}")
private String name;


@Value("${server.port}")
private String port;

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

@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("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.enabled=false

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
server.port=65001
spring.application.name=service-provider-hoxton
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.enabled=true
spring.cloud.nacos.config.enabled=false
spring.cloud.nacos.discovery.enabled=false
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.cloud.nacos.password=nacos
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: service-provider-hoxton-b
name: service-provider-hoxton-b
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: service-provider-hoxton-b
template:
metadata:
labels:
app: service-provider-hoxton-b
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-provider-b: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=65004 -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
Loading