Skip to content

Commit 067fabf

Browse files
Merge pull request #127 from LearningGp/mse-simple-demo
Mse simple demo
2 parents 18993fe + 4a6c895 commit 067fabf

40 files changed

+574
-467
lines changed

mse-simple-demo/A/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -e
33

44
cd "$(dirname "$0")"
55

6-
docker build . -t ${REGISTRY}spring-cloud-a:1.2.0
6+
docker build --platform linux/amd64 . -t ${REGISTRY}spring-cloud-a:1.2.0
77

88
if [ -n "${REGISTRY}" ]; then
99
docker push ${REGISTRY}spring-cloud-a:1.2.0

mse-simple-demo/A/pom.xml

+19-2
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,39 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.1.0.RELEASE</version>
17+
<version>2.6.1</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

2121
<properties>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2424
<java.version>1.8</java.version>
25-
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
25+
<spring-cloud.version>2021.0.4</spring-cloud.version>
2626
</properties>
2727

2828
<dependencies>
2929
<dependency>
3030
<groupId>com.alibaba.cloud</groupId>
3131
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
3232
<version>2.1.1.RELEASE</version>
33+
<exclusions>
34+
<exclusion>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
37+
</exclusion>
38+
</exclusions>
3339
</dependency>
3440
<dependency>
3541
<groupId>org.springframework.boot</groupId>
3642
<artifactId>spring-boot-starter-web</artifactId>
3743
</dependency>
3844

45+
<dependency>
46+
<groupId>org.springframework.cloud</groupId>
47+
<artifactId>spring-cloud-loadbalancer</artifactId>
48+
</dependency>
49+
3950
<dependency>
4051
<groupId>org.projectlombok</groupId>
4152
<artifactId>lombok</artifactId>
@@ -91,6 +102,12 @@
91102
<artifactId>commons-lang3</artifactId>
92103
<version>3.12.0</version>
93104
</dependency>
105+
106+
<dependency>
107+
<groupId>org.springframework.cloud</groupId>
108+
<artifactId>spring-cloud-starter-openfeign</artifactId>
109+
<version>2.2.5.RELEASE</version>
110+
</dependency>
94111
</dependencies>
95112

96113
<dependencyManagement>

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

+3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
package com.alibabacloud.mse.demo.a;
33

