Skip to content

Mse simple demo #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mse-simple-demo/A/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

cd "$(dirname "$0")"

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

if [ -n "${REGISTRY}" ]; then
docker push ${REGISTRY}spring-cloud-a:1.2.0
Expand Down
21 changes: 19 additions & 2 deletions mse-simple-demo/A/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,39 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -91,6 +102,12 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
package com.alibabacloud.mse.demo.a;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;
Expand All @@ -21,6 +23,7 @@
*/
@SpringBootApplication
@EnableSwagger2
@EnableFeignClients
public class AApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.alibabacloud.mse.demo.a;

import com.alibabacloud.mse.demo.a.service.FeignClient;
import com.alibabacloud.mse.demo.b.service.HelloServiceB;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
Expand Down Expand Up @@ -37,6 +39,9 @@ class AController {
@Qualifier("loadBalancedRestTemplate")
private RestTemplate loadBalancedRestTemplate;

@Autowired
private FeignClient feignClient;

@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;
Expand All @@ -50,15 +55,11 @@ class AController {
@Autowired
String serviceTag;

@Autowired
ThreadPoolTaskExecutor taskExecutor;

@Value("${custom.config.value}")
private String configValue;

private String currentZone;


@PostConstruct
private void init() {
try {
Expand Down Expand Up @@ -92,9 +93,48 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
}

String result = loadBalancedRestTemplate.getForObject("http://sc-B/b", String.class);
// String result = taskExecutor.submit(() ->
// restTemplate.getForObject("http://sc-B/b", String.class)
// ).get();

return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + result;
}

@ApiOperation(value = "HTTP 全链路灰度入口 a调用b和c", tags = {"入口应用"})
@GetMapping("/a2bc")
public String a2bc(HttpServletRequest request) throws ExecutionException, InterruptedException {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}

String resultB = "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + loadBalancedRestTemplate.getForObject("http://sc-B/b", String.class);
String resultA = "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + loadBalancedRestTemplate.getForObject("http://sc-C/c", String.class);

return resultA + "\n" + resultB;
}

@ApiOperation(value = "HTTP 全链路灰度入口 feign", tags = {"入口应用"})
@GetMapping("/aByFeign")
public String aByFeign(HttpServletRequest request) throws ExecutionException, InterruptedException {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}

String result = feignClient.bByFeign("test");

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

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

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

HttpStatus status = responseEntity.getStatusCode();
String result = responseEntity.getBody() + status.value();
String result = responseEntity.getBody() + " code:" + status.value();

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

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

HttpStatus status = responseEntity.getStatusCode();
String result = responseEntity.getBody() + status.value();
String result = responseEntity.getBody() + " code:" + status.value();

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

@GetMapping("/sql")
public String sql(HttpServletRequest request) {

String url = "http://sc-B/sql?" + request.getQueryString();
String result = loadBalancedRestTemplate.getForObject(url, String.class);
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + result;
}

@ApiOperation(value = "HTTP 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/a-zone")
public String aZone(HttpServletRequest request) {
Expand Down Expand Up @@ -177,10 +226,10 @@ public String dubbo(HttpServletRequest request) {
}
}
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello("A");
helloServiceB.hello(JSON.toJSONString(request.getParameterMap()));
}

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

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

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
Expand All @@ -11,7 +13,7 @@
* Swagger2API文档的配置
*/
@Configuration
public class Swagger2Config {
public class Swagger2Config extends WebMvcConfigurationSupport {

@Bean
public Docket api() {
Expand All @@ -21,4 +23,12 @@ public Docket api() {
.paths(PathSelectors.any())
.build();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.
addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.alibabacloud.mse.demo.a;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SwaggerBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider)
{
List<RequestMappingInfoHandlerMapping> handlerMappings = getHandlerMappings(bean);
customizeSpringfoxHandlerMappings(handlerMappings);
}
return bean;
}

private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}

@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
}
catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibabacloud.mse.demo.a.service;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
* @author yushan
* @date 2023年02月21日
*/
@org.springframework.cloud.openfeign.FeignClient("sc-B")
public interface FeignClient {

@GetMapping("/bByFeign")
String bByFeign(@RequestParam("s") String s);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ server.port=20001
spring.cloud.nacos.discovery.server-addr=${nacos.host}:8848
spring.cloud.nacos.discovery.namespace=${nacos.namespace}


dubbo.application.id=dubbo-provider-A
dubbo.application.name=dubbo-provider-A
dubbo.protocol.id=dubbo
Expand Down
2 changes: 1 addition & 1 deletion mse-simple-demo/B/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

cd "$(dirname "$0")"

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

if [ -n "${REGISTRY}" ]; then
docker push ${REGISTRY}spring-cloud-b:1.2.0
Expand Down
Loading