Skip to content

Commit 81b169a

Browse files
committed
add spring boot call
1 parent 89384c0 commit 81b169a

File tree

8 files changed

+63
-19
lines changed

8 files changed

+63
-19
lines changed

mse-simple-demo/A/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ RUN --mount=type=cache,target=/root/.m2/repository/ \
3636

3737
EXPOSE 20001
3838
ENTRYPOINT ["sh", "-c"]
39-
CMD ["java -jar /app/target/A-1.0.0.jar"]
39+
CMD ["java -jar /app/target/A.jar"]

mse-simple-demo/A/pom.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@
107107
<artifactId>spring-boot-maven-plugin</artifactId>
108108
</plugin>
109109
</plugins>
110+
<finalName>${project.artifactId}</finalName>
110111
</build>
111112

112-
113113
</project>
114-

mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/AApplication.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public static void main(String[] args) {
2424
SpringApplication.run(AApplication.class, args);
2525
}
2626

27-
@Bean
27+
@Bean(name = "loadBalancedRestTemplate")
28+
@LoadBalanced
29+
RestTemplate loadBalancedRestTemplate() {
30+
return new RestTemplate();
31+
}
32+
33+
@Bean(name = "restTemplate")
2834
@LoadBalanced
2935
RestTemplate restTemplate() {
3036
return new RestTemplate();

mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/AController.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.http.impl.client.HttpClientBuilder;
1313
import org.apache.http.util.EntityUtils;
1414
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.beans.factory.annotation.Qualifier;
1516
import org.springframework.beans.factory.annotation.Value;
1617
import org.springframework.cloud.commons.util.InetUtils;
1718
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@@ -30,7 +31,12 @@
3031
class AController {
3132

3233
@Autowired
33-
RestTemplate restTemplate;
34+
@Qualifier("loadBalancedRestTemplate")
35+
private RestTemplate loadBalancedRestTemplate;
36+
37+
@Autowired
38+
@Qualifier("restTemplate")
39+
private RestTemplate restTemplate;
3440

3541
@Autowired
3642
InetUtils inetUtils;
@@ -81,13 +87,21 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
8187
}
8288
}
8389

84-
String result=restTemplate.getForObject("http://sc-B/b", String.class);
90+
String result = loadBalancedRestTemplate.getForObject("http://sc-B/b", String.class);
8591
// String result = taskExecutor.submit(() ->
8692
// restTemplate.getForObject("http://sc-B/b", String.class)
8793
// ).get();
8894

89-
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + "[config=" + configValue + "]" + " -> " +
90-
result;
95+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
96+
"[config=" + configValue + "]" + " -> " + result;
97+
}
98+
99+
@GetMapping("/spring_boot")
100+
public String spring_boot(HttpServletRequest request) {
101+
String result = restTemplate.getForObject("http://sc-B/spring_boot", String.class);
102+
103+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
104+
" -> " + result;
91105
}
92106

93107
@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
@@ -104,7 +118,7 @@ public String aZone(HttpServletRequest request) {
104118
}
105119
}
106120
return "A" + serviceTag + "[" + currentZone + "]" + " -> " +
107-
restTemplate.getForObject("http://sc-B/b-zone", String.class);
121+
loadBalancedRestTemplate.getForObject("http://sc-B/b-zone", String.class);
108122
}
109123

110124
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})

mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/BApplication.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ public static void main(String[] args) {
2020
SpringApplication.run(BApplication.class, args);
2121
}
2222

23+
@Bean(name = "loadBalancedRestTemplate")
24+
@LoadBalanced
25+
RestTemplate loadBalancedRestTemplate() {
26+
return new RestTemplate();
27+
}
2328

24-
@Bean
29+
@Bean(name = "restTemplate")
2530
@LoadBalanced
2631
RestTemplate restTemplate() {
2732
return new RestTemplate();

mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/BController.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.http.impl.client.HttpClientBuilder;
88
import org.apache.http.util.EntityUtils;
99
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Qualifier;
1011
import org.springframework.cloud.commons.util.InetUtils;
1112
import org.springframework.web.bind.annotation.GetMapping;
1213
import org.springframework.web.bind.annotation.RestController;
@@ -19,7 +20,12 @@
1920
class BController {
2021

2122
@Autowired
22-
RestTemplate restTemplate;
23+
@Qualifier("loadBalancedRestTemplate")
24+
private RestTemplate loadBalancedRestTemplate;
25+
26+
@Autowired
27+
@Qualifier("restTemplate")
28+
private RestTemplate restTemplate;
2329

2430
@Autowired
2531
InetUtils inetUtils;
@@ -50,12 +56,18 @@ private void init() {
5056
@GetMapping("/b")
5157
public String b(HttpServletRequest request) {
5258
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
53-
restTemplate.getForObject("http://sc-C/c", String.class);
59+
loadBalancedRestTemplate.getForObject("http://sc-C/c", String.class);
5460
}
5561

5662
@GetMapping("/b-zone")
5763
public String bZone(HttpServletRequest request) {
5864
return "B" + serviceTag + "[" + currentZone + "]" + " -> " +
59-
restTemplate.getForObject("http://sc-C/c-zone", String.class);
65+
loadBalancedRestTemplate.getForObject("http://sc-C/c-zone", String.class);
66+
}
67+
68+
@GetMapping("/spring-boot")
69+
public String spring_boot(HttpServletRequest request) {
70+
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
71+
restTemplate.getForObject("http://sc-C/spring-boot", String.class);
6072
}
6173
}

mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/CApplication.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ public static void main(String[] args) {
1919
SpringApplication.run(CApplication.class, args);
2020
}
2121

22-
@Bean
22+
@Bean(name = "loadBalancedRestTemplate")
2323
@LoadBalanced
24-
RestTemplate restTemplate() {
24+
RestTemplate loadBalancedRestTemplate() {
2525
return new RestTemplate();
2626
}
2727

28+
@Bean(name = "restTemplate")
29+
@LoadBalanced
30+
RestTemplate restTemplate() {
31+
return new RestTemplate();
32+
}
2833

2934
@Bean(name = "serviceTag")
3035
String serviceTag() {

mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/CController.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.http.impl.client.HttpClientBuilder;
88
import org.apache.http.util.EntityUtils;
99
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Qualifier;
1011
import org.springframework.beans.factory.annotation.Value;
1112
import org.springframework.cloud.commons.util.InetUtils;
1213
import org.springframework.web.bind.annotation.GetMapping;
@@ -19,9 +20,6 @@
1920
@RestController
2021
class CController {
2122

22-
@Autowired
23-
RestTemplate restTemplate;
24-
2523
@Autowired
2624
InetUtils inetUtils;
2725

@@ -56,7 +54,7 @@ public String c(HttpServletRequest request) {
5654
if (throwException) {
5755
throw new RuntimeException();
5856
}
59-
return "C" + serviceTag+ "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
57+
return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
6058
}
6159

6260
@GetMapping("/c-zone")
@@ -66,4 +64,9 @@ public String cZone(HttpServletRequest request) {
6664
}
6765
return "C" + serviceTag + "[" + currentZone + "]";
6866
}
67+
68+
@GetMapping("/spring-boot")
69+
public String spring_boot(HttpServletRequest request) {
70+
return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
71+
}
6972
}

0 commit comments

Comments
 (0)