44
import org.apache.commons.lang3.StringUtils;
5+
import org.springframework.beans.factory.annotation.Value;
56
import org.springframework.boot.SpringApplication;
67
import org.springframework.boot.autoconfigure.SpringBootApplication;
78
import org.springframework.boot.web.client.RestTemplateBuilder;
89
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
10+
import org.springframework.cloud.openfeign.EnableFeignClients;
911
import org.springframework.context.annotation.Bean;
1012
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1113
import org.springframework.web.client.RestTemplate;
@@ -21,6 +23,7 @@
2123
*/
2224
@SpringBootApplication
2325
@EnableSwagger2
26+
@EnableFeignClients
2427
public class AApplication {
2528

2629
public static void main(String[] args) {

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

+66-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.alibabacloud.mse.demo.a;
22

3+
import com.alibabacloud.mse.demo.a.service.FeignClient;
34
import com.alibabacloud.mse.demo.b.service.HelloServiceB;
5+
import com.alibaba.fastjson.JSON;
46
import io.swagger.annotations.Api;
57
import io.swagger.annotations.ApiOperation;
68
import io.swagger.annotations.ApiParam;
@@ -37,6 +39,9 @@ class AController {
3739
@Qualifier("loadBalancedRestTemplate")
3840
private RestTemplate loadBalancedRestTemplate;
3941

42+
@Autowired
43+
private FeignClient feignClient;
44+
4045
@Autowired
4146
@Qualifier("restTemplate")
4247
private RestTemplate restTemplate;
@@ -50,15 +55,11 @@ class AController {
5055
@Autowired
5156
String serviceTag;
5257

53-
@Autowired
54-
ThreadPoolTaskExecutor taskExecutor;
55-
5658
@Value("${custom.config.value}")
5759
private String configValue;
5860

5961
private String currentZone;
6062

61-
6263
@PostConstruct
6364
private void init() {
6465
try {
@@ -92,9 +93,48 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
9293
}
9394

9495
String result = loadBalancedRestTemplate.getForObject("http://sc-B/b", String.class);
95-
// String result = taskExecutor.submit(() ->
96-
// restTemplate.getForObject("http://sc-B/b", String.class)
97-
// ).get();
96+
97+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
98+
"[config=" + configValue + "]" + " -> " + result;
99+
}
100+
101+
@ApiOperation(value = "HTTP 全链路灰度入口 a调用b和c", tags = {"入口应用"})
102+
@GetMapping("/a2bc")
103+
public String a2bc(HttpServletRequest request) throws ExecutionException, InterruptedException {
104+
StringBuilder headerSb = new StringBuilder();
105+
Enumeration<String> enumeration = request.getHeaderNames();
106+
while (enumeration.hasMoreElements()) {
107+
String headerName = enumeration.nextElement();
108+
Enumeration<String> val = request.getHeaders(headerName);
109+
while (val.hasMoreElements()) {
110+
String headerVal = val.nextElement();
111+
headerSb.append(headerName + ":" + headerVal + ",");
112+
}
113+
}
114+
115+
String resultB = "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
116+
"[config=" + configValue + "]" + " -> " + loadBalancedRestTemplate.getForObject("http://sc-B/b", String.class);
117+
String resultA = "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
118+
"[config=" + configValue + "]" + " -> " + loadBalancedRestTemplate.getForObject("http://sc-C/c", String.class);
119+
120+
return resultA + "\n" + resultB;
121+
}
122+
123+
@ApiOperation(value = "HTTP 全链路灰度入口 feign", tags = {"入口应用"})
124+
@GetMapping("/aByFeign")
125+
public String aByFeign(HttpServletRequest request) throws ExecutionException, InterruptedException {
126+
StringBuilder headerSb = new StringBuilder();
127+
Enumeration<String> enumeration = request.getHeaderNames();
128+
while (enumeration.hasMoreElements()) {
129+
String headerName = enumeration.nextElement();
130+
Enumeration<String> val = request.getHeaders(headerName);
131+
while (val.hasMoreElements()) {
132+
String headerVal = val.nextElement();
133+
headerSb.append(headerName + ":" + headerVal + ",");
134+
}
135+
}
136+
137+
String result = feignClient.bByFeign("test");
98138

99139
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
100140
"[config=" + configValue + "]" + " -> " + result;
@@ -106,7 +146,7 @@ public String flow(HttpServletRequest request) throws ExecutionException, Interr
106146

107147
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/flow", String.class);
108148
HttpStatus status = responseEntity.getStatusCode();
109-
String result = responseEntity.getBody() + status.value();
149+
String result = responseEntity.getBody() + " code:" + status.value();
110150

111151
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
112152
"[config=" + configValue + "]" + " -> " + result;
@@ -116,13 +156,13 @@ public String flow(HttpServletRequest request) throws ExecutionException, Interr
116156
@ApiOperation(value = "测试热点规则", tags = {"流量防护"})
117157
@GetMapping("/params/{hot}")
118158
public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
119-
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/params/"+hot, String.class);
159+
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/params/" + hot, String.class);
120160

121161
HttpStatus status = responseEntity.getStatusCode();
122-
String result = responseEntity.getBody() + status.value();
162+
String result = responseEntity.getBody() + " code:" + status.value();
123163

124164
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
125-
"[config=" + configValue + "]" + " -> " + result;
165+
"[config=" + configValue + "]" + " params:" + hot + " -> " + result;
126166
}
127167

128168
@ApiOperation(value = "测试隔离规则", tags = { "流量防护"})
@@ -131,7 +171,7 @@ public String isolate(HttpServletRequest request) throws ExecutionException, Int
131171
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/isolate", String.class);
132172

133173
HttpStatus status = responseEntity.getStatusCode();
134-
String result = responseEntity.getBody() + status.value();
174+
String result = responseEntity.getBody() + " code:" + status.value();
135175

136176
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
137177
"[config=" + configValue + "]" + " -> " + result;
@@ -146,6 +186,15 @@ public String spring_boot(HttpServletRequest request) {
146186
" -> " + result;
147187
}
148188

189+
@GetMapping("/sql")
190+
public String sql(HttpServletRequest request) {
191+
192+
String url = "http://sc-B/sql?" + request.getQueryString();
193+
String result = loadBalancedRestTemplate.getForObject(url, String.class);
194+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
195+
"[config=" + configValue + "]" + " -> " + result;
196+
}
197+
149198
@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
150199
@GetMapping("/a-zone")
151200
public String aZone(HttpServletRequest request) {
@@ -177,10 +226,10 @@ public String dubbo(HttpServletRequest request) {
177226
}
178227
}
179228
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
180-
helloServiceB.hello("A");
229+
helloServiceB.hello(JSON.toJSONString(request.getParameterMap()));
181230
}
182231

183-
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
232+
@ApiOperation(value = "Dubbo 限流测试", tags = {"入口应用"})
184233
@GetMapping("/dubbo-flow")
185234
public String dubbo_flow(HttpServletRequest request) {
186235
StringBuilder headerSb = new StringBuilder();
@@ -197,7 +246,7 @@ public String dubbo_flow(HttpServletRequest request) {
197246
helloServiceB.hello("A");
198247
}
199248

200-
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
249+
@ApiOperation(value = "Dubbo 热点测试", tags = {"入口应用"})
201250
@GetMapping("/dubbo-params/{hot}")
202251
public String dubbo_params(HttpServletRequest request, @PathVariable("hot") String hot) {
203252
StringBuilder headerSb = new StringBuilder();
@@ -210,11 +259,11 @@ public String dubbo_params(HttpServletRequest request, @PathVariable("hot") Stri
210259
headerSb.append(headerName + ":" + headerVal + ",");
211260
}
212261
}
213-
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
262+
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " params:" + hot + " -> " +
214263
helloServiceB.hello(hot);
215264
}
216265

217-
@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
266+
@ApiOperation(value = "Dubbo 隔离测试", tags = {"入口应用"})
218267
@GetMapping("/dubbo-isolate")
219268
public String dubbo_isolate(HttpServletRequest request) {
220269
StringBuilder headerSb = new StringBuilder();
@@ -239,6 +288,4 @@ public String swagger(@ApiParam(name = "name", value = "我是姓名", required
239288
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
240289
return "hello swagger";
241290
}
242-
243-
244291
}

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
57
import springfox.documentation.builders.PathSelectors;
68
import springfox.documentation.builders.RequestHandlerSelectors;
79
import springfox.documentation.spi.DocumentationType;
@@ -11,7 +13,7 @@
1113
* Swagger2API文档的配置
1214
*/
1315
@Configuration
14-
public class Swagger2Config {
16+
public class Swagger2Config extends WebMvcConfigurationSupport {
1517

1618
@Bean
1719
public Docket api() {
@@ -21,4 +23,12 @@ public Docket api() {
2123
.paths(PathSelectors.any())
2224
.build();
2325
}
26+
27+
@Override
28+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
29+
registry.
30+
addResourceHandler("/swagger-ui/**")
31+
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
32+
.resourceChain(false);
33+
}
2434
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.alibabacloud.mse.demo.a;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanPostProcessor;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.util.ReflectionUtils;
7+
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
8+
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
9+
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
10+
11+
import java.lang.reflect.Field;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
@Component
16+
public class SwaggerBeanPostProcessor implements BeanPostProcessor {
17+
@Override
18+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
19+
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider)
20+
{
21+
List<RequestMappingInfoHandlerMapping> handlerMappings = getHandlerMappings(bean);
22+
customizeSpringfoxHandlerMappings(handlerMappings);
23+
}
24+
return bean;
25+
}
26+
27+
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
28+
List<T> copy = mappings.stream()
29+
.filter(mapping -> mapping.getPatternParser() == null)
30+
.collect(Collectors.toList());
31+
mappings.clear();
32+
mappings.addAll(copy);
33+
}
34+
35+
@SuppressWarnings("unchecked")
36+
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
37+
try {
38+
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
39+
field.setAccessible(true);
40+
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
41+
}
42+
catch (IllegalArgumentException | IllegalAccessException e) {
43+
throw new IllegalStateException(e);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.alibabacloud.mse.demo.a.service;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestParam;
5+
6+
import javax.servlet.http.HttpServletRequest;
7+
8+
/**
9+
* @author yushan
10+
* @date 2023年02月21日
11+
*/
12+
@org.springframework.cloud.openfeign.FeignClient("sc-B")
13+
public interface FeignClient {
14+
15+
@GetMapping("/bByFeign")
16+
String bByFeign(@RequestParam("s") String s);
17+
}

mse-simple-demo/A/src/main/resources/application.properties

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ server.port=20001
77
spring.cloud.nacos.discovery.server-addr=${nacos.host}:8848
88
spring.cloud.nacos.discovery.namespace=${nacos.namespace}
99

10-
1110
dubbo.application.id=dubbo-provider-A
1211
dubbo.application.name=dubbo-provider-A
1312
dubbo.protocol.id=dubbo

mse-simple-demo/B/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
cd "$(dirname "$0")"
44

5-
docker build . -t ${REGISTRY}spring-cloud-b:1.2.0
5+
docker build --platform linux/amd64 . -t ${REGISTRY}spring-cloud-b:1.2.0
66

77
if [ -n "${REGISTRY}" ]; then
88
docker push ${REGISTRY}spring-cloud-b:1.2.0

0 commit comments

Comments
 (0)