Skip to content

Commit eb0061e

Browse files
authored
feat: support config center and update sct version to 2.0.x (#207)
1 parent 91971fc commit eb0061e

File tree

115 files changed

+1539
-795
lines changed

Some content is hidden

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

115 files changed

+1539
-795
lines changed

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/consumer/deployment.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ spec:
1717
app: service-consumer-2022
1818
annotations:
1919
polarismesh.cn/javaagent: "true"
20-
polarismesh.cn/javaagentVersion: "1.7.0-RC4"
21-
polarismesh.cn/javaagentFrameworkName: "spring-cloud"
22-
polarismesh.cn/javaagentFrameworkVersion: "2022"
20+
polarismesh.cn/javaagentConfig: |
21+
{
22+
"spring.cloud.polaris.circuitbreaker.enabled": "true",
23+
"spring.cloud.polaris.lossless.enabled": "true",
24+
"spring.cloud.polaris.router.enabled": "true"
25+
}
2326
spec:
2427
containers:
25-
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-2022-consumer:1.7.0-java17
28+
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-2022-consumer:2.0.1.0-java17
2629
imagePullPolicy: Always
2730
name: consumer
2831
resources:
@@ -34,7 +37,7 @@ spec:
3437
command:
3538
- /bin/bash
3639
- -c
37-
- cd /app && java -Dserver.port=65002 -jar main.jar
40+
- cd /app && java -Dserver.port=65002 -Dspring.cloud.nacos.config.enabled=false -jar main.jar
3841
lifecycle:
3942
preStop:
4043
exec:

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/consumer/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<groupId>com.tencent.polaris</groupId>
1414
<artifactId>polaris-javaagent-demo-sc-quickstart-2022-consumer</artifactId>
1515
<packaging>jar</packaging>
16-
<version>1.7.0</version>
16+
<version>2.0.1.0</version>
1717

1818
<name>2022-consumer</name>
1919
<description>Demo Consumer Project For Spring Cloud Alibaba</description>

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java

Lines changed: 10 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,20 @@
1717

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

20-
import org.springframework.beans.factory.annotation.Autowired;
21-
import org.springframework.beans.factory.annotation.Qualifier;
2220
import org.springframework.boot.SpringApplication;
2321
import org.springframework.boot.autoconfigure.SpringBootApplication;
24-
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
2522
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
26-
import org.springframework.cloud.openfeign.FeignClient;
23+
import org.springframework.cloud.openfeign.EnableFeignClients;
2724
import org.springframework.context.annotation.Bean;
28-
import org.springframework.http.ResponseEntity;
29-
import org.springframework.stereotype.Component;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
3325
import org.springframework.web.client.RestTemplate;
3426
import org.springframework.web.util.DefaultUriBuilderFactory;
35-
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
36-
import org.springframework.beans.factory.annotation.Autowired;
37-
import org.springframework.beans.factory.annotation.Qualifier;
38-
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
39-
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
40-
import org.springframework.http.ResponseEntity;
41-
import org.springframework.web.bind.annotation.GetMapping;
42-
import org.springframework.web.bind.annotation.RequestMapping;
43-
import org.springframework.web.bind.annotation.RestController;
44-
import org.springframework.web.client.RestTemplate;
45-
4627

4728

4829
/**
4930
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
5031
*/
5132
@SpringBootApplication
33+
@EnableFeignClients
5234
public class ConsumerApplication {
5335

5436
public static void main(String[] args) {
@@ -61,87 +43,14 @@ public RestTemplate restTemplate() {
6143
return new RestTemplate();
6244
}
6345

64-
65-
@RestController
66-
public static class EchoController {
67-
68-
private RestTemplate template;
69-
70-
public EchoController(RestTemplate restTemplate) {
71-
this.template = restTemplate;
72-
}
73-
74-
@Autowired
75-
@Qualifier("defaultRestTemplate")
76-
private RestTemplate defaultRestTemplate;
77-
78-
79-
@Autowired
80-
private CircuitBreakerFactory circuitBreakerFactory;
81-
82-
@GetMapping("/rest")
83-
public String circuitBreakRestTemplate() {
84-
return circuitBreakerFactory
85-
.create("service-provider-2022#/circuitBreak")
86-
.run(() -> defaultRestTemplate.getForObject("/circuitBreak", String.class),
87-
throwable -> "trigger the refuse for service callee."
88-
);
89-
}
90-
91-
92-
93-
@Bean
94-
@LoadBalanced
95-
public RestTemplate defaultRestTemplate() {
96-
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://service-provider-2022");
97-
RestTemplate restTemplate = new RestTemplate();
98-
restTemplate.setUriTemplateHandler(uriBuilderFactory);
99-
return restTemplate;
100-
}
101-
102-
103-
104-
@GetMapping("/echo/{str}")
105-
public ResponseEntity<String> rest(@PathVariable String str) {
106-
ResponseEntity<String> response = template.getForEntity("http://service-provider-2022/echo/" + str,
107-
String.class);
108-
return response;
109-
}
110-
111-
112-
113-
114-
}
115-
@FeignClient(name = "service-provider-2022", contextId = "fallback-from-polaris")
116-
public interface CircuitBreakerQuickstartCalleeService {
117-
118-
/**
119-
* Check circuit break.
120-
*
121-
* @return circuit break info
122-
*/
123-
@GetMapping("/circuitBreak")
124-
String circuitBreak();
125-
}
126-
@Component
127-
public class CircuitBreakerQuickstartCalleeServiceFallback implements CircuitBreakerQuickstartCalleeServiceWithFallback {
128-
129-
@Override
130-
public String circuitBreak() {
131-
return "fallback: trigger the refuse for service callee.";
132-
}
133-
}
134-
135-
@FeignClient(name = "service-provider-2022", contextId = "fallback-from-code", fallback = CircuitBreakerQuickstartCalleeServiceFallback.class)
136-
public interface CircuitBreakerQuickstartCalleeServiceWithFallback {
137-
138-
/**
139-
* Check circuit break.
140-
*
141-
* @return circuit break info
142-
*/
143-
@GetMapping("/circuitBreak")
144-
String circuitBreak();
46+
@Bean
47+
@LoadBalanced
48+
public RestTemplate defaultRestTemplate() {
49+
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(
50+
"http://service-provider-2022");
51+
RestTemplate restTemplate = new RestTemplate();
52+
restTemplate.setUriTemplateHandler(uriBuilderFactory);
53+
return restTemplate;
14554
}
14655

14756
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
2+
3+
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.client.RestTemplate;
15+
16+
17+
@RestController
18+
@RequestMapping("/")
19+
public class ConsumerController {
20+
21+
private static final Logger LOG = LoggerFactory.getLogger(ConsumerController.class);
22+
23+
@Autowired
24+
@Qualifier("restTemplate")
25+
private RestTemplate template;
26+
27+
@Autowired
28+
@Qualifier("defaultRestTemplate")
29+
private RestTemplate defaultRestTemplate;
30+
31+
@Autowired
32+
private ConsumerServiceWithFallback consumerServiceWithFallback;
33+
34+
@Autowired
35+
private ConsumerService consumerService;
36+
37+
@Autowired
38+
private CircuitBreakerFactory circuitBreakerFactory;
39+
40+
@GetMapping("/rest/circuitBreak")
41+
public String circuitBreakRestTemplate() {
42+
return circuitBreakerFactory
43+
.create("service-provider-2022#/circuitBreak")
44+
.run(() -> defaultRestTemplate.getForObject("/circuitBreak", String.class),
45+
throwable -> "trigger the refuse for service callee."
46+
);
47+
}
48+
49+
@GetMapping("/echo/{str}")
50+
public ResponseEntity<String> rest(@PathVariable String str) {
51+
ResponseEntity<String> response = template.getForEntity("http://service-provider-2022/echo/" + str,
52+
String.class);
53+
LOG.info("response:{}", response);
54+
return response;
55+
}
56+
57+
@GetMapping("/feign/circuitBreak/fallbackFromCode")
58+
public String circuitBreakFeignFallbackFromCode() {
59+
return consumerServiceWithFallback.circuitBreak();
60+
}
61+
62+
@GetMapping("/feign/circuitBreak/fallbackFromPolaris")
63+
public String circuitBreakFeignFallbackFromPolaris() {
64+
return consumerService.circuitBreak();
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
2+
3+
import org.springframework.cloud.openfeign.FeignClient;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@FeignClient(name = "service-provider-2022", contextId = "fallback-from-polaris")
7+
public interface ConsumerService {
8+
/**
9+
* Check circuit break.
10+
*
11+
* @return circuit break info
12+
*/
13+
@GetMapping("/circuitBreak")
14+
String circuitBreak();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class ConsumerServiceFallback implements ConsumerServiceWithFallback {
7+
@Override
8+
public String circuitBreak() {
9+
return "fallback: trigger the refuse for service callee.";
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
2+
3+
import org.springframework.cloud.openfeign.FeignClient;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@FeignClient(name = "service-provider-2022", contextId = "fallback-from-code",
7+
fallback = ConsumerServiceFallback.class)
8+
public interface ConsumerServiceWithFallback {
9+
10+
/**
11+
* Check circuit break.
12+
*
13+
* @return circuit break info
14+
*/
15+
@GetMapping("/circuitBreak")
16+
String circuitBreak();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.scheduling.annotation.EnableScheduling;
11+
import org.springframework.scheduling.annotation.Scheduled;
12+
import org.springframework.stereotype.Service;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
@EnableScheduling
16+
@Service
17+
public class ScheduleTask {
18+
19+
private static final Logger LOG = LoggerFactory.getLogger(ScheduleTask.class);
20+
21+
@Autowired
22+
@Qualifier("restTemplate")
23+
private RestTemplate template;
24+
25+
@Autowired
26+
private CircuitBreakerFactory circuitBreakerFactory;
27+
28+
@Value("${consumer.auto.test.enabled:false}")
29+
private Boolean autoTest;
30+
31+
@Scheduled(fixedDelayString = "${consumer.auto.test.interval:30000}")
32+
public void autoSendRequest() {
33+
if (!autoTest) {
34+
return;
35+
}
36+
String[] testStrings = {"test1", "test2", "auto-test"};
37+
for (String str : testStrings) {
38+
try {
39+
ResponseEntity<String> response = circuitBreakerFactory.create("autoRequestCircuitBreaker")
40+
.run(() -> template.getForEntity("http://service-provider-2022/echo/" + str, String.class),
41+
throwable -> {
42+
LOG.error("自动请求失败: {}", throwable.getMessage());
43+
return ResponseEntity.status(503).body("服务暂不可用");
44+
});
45+
LOG.info("自动请求[{}]响应: {}", str, response.getBody());
46+
} catch (Exception e) {
47+
LOG.error("自动请求异常: {}", e.getMessage());
48+
}
49+
}
50+
}
51+
52+
}

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/consumer/src/main/resources/application.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/consumer/src/main/resources/application.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)