Skip to content

Commit 9b70a64

Browse files
committed
docs:update quickstart demo.
Signed-off-by: Haotian Zhang <928016560@qq.com>
1 parent 7b0a17a commit 9b70a64

File tree

9 files changed

+161
-72
lines changed

9 files changed

+161
-72
lines changed

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
1919

2020

21+
import com.alibaba.cloud.commons.lang.StringUtils;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324
import org.springframework.beans.factory.annotation.Autowired;
2425
import org.springframework.beans.factory.annotation.Qualifier;
2526
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
27+
import org.springframework.http.HttpEntity;
28+
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.ResponseEntity;
27-
import org.springframework.web.bind.annotation.GetMapping;
28-
import org.springframework.web.bind.annotation.PathVariable;
29-
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.bind.annotation.*;
3132
import org.springframework.web.client.HttpClientErrorException;
3233
import org.springframework.web.client.HttpServerErrorException;
3334
import org.springframework.web.client.RestTemplate;
35+
import org.springframework.web.util.UriComponentsBuilder;
36+
37+
import java.util.Map;
3438

3539

3640
@RestController
@@ -66,11 +70,36 @@ public String circuitBreakRestTemplate() {
6670
}
6771

6872
@GetMapping("/echo/{str}")
69-
public ResponseEntity<String> rest(@PathVariable String str) {
70-
ResponseEntity<String> response = template.getForEntity("http://service-provider-2021/echo/" + str,
71-
String.class);
72-
LOG.info("response:{}", response);
73-
return response;
73+
public ResponseEntity<String> rest(@RequestHeader Map<String, String> headerMap,
74+
@PathVariable String str,
75+
@RequestParam String param) {
76+
String url = UriComponentsBuilder
77+
.fromHttpUrl("http://service-provider-2021/echo/" + str)
78+
.queryParam("param", param)
79+
.toUriString();
80+
81+
HttpHeaders headers = new HttpHeaders();
82+
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
83+
if (StringUtils.isNotBlank(entry.getKey()) && StringUtils.isNotBlank(entry.getValue())
84+
&& !entry.getKey().contains("sct-")
85+
&& !entry.getKey().contains("SCT-")
86+
&& !entry.getKey().contains("polaris-")
87+
&& !entry.getKey().contains("POLARIS-")) {
88+
headers.add(entry.getKey(), entry.getValue());
89+
}
90+
}
91+
92+
// 创建 HttpEntity 实例并传入 HttpHeaders
93+
HttpEntity<String> entity = new HttpEntity<>(headers);
94+
95+
// 使用 exchange 方法发送 GET 请求,并获取响应
96+
try {
97+
ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, entity, String.class);
98+
LOG.info("response:{}", response);
99+
return response;
100+
} catch (HttpClientErrorException | HttpServerErrorException httpClientErrorException) {
101+
return new ResponseEntity<>(httpClientErrorException.getResponseBodyAsString(), httpClientErrorException.getStatusCode());
102+
}
74103
}
75104

76105
@GetMapping("/rest/circuitBreak/fallbackFromPolaris")

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2021-examples/quickstart-examples/provider-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.cloud.context.config.annotation.RefreshScope;
2828
import org.springframework.http.HttpStatus;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
30+
import org.springframework.web.bind.annotation.*;
31+
32+
import java.util.Map;
3333

3434
/**
3535
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@@ -85,9 +85,9 @@ public EchoController(Registration registration) {
8585
}
8686

8787
@GetMapping("/echo/{string}")
88-
public String echo(@PathVariable String string) {
89-
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, my metadata : %s, name config:"
90-
+ "%s", svcName, ip, port, string, JacksonUtils.toJson(registration.getMetadata()), name);
88+
public String echo(@RequestHeader Map<String, String> headerMap, @PathVariable String string, @RequestParam String param) {
89+
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, param : %s, header : %s, my metadata : %s, name config:"
90+
+ "%s", svcName, ip, port, string, param, JacksonUtils.toJson(headerMap), JacksonUtils.toJson(registration.getMetadata()), name);
9191
LOG.info("{} -- response result: {}", svcName, result);
9292
return result;
9393
}

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2021-examples/quickstart-examples/provider-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.cloud.context.config.annotation.RefreshScope;
2828
import org.springframework.http.HttpStatus;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
30+
import org.springframework.web.bind.annotation.*;
31+
32+
import java.util.Map;
3333

3434
/**
3535
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@@ -91,12 +91,12 @@ public EchoController(Registration registration) {
9191
}
9292

9393
@GetMapping("/echo/{string}")
94-
public String echo(@PathVariable String string) {
95-
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, my metadata : %s, name config:"
96-
+ "%s", svcName, ip, port, string, JacksonUtils.toJson(registration.getMetadata()), name);
97-
LOG.info("{} -- response result: {}", svcName, result);
98-
return result;
99-
}
94+
public String echo(@RequestHeader Map<String, String> headerMap, @PathVariable String string, @RequestParam String param) {
95+
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, param : %s, header : %s, my metadata : %s, name config:"
96+
+ "%s", svcName, ip, port, string, param, JacksonUtils.toJson(headerMap), JacksonUtils.toJson(registration.getMetadata()), name);
97+
LOG.info("{} -- response result: {}", svcName, result);
98+
return result;
99+
}
100100

101101
@GetMapping("/circuitBreak")
102102
public ResponseEntity<String> circuitBreak() {

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
1919

2020

21+
import com.alibaba.cloud.commons.lang.StringUtils;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324
import org.springframework.beans.factory.annotation.Autowired;
2425
import org.springframework.beans.factory.annotation.Qualifier;
2526
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
27+
import org.springframework.http.HttpEntity;
28+
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.ResponseEntity;
27-
import org.springframework.web.bind.annotation.GetMapping;
28-
import org.springframework.web.bind.annotation.PathVariable;
29-
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.bind.annotation.*;
3132
import org.springframework.web.client.HttpClientErrorException;
3233
import org.springframework.web.client.HttpServerErrorException;
3334
import org.springframework.web.client.RestTemplate;
35+
import org.springframework.web.util.UriComponentsBuilder;
36+
37+
import java.util.Map;
3438

3539

3640
@RestController
@@ -66,11 +70,36 @@ public String circuitBreakRestTemplate() {
6670
}
6771

6872
@GetMapping("/echo/{str}")
69-
public ResponseEntity<String> rest(@PathVariable String str) {
70-
ResponseEntity<String> response = template.getForEntity("http://service-provider-2022/echo/" + str,
71-
String.class);
72-
LOG.info("response:{}", response);
73-
return response;
73+
public ResponseEntity<String> rest(@RequestHeader Map<String, String> headerMap,
74+
@PathVariable String str,
75+
@RequestParam String param) {
76+
String url = UriComponentsBuilder
77+
.fromHttpUrl("http://service-provider-2021/echo/" + str)
78+
.queryParam("param", param)
79+
.toUriString();
80+
81+
HttpHeaders headers = new HttpHeaders();
82+
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
83+
if (StringUtils.isNotBlank(entry.getKey()) && StringUtils.isNotBlank(entry.getValue())
84+
&& !entry.getKey().contains("sct-")
85+
&& !entry.getKey().contains("SCT-")
86+
&& !entry.getKey().contains("polaris-")
87+
&& !entry.getKey().contains("POLARIS-")) {
88+
headers.add(entry.getKey(), entry.getValue());
89+
}
90+
}
91+
92+
// 创建 HttpEntity 实例并传入 HttpHeaders
93+
HttpEntity<String> entity = new HttpEntity<>(headers);
94+
95+
// 使用 exchange 方法发送 GET 请求,并获取响应
96+
try {
97+
ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, entity, String.class);
98+
LOG.info("response:{}", response);
99+
return response;
100+
} catch (HttpClientErrorException | HttpServerErrorException httpClientErrorException) {
101+
return new ResponseEntity<>(httpClientErrorException.getResponseBodyAsString(), httpClientErrorException.getStatusCode());
102+
}
74103
}
75104

76105
@GetMapping("/rest/circuitBreak/fallbackFromPolaris")

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/provider-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
1919

2020

21+
import com.alibaba.nacos.common.utils.JacksonUtils;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324
import org.springframework.beans.factory.annotation.Value;
@@ -27,9 +28,9 @@
2728
import org.springframework.cloud.context.config.annotation.RefreshScope;
2829
import org.springframework.http.HttpStatus;
2930
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.bind.annotation.*;
32+
33+
import java.util.Map;
3334

3435
/**
3536
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@@ -85,9 +86,9 @@ public String circuitBreak() {
8586
}
8687

8788
@GetMapping("/echo/{string}")
88-
public String echo(@PathVariable String string) {
89-
String result = String.format("Hello, I'm %s [%s:%s], receive msg : %s, name config:%s", svcName, ip, port,
90-
string, name);
89+
public String echo(@RequestHeader Map<String, String> headerMap, @PathVariable String string, @RequestParam String param) {
90+
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, param : %s, header : %s, name config:"
91+
+ "%s", svcName, ip, port, string, param, JacksonUtils.toJson(headerMap), name);
9192
LOG.info("{} -- response result: {}", svcName, result);
9293
return result;
9394
}

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2022-examples/quickstart-examples/provider-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
1919

2020

21+
import com.alibaba.nacos.common.utils.JacksonUtils;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324
import org.springframework.beans.factory.annotation.Value;
@@ -27,9 +28,9 @@
2728
import org.springframework.cloud.context.config.annotation.RefreshScope;
2829
import org.springframework.http.HttpStatus;
2930
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.bind.annotation.*;
32+
33+
import java.util.Map;
3334

3435
/**
3536
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@@ -108,9 +109,9 @@ public ResponseEntity<String> circuitBreak() {
108109
}
109110

110111
@GetMapping("/echo/{string}")
111-
public String echo(@PathVariable String string) {
112-
String result = String.format("Hello, I'm %s [%s:%s], receive msg : %s, name config:%s", svcName, ip, port,
113-
string, name);
112+
public String echo(@RequestHeader Map<String, String> headerMap, @PathVariable String string, @RequestParam String param) {
113+
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, param : %s, header : %s, name config:"
114+
+ "%s", svcName, ip, port, string, param, JacksonUtils.toJson(headerMap), name);
114115
LOG.info("{} -- response result: {}", svcName, result);
115116
return result;
116117
}

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818
package cn.polarismesh.agent.examples.alibaba.cloud.cloud;
1919

2020

21+
import com.alibaba.cloud.commons.lang.StringUtils;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324
import org.springframework.beans.factory.annotation.Autowired;
2425
import org.springframework.beans.factory.annotation.Qualifier;
2526
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
27+
import org.springframework.http.HttpEntity;
28+
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.ResponseEntity;
27-
import org.springframework.web.bind.annotation.GetMapping;
28-
import org.springframework.web.bind.annotation.PathVariable;
29-
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.bind.annotation.*;
3132
import org.springframework.web.client.HttpClientErrorException;
3233
import org.springframework.web.client.HttpServerErrorException;
3334
import org.springframework.web.client.RestTemplate;
35+
import org.springframework.web.util.UriComponentsBuilder;
36+
37+
import java.util.Map;
3438

3539

3640
@RestController
@@ -66,11 +70,36 @@ public String circuitBreakRestTemplate() {
6670
}
6771

6872
@GetMapping("/echo/{str}")
69-
public ResponseEntity<String> rest(@PathVariable String str) {
70-
ResponseEntity<String> response = template.getForEntity("http://service-provider-2023/echo/" + str,
71-
String.class);
72-
LOG.info("response:{}", response);
73-
return response;
73+
public ResponseEntity<String> rest(@RequestHeader Map<String, String> headerMap,
74+
@PathVariable String str,
75+
@RequestParam String param) {
76+
String url = UriComponentsBuilder
77+
.fromHttpUrl("http://service-provider-2021/echo/" + str)
78+
.queryParam("param", param)
79+
.toUriString();
80+
81+
HttpHeaders headers = new HttpHeaders();
82+
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
83+
if (StringUtils.isNotBlank(entry.getKey()) && StringUtils.isNotBlank(entry.getValue())
84+
&& !entry.getKey().contains("sct-")
85+
&& !entry.getKey().contains("SCT-")
86+
&& !entry.getKey().contains("polaris-")
87+
&& !entry.getKey().contains("POLARIS-")) {
88+
headers.add(entry.getKey(), entry.getValue());
89+
}
90+
}
91+
92+
// 创建 HttpEntity 实例并传入 HttpHeaders
93+
HttpEntity<String> entity = new HttpEntity<>(headers);
94+
95+
// 使用 exchange 方法发送 GET 请求,并获取响应
96+
try {
97+
ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, entity, String.class);
98+
LOG.info("response:{}", response);
99+
return response;
100+
} catch (HttpClientErrorException | HttpServerErrorException httpClientErrorException) {
101+
return new ResponseEntity<>(httpClientErrorException.getResponseBodyAsString(), httpClientErrorException.getStatusCode());
102+
}
74103
}
75104

76105
@GetMapping("/rest/circuitBreak/fallbackFromPolaris")

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.cloud.context.config.annotation.RefreshScope;
2828
import org.springframework.http.HttpStatus;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
30+
import org.springframework.web.bind.annotation.*;
31+
32+
import java.util.Map;
3333

3434
/**
3535
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@@ -85,12 +85,12 @@ public EchoController(Registration registration) {
8585
}
8686

8787
@GetMapping("/echo/{string}")
88-
public String echo(@PathVariable String string) {
89-
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, my metadata : %s, name config:"
90-
+ "%s", svcName, ip, port, string, JacksonUtils.toJson(registration.getMetadata()), name);
91-
LOG.info("{} -- response result: {}", svcName, result);
92-
return result;
93-
}
88+
public String echo(@RequestHeader Map<String, String> headerMap, @PathVariable String string, @RequestParam String param) {
89+
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, param : %s, header : %s, my metadata : %s, name config:"
90+
+ "%s", svcName, ip, port, string, param, JacksonUtils.toJson(headerMap), JacksonUtils.toJson(registration.getMetadata()), name);
91+
LOG.info("{} -- response result: {}", svcName, result);
92+
return result;
93+
}
9494

9595
@GetMapping("/circuitBreak")
9696
public String circuitBreak() {

polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.cloud.context.config.annotation.RefreshScope;
2828
import org.springframework.http.HttpStatus;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.GetMapping;
31-
import org.springframework.web.bind.annotation.PathVariable;
32-
import org.springframework.web.bind.annotation.RestController;
30+
import org.springframework.web.bind.annotation.*;
31+
32+
import java.util.Map;
3333

3434
/**
3535
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@@ -91,12 +91,12 @@ public EchoController(Registration registration) {
9191
}
9292

9393
@GetMapping("/echo/{string}")
94-
public String echo(@PathVariable String string) {
95-
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, my metadata : %s, name config:"
96-
+ "%s", svcName, ip, port, string, JacksonUtils.toJson(registration.getMetadata()), name);
97-
LOG.info("{} -- response result: {}", svcName, result);
98-
return result;
99-
}
94+
public String echo(@RequestHeader Map<String, String> headerMap, @PathVariable String string, @RequestParam String param) {
95+
String result = String.format("Hello, I'm %s[%s:%s], receive msg : %s, param : %s, header : %s, my metadata : %s, name config:"
96+
+ "%s", svcName, ip, port, string, param, JacksonUtils.toJson(headerMap), JacksonUtils.toJson(registration.getMetadata()), name);
97+
LOG.info("{} -- response result: {}", svcName, result);
98+
return result;
99+
}
100100

101101
@GetMapping("/circuitBreak")
102102
public ResponseEntity<String> circuitBreak() {

0 commit comments

Comments
 (0)