|
18 | 18 | package cn.polarismesh.agent.examples.alibaba.cloud.cloud; |
19 | 19 |
|
20 | 20 |
|
| 21 | +import com.alibaba.cloud.commons.lang.StringUtils; |
21 | 22 | import org.slf4j.Logger; |
22 | 23 | import org.slf4j.LoggerFactory; |
23 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
24 | 25 | import org.springframework.beans.factory.annotation.Qualifier; |
25 | 26 | 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; |
26 | 30 | 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.*; |
31 | 32 | import org.springframework.web.client.HttpClientErrorException; |
32 | 33 | import org.springframework.web.client.HttpServerErrorException; |
33 | 34 | import org.springframework.web.client.RestTemplate; |
| 35 | +import org.springframework.web.util.UriComponentsBuilder; |
| 36 | + |
| 37 | +import java.util.Map; |
34 | 38 |
|
35 | 39 |
|
36 | 40 | @RestController |
@@ -66,11 +70,36 @@ public String circuitBreakRestTemplate() { |
66 | 70 | } |
67 | 71 |
|
68 | 72 | @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 | + } |
74 | 103 | } |
75 | 104 |
|
76 | 105 | @GetMapping("/rest/circuitBreak/fallbackFromPolaris") |
|
0 commit comments