-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add circuitbreaker demo * add circuitbreaker demo --------- Co-authored-by: fredrikliu <[email protected]>
- Loading branch information
1 parent
06a4d59
commit acbb2d6
Showing
15 changed files
with
425 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...src/main/resources/application.properties → ...src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 3 additions & 2 deletions
5
...r/src/main/resources/bootstrap.properties → ...a/src/main/resources/bootstrap.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
29 changes: 29 additions & 0 deletions
29
...d-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider-b/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
48 changes: 48 additions & 0 deletions
48
...gins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider-b/deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.