diff --git a/mse-simple-demo/A/build.sh b/mse-simple-demo/A/build.sh index e11901c8..255987c7 100755 --- a/mse-simple-demo/A/build.sh +++ b/mse-simple-demo/A/build.sh @@ -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 diff --git a/mse-simple-demo/A/pom.xml b/mse-simple-demo/A/pom.xml index f4701260..179f6ee2 100644 --- a/mse-simple-demo/A/pom.xml +++ b/mse-simple-demo/A/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 2.6.1 @@ -22,7 +22,7 @@ UTF-8 UTF-8 1.8 - Greenwich.SR3 + 2021.0.4 @@ -30,12 +30,23 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.1.1.RELEASE + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + org.springframework.boot spring-boot-starter-web + + org.springframework.cloud + spring-cloud-loadbalancer + + org.projectlombok lombok @@ -91,6 +102,12 @@ commons-lang3 3.12.0 + + + org.springframework.cloud + spring-cloud-starter-openfeign + 2.2.5.RELEASE + diff --git a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AApplication.java b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AApplication.java index cf49d4e3..de14c380 100644 --- a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AApplication.java +++ b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AApplication.java @@ -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; @@ -21,6 +23,7 @@ */ @SpringBootApplication @EnableSwagger2 +@EnableFeignClients public class AApplication { public static void main(String[] args) { diff --git a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AController.java b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AController.java index 0f910a1e..bfc0fbdc 100644 --- a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AController.java +++ b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/AController.java @@ -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; @@ -37,6 +39,9 @@ class AController { @Qualifier("loadBalancedRestTemplate") private RestTemplate loadBalancedRestTemplate; + @Autowired + private FeignClient feignClient; + @Autowired @Qualifier("restTemplate") private RestTemplate restTemplate; @@ -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 { @@ -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 enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String headerName = enumeration.nextElement(); + Enumeration 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 enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String headerName = enumeration.nextElement(); + Enumeration 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; @@ -106,7 +146,7 @@ public String flow(HttpServletRequest request) throws ExecutionException, Interr ResponseEntity 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; @@ -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 responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/params/"+hot, String.class); + ResponseEntity 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 = { "流量防护"}) @@ -131,7 +171,7 @@ public String isolate(HttpServletRequest request) throws ExecutionException, Int ResponseEntity 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; @@ -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) { @@ -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(); @@ -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(); @@ -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(); @@ -239,6 +288,4 @@ public String swagger(@ApiParam(name = "name", value = "我是姓名", required @ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List aliwareProducts) { return "hello swagger"; } - - } diff --git a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/Swagger2Config.java b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/Swagger2Config.java index 50a7c536..819b0552 100644 --- a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/Swagger2Config.java +++ b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/Swagger2Config.java @@ -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; @@ -11,7 +13,7 @@ * Swagger2API文档的配置 */ @Configuration -public class Swagger2Config { +public class Swagger2Config extends WebMvcConfigurationSupport { @Bean public Docket api() { @@ -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); + } } diff --git a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/SwaggerBeanPostProcessor.java b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/SwaggerBeanPostProcessor.java new file mode 100644 index 00000000..64459812 --- /dev/null +++ b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/SwaggerBeanPostProcessor.java @@ -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 handlerMappings = getHandlerMappings(bean); + customizeSpringfoxHandlerMappings(handlerMappings); + } + return bean; + } + + private void customizeSpringfoxHandlerMappings(List mappings) { + List copy = mappings.stream() + .filter(mapping -> mapping.getPatternParser() == null) + .collect(Collectors.toList()); + mappings.clear(); + mappings.addAll(copy); + } + + @SuppressWarnings("unchecked") + private List getHandlerMappings(Object bean) { + try { + Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); + field.setAccessible(true); + return (List) field.get(bean); + } + catch (IllegalArgumentException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } +} \ No newline at end of file diff --git a/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/service/FeignClient.java b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/service/FeignClient.java new file mode 100644 index 00000000..670f3363 --- /dev/null +++ b/mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/a/service/FeignClient.java @@ -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); +} diff --git a/mse-simple-demo/A/src/main/resources/application.properties b/mse-simple-demo/A/src/main/resources/application.properties index fe35661a..90e96bb0 100644 --- a/mse-simple-demo/A/src/main/resources/application.properties +++ b/mse-simple-demo/A/src/main/resources/application.properties @@ -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 diff --git a/mse-simple-demo/B/build.sh b/mse-simple-demo/B/build.sh index c021cb21..c6722eb8 100755 --- a/mse-simple-demo/B/build.sh +++ b/mse-simple-demo/B/build.sh @@ -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 diff --git a/mse-simple-demo/B/pom.xml b/mse-simple-demo/B/pom.xml index a20f14dc..8286610d 100644 --- a/mse-simple-demo/B/pom.xml +++ b/mse-simple-demo/B/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 2.2.0.RELEASE @@ -22,7 +22,7 @@ UTF-8 UTF-8 1.8 - Greenwich.SR3 + Hoxton.SR1 @@ -67,6 +67,42 @@ 3.12.0 + + com.baomidou + mybatis-plus-boot-starter + 3.5.3 + + + + com.alibaba + druid-spring-boot-starter + 1.2.15 + + + + mysql + mysql-connector-java + 8.0.29 + + + + org.projectlombok + lombok + 1.18.24 + + + + org.springframework.cloud + spring-cloud-starter-openfeign + 2.2.5.RELEASE + + + + org.springframework.cloud + spring-cloud-starter-openfeign + 2.2.5.RELEASE + + diff --git a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BApplication.java b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BApplication.java index e2cad0b2..f72f65fe 100644 --- a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BApplication.java +++ b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BApplication.java @@ -2,10 +2,12 @@ package com.alibabacloud.mse.demo.b; import org.apache.commons.lang3.StringUtils; +import org.mybatis.spring.annotation.MapperScan; 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.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; @@ -20,6 +22,8 @@ import java.util.Properties; @SpringBootApplication +@MapperScan("com.alibabacloud.mse.demo.mapper") +@EnableFeignClients public class BApplication { public static void main(String[] args) { diff --git a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BController.java b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BController.java index dd5dc8f5..00cb0803 100644 --- a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BController.java +++ b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/BController.java @@ -1,6 +1,8 @@ package com.alibabacloud.mse.demo.b; +import com.alibaba.fastjson.JSON; import com.alibabacloud.mse.demo.c.service.HelloServiceC; +import com.alibabacloud.mse.demo.entity.User; import org.apache.dubbo.config.annotation.Reference; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; @@ -18,6 +20,7 @@ import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; +import java.util.List; import java.util.Random; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -69,48 +72,24 @@ private void init() { public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException { long sleepTime = 5 + RANDOM.nextInt(5); silentSleep(sleepTime); - - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime; + String result = loadBalancedRestTemplate.getForObject("http://sc-C/flow", String.class); + return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " sleepTime:" + sleepTime + " -> " + result; } @GetMapping("/params/{hot}") - public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException { + public String params(@PathVariable("hot") String hot) throws ExecutionException, InterruptedException { long sleepTime = 5 + RANDOM.nextInt(5); silentSleep(sleepTime); - helloServiceC.hello(hot); - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime+":"+hot; + String result = loadBalancedRestTemplate.getForObject("http://sc-C/params/" + hot, String.class); + return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " sleepTime:" + sleepTime + " params:" + hot + " -> " + result; } @GetMapping("/isolate") public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException { long sleepTime = 500 + RANDOM.nextInt(5); silentSleep(sleepTime); - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime; - } - - - @GetMapping("/flow-c") - public String flow_c(HttpServletRequest request) throws ExecutionException, InterruptedException { - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + - helloServiceC.hello("B"); - } - - @GetMapping("/params-c/{hot}") - public String params_c(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException { - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + - helloServiceC.hello(hot); - } - - @GetMapping("/isolate-c") - public String isolate_c(HttpServletRequest request) throws ExecutionException, InterruptedException { - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + - helloServiceC.hello("B"); - } - - @GetMapping("/rpc-c") - public String rpc_c(HttpServletRequest request) throws ExecutionException, InterruptedException { - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + - helloServiceC.world("B"); + String result = loadBalancedRestTemplate.getForObject("http://sc-C/isolate", String.class); + return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " sleepTime:" + sleepTime + " -> " + result; } @GetMapping("/b") @@ -119,6 +98,11 @@ public String b(HttpServletRequest request) { loadBalancedRestTemplate.getForObject("http://sc-C/c", String.class); } + @GetMapping("/bByFeign") + public String bByFeign(String s) { + return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]"; + } + @GetMapping("/b-zone") public String bZone(HttpServletRequest request) { return "B" + serviceTag + "[" + currentZone + "]" + " -> " + @@ -131,6 +115,40 @@ public String spring_boot(HttpServletRequest request) { restTemplate.getForObject("http://sc-c:20003/spring_boot", String.class); } + @GetMapping("/sql") + public String sql(HttpServletRequest request) { + User user = new User(); + String command = request.getParameter("command"); + String result = ""; + switch (command) { + case "query": + user.setId(Long.parseLong(request.getParameter("id"))); + result = JSON.toJSONString(user.selectById()); + break; + case "insert": + user.setName(request.getParameter("name")); + user.setAge(Integer.parseInt(request.getParameter("age"))); + user.setEmail(request.getParameter("email")); + result = String.valueOf(user.insert()); + break; + case "delete": + user.setId(Long.parseLong(request.getParameter("id"))); + result = String.valueOf(user.deleteById()); + break; + case "update": + user.setId(Long.parseLong(request.getParameter("id"))); + user.setName(request.getParameter("name")); + user.setAge(Integer.parseInt(request.getParameter("age"))); + user.setEmail(request.getParameter("email")); + result = String.valueOf(user.updateById()); + break; + default: + List list = user.selectAll(); + result = JSON.toJSONString(list); + } + return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " result:" + result; + } + private void silentSleep(long ms) { try { diff --git a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/service/HelloServiceBImpl.java b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/service/HelloServiceBImpl.java index 449243fb..9c2e3f60 100644 --- a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/service/HelloServiceBImpl.java +++ b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/b/service/HelloServiceBImpl.java @@ -20,7 +20,7 @@ public class HelloServiceBImpl implements HelloServiceB { @Override public String hello(String name) { - return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + + return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " params:" + name + " -> " + helloServiceC.hello(name); } diff --git a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/entity/User.java b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/entity/User.java new file mode 100644 index 00000000..872e082b --- /dev/null +++ b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/entity/User.java @@ -0,0 +1,25 @@ +package com.alibabacloud.mse.demo.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.Data; + +/** + * @author yushan + * @date 2023年01月07日 + */ +@Data +@TableName("user") +public class User extends Model { + @TableId(type = IdType.AUTO) + private long id; + @TableField("name") + private String name; + @TableField("age") + private Integer age; + @TableField("email") + private String email; +} diff --git a/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/mapper/UserMapper.java b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/mapper/UserMapper.java new file mode 100644 index 00000000..4f8f8e23 --- /dev/null +++ b/mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/mapper/UserMapper.java @@ -0,0 +1,12 @@ +package com.alibabacloud.mse.demo.mapper; + +import com.alibabacloud.mse.demo.entity.User; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** + * @author yushan + * @date 2023年01月07日 + */ +public interface UserMapper extends BaseMapper { + +} diff --git a/mse-simple-demo/B/src/main/resources/application-druid.yml b/mse-simple-demo/B/src/main/resources/application-druid.yml new file mode 100644 index 00000000..5b5ac5ef --- /dev/null +++ b/mse-simple-demo/B/src/main/resources/application-druid.yml @@ -0,0 +1,17 @@ +spring: + datasource: + #1.JDBC + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://demo-mysql:3306/demo + username: root + password: root + druid: + #2.连接池配置 + #初始化连接池的连接数量 大小,最小,最大 + initial-size: 5 + min-idle: 5 + max-active: 20 + #配置获取连接等待超时的时间 + max-wait: 60000 + diff --git a/mse-simple-demo/B/src/main/resources/application.properties b/mse-simple-demo/B/src/main/resources/application.properties index a4675213..5bb66c6c 100644 --- a/mse-simple-demo/B/src/main/resources/application.properties +++ b/mse-simple-demo/B/src/main/resources/application.properties @@ -17,3 +17,5 @@ dubbo.consumer.check=false dubbo.scan.base-packages=com.alibabacloud.mse.demo dubbo.protocol.port=48080 dubbo.registry.check=false + +spring.profiles.active=druid diff --git a/mse-simple-demo/C/build.sh b/mse-simple-demo/C/build.sh index 743987b4..419e79fe 100755 --- a/mse-simple-demo/C/build.sh +++ b/mse-simple-demo/C/build.sh @@ -3,7 +3,7 @@ set -e cd "$(dirname "$0")" -docker build . -t ${REGISTRY}spring-cloud-c:1.2.0 +docker build --platform linux/amd64 . -t ${REGISTRY}spring-cloud-c:1.2.0 if [ -n "${REGISTRY}" ]; then docker push ${REGISTRY}spring-cloud-c:1.2.0 diff --git a/mse-simple-demo/C/pom.xml b/mse-simple-demo/C/pom.xml index 636ca69d..cf099386 100644 --- a/mse-simple-demo/C/pom.xml +++ b/mse-simple-demo/C/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 1.5.1.RELEASE @@ -22,14 +22,14 @@ UTF-8 UTF-8 1.8 - Greenwich.SR3 + Edgware.SR2 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery - 2.1.1.RELEASE + 1.5.0.RELEASE org.springframework.boot @@ -56,6 +56,12 @@ org.apache.dubbo dubbo 2.7.18 + + + io.netty + netty-all + + diff --git a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CApplication.java b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CApplication.java index 1f5b004f..8e99e2b3 100644 --- a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CApplication.java +++ b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CApplication.java @@ -1,6 +1,6 @@ package com.alibabacloud.mse.demo.c; -import org.apache.commons.lang3.StringUtils; +import com.alibaba.nacos.common.utils.StringUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; diff --git a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CController.java b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CController.java index b8132657..89f0aa65 100644 --- a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CController.java +++ b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CController.java @@ -1,7 +1,5 @@ package com.alibabacloud.mse.demo.c; -import com.alibabacloud.mse.demo.c.sql.User; -import com.alibabacloud.mse.demo.c.sql.UserDao; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; @@ -39,11 +37,6 @@ class CController { private static final Random RANDOM = new Random(); - @Autowired - private UserDao userDao; - - private AutoTask task = null; - @PostConstruct private void init() { try { @@ -61,74 +54,6 @@ private void init() { currentZone = e.getMessage(); } - if (task != null) { - return; - } - task = new AutoTask(); - task.start(); - } - - private class AutoTask extends Thread { - - private boolean stopped = false; - - public AutoTask() { - setName("ahas-mybatis-auto-task"); - } - - @Override - public void run() { - while (true) { - if (stopped) { - return; - } - try { - autoDo(); - } catch (Exception e) { - e.printStackTrace(); - } - try { - Thread.sleep(ThreadLocalRandom.current().nextInt(30, 50)); - } catch (InterruptedException e) { - // Ignore - } - } - } - - public void stopTask() { - stopped = true; - } - } - - @Transactional - public void autoDo() { - String username = "foo_test_$$" + ThreadLocalRandom.current().nextInt(0, 100000); - int rand = ThreadLocalRandom.current().nextInt(10000000); - userDao.addUser(username, "abc" + rand + "@ss.com"); - User user = userDao.findUserByName(username); - if (user != null) { - if (rand % 3 == 0) { - userDao.updateUsername(user.getId(), username + rand); - } - userDao.deleteUser(user.getId()); - } - -// try{ -// Class.forName("com.mysql.jdbc.Driver"); -// //2. 获得数据库连接 -// Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "", ""); -// //3.操作数据库,实现增删改查 -// Statement stmt = conn.createStatement(); -// ResultSet rs = stmt.executeQuery("SELECT username FROM user_test where username=" + username); -// //如果有数据,rs.next()返回true -// while(rs.next()){ -// System.out.println(rs.getString("username")); -// } -// -// }catch (Throwable e){ -// -// } - } @GetMapping("/c") @@ -157,21 +82,21 @@ public String spring_boot(HttpServletRequest request) { public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException { long sleepTime = 5 + RANDOM.nextInt(5); silentSleep(sleepTime); - return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime; + return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " sleepTime:" + sleepTime; } @GetMapping("/params/{hot}") - public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException { + public String params(@PathVariable("hot") String hot) throws ExecutionException, InterruptedException { long sleepTime = 5 + RANDOM.nextInt(5); silentSleep(sleepTime); - return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime+":"+hot; + return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " sleepTime:" + sleepTime + " params:" + hot; } @GetMapping("/isolate") public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException { long sleepTime = 5 + RANDOM.nextInt(5); silentSleep(sleepTime); - return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime; + return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " sleepTime:" + sleepTime; } private void silentSleep(long ms) { diff --git a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/interceptor/MyBatisSentinelBlockException.java b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/interceptor/MyBatisSentinelBlockException.java deleted file mode 100644 index 29ba7469..00000000 --- a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/interceptor/MyBatisSentinelBlockException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 1999-2019 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibabacloud.mse.demo.c.interceptor; - -import org.apache.ibatis.exceptions.PersistenceException; - -/** - * @author Eric Zhao - */ -public class MyBatisSentinelBlockException extends PersistenceException { - - public MyBatisSentinelBlockException() { - super(); - } - - public MyBatisSentinelBlockException(String message) { - super(message); - } - - public MyBatisSentinelBlockException(String message, Throwable cause) { - super(message, cause); - } - - public MyBatisSentinelBlockException(Throwable cause) { - super(cause); - } -} diff --git a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/service/HelloServiceCImpl.java b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/service/HelloServiceCImpl.java index ecaee35d..70dce533 100644 --- a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/service/HelloServiceCImpl.java +++ b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/service/HelloServiceCImpl.java @@ -39,7 +39,7 @@ public String hello(String name) { throw new RuntimeException(); } - String value = "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]"; + String value = "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " params:" + name; String invokerTag=""; String userData = RpcContext.getContext().getAttachment("__microservice_tag__"); if (!StringUtils.isEmpty(userData)) { diff --git a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/sql/User.java b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/sql/User.java deleted file mode 100644 index ae1ee6c6..00000000 --- a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/sql/User.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 1999-2019 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibabacloud.mse.demo.c.sql; - -/** - * @author Eric Zhao - */ -public class User { - - private Long id; - private String username; - private String email; - - public Long getId() { - return id; - } - - public User setId(Long id) { - this.id = id; - return this; - } - - public String getUsername() { - return username; - } - - public User setUsername(String username) { - this.username = username; - return this; - } - - public String getEmail() { - return email; - } - - public User setEmail(String email) { - this.email = email; - return this; - } - - @Override - public String toString() { - return "User{" + - "id=" + id + - ", username='" + username + '\'' + - ", email='" + email + '\'' + - '}'; - } - -} diff --git a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/sql/UserDao.java b/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/sql/UserDao.java deleted file mode 100644 index 0eda7613..00000000 --- a/mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/sql/UserDao.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2019 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibabacloud.mse.demo.c.sql; - -import java.util.List; - - -import org.apache.ibatis.annotations.Delete; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; -import org.apache.ibatis.annotations.Update; - -/** - * @author Eric Zhao - */ -@Mapper -public interface UserDao { - - @Select("SELECT * FROM user_test WHERE username = #{username}") - User findUserByName(@Param("username") String username); - - @Select("SELECT * FROM user_test") - List findAllUsers(); - - @Select("SELECT * FROM user_test WHERE id = #{id}") - User findUserById(@Param("id") Long id); - - @Insert("INSERT INTO user_test(username, email) VALUES(#{username}, #{email})") - int addUser(@Param("username") String username, @Param("email") String email); - - @Update("UPDATE user_test SET username = #{username} WHERE id = #{id}") - int updateUsername(@Param("id") Long id, @Param("username") String username); - - @Delete("DELETE from user_test WHERE id = #{id}") - void deleteUser(@Param("id") Long id); -} diff --git a/mse-simple-demo/C/src/main/resources/application.properties b/mse-simple-demo/C/src/main/resources/application.properties index c65e4f62..db395997 100644 --- a/mse-simple-demo/C/src/main/resources/application.properties +++ b/mse-simple-demo/C/src/main/resources/application.properties @@ -4,7 +4,6 @@ nacos.namespace=public spring.application.name=sc-C server.port=20003 - spring.cloud.nacos.discovery.server-addr=${nacos.host}:8848 spring.cloud.nacos.discovery.namespace=${nacos.namespace} @@ -21,12 +20,4 @@ dubbo.registry.check=false rocketmq.consumer.topic=TEST_MQ rocketmq.consumer.group=test-c -middleware.mq.address=mqnamesrv:9876 - -spring.datasource.url=jdbc:h2:mem:test -spring.datasource.username= -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.schema=classpath:db/schema.sql - -mybatis.config-location=classpath:mybatis-config.xml \ No newline at end of file +middleware.mq.address=mqnamesrv:9876 \ No newline at end of file diff --git a/mse-simple-demo/C/src/main/resources/db/schema.sql b/mse-simple-demo/C/src/main/resources/db/schema.sql deleted file mode 100644 index bc6bc9ed..00000000 --- a/mse-simple-demo/C/src/main/resources/db/schema.sql +++ /dev/null @@ -1,9 +0,0 @@ -DROP TABLE IF EXISTS `user_test`; -CREATE TABLE `user_test` ( - `id` bigint(20) NOT NULL AUTO_INCREMENT, - `username` varchar(60) NOT NULL, - `email` varchar(128) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `username_UNIQUE` (`username`), - UNIQUE KEY `email_UNIQUE` (`email`) -) DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/mse-simple-demo/C/src/main/resources/mybatis-config.xml b/mse-simple-demo/C/src/main/resources/mybatis-config.xml deleted file mode 100644 index bab36c6d..00000000 --- a/mse-simple-demo/C/src/main/resources/mybatis-config.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/mse-simple-demo/gateway/build.sh b/mse-simple-demo/gateway/build.sh index 4c432b7b..047fda3c 100755 --- a/mse-simple-demo/gateway/build.sh +++ b/mse-simple-demo/gateway/build.sh @@ -3,7 +3,7 @@ set -e cd "$(dirname "$0")" -docker build . -t ${REGISTRY}spring-cloud-zuul:1.2.0 +docker build --platform linux/amd64 . -t ${REGISTRY}spring-cloud-zuul:1.2.0 if [ -n "${REGISTRY}" ]; then docker push ${REGISTRY}spring-cloud-zuul:1.2.0 diff --git a/mse-simple-demo/gateway/src/main/java/com/alibabacloud/mse/demo/DemoController.java b/mse-simple-demo/gateway/src/main/java/com/alibabacloud/mse/demo/DemoController.java index 7c562169..0fc0a452 100644 --- a/mse-simple-demo/gateway/src/main/java/com/alibabacloud/mse/demo/DemoController.java +++ b/mse-simple-demo/gateway/src/main/java/com/alibabacloud/mse/demo/DemoController.java @@ -15,7 +15,7 @@ import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; -import java.time.LocalDateTime; +import java.util.Random; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; @@ -31,12 +31,23 @@ public class DemoController { @Value("${demo.qps:100}") private int qps; - @Value("${enable.mq.invoke:false}") - private boolean enableMqInvoke; + @Value("${enable.rpc.invoke:false}") + private boolean enableRpcInvoke; @Value("${background.color:white}") private String backgroundColor; + @Value("${enable.sql:false}") + private boolean enableSql; + + @Value("${enable.auto:false}") + private boolean enableAuto; + + @Value("${enable.sentinel.demo.flow:false}") + private boolean enableSentinelFlow; + + private static Random random = new Random(); + private static final ScheduledExecutorService FLOW_EXECUTOR = Executors.newScheduledThreadPool(16, new ThreadFactory() { @Override @@ -63,7 +74,7 @@ public String spring_boot(HttpServletRequest request) { @PostConstruct private void flow() { - if (qps > 0) { + if (qps > 0 && enableAuto) { FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { @Override public void run() { @@ -77,13 +88,12 @@ public void run() { } }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); - FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { @Override public void run() { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/a?name=xiaoming"); + HttpGet httpGet = new HttpGet("http://localhost:20000/A/aByFeign"); httpClient.execute(httpGet); } catch (Exception ignore) { @@ -91,14 +101,12 @@ public void run() { } }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); - FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { @Override public void run() { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/a"); - httpGet.addHeader("x-mse-tag", "gray"); + HttpGet httpGet = new HttpGet("http://localhost:20000/A/a2bc"); httpClient.execute(httpGet); } catch (Exception ignore) { @@ -106,169 +114,198 @@ public void run() { } }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); - // 限流降级的流量发起 + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { @Override public void run() { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/flow"); + HttpGet httpGet = new HttpGet("http://localhost:20000/A/a?name=xiaoming"); httpClient.execute(httpGet); } catch (Exception ignore) { } + } + }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); + + + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/flow"); + HttpGet httpGet = new HttpGet("http://localhost:20000/A/a"); httpGet.addHeader("x-mse-tag", "gray"); httpClient.execute(httpGet); } catch (Exception ignore) { } + } + }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-flow"); - httpClient.execute(httpGet); + // 限流降级的流量发起 + if (enableSentinelFlow) { + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/flow"); + httpClient.execute(httpGet); - } catch (Exception ignore) { - } - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-flow"); - httpGet.addHeader("x-mse-tag", "gray"); - httpClient.execute(httpGet); + } catch (Exception ignore) { + } - } catch (Exception ignore) { - } + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/flow"); + httpGet.addHeader("x-mse-tag", "gray"); + httpClient.execute(httpGet); - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-isolate"); - httpClient.execute(httpGet); + } catch (Exception ignore) { + } - } catch (Exception ignore) { - } - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-isolate"); - httpGet.addHeader("x-mse-tag", "gray"); - httpClient.execute(httpGet); + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-flow"); + httpClient.execute(httpGet); - } catch (Exception ignore) { - } + } catch (Exception ignore) { + } + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-flow"); + httpGet.addHeader("x-mse-tag", "gray"); + httpClient.execute(httpGet); -// -// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { -// HttpGet httpGet = new HttpGet("http://localhost:20000/B/flow-c"); -// httpClient.execute(httpGet); -// -// } catch (Exception ignore) { -// } -// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { -// HttpGet httpGet = new HttpGet("http://localhost:20000/B/flow-c"); -// httpGet.addHeader("x-mse-tag", "gray"); -// httpClient.execute(httpGet); -// -// } catch (Exception ignore) { -// } -// -// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { -// HttpGet httpGet = new HttpGet("http://localhost:20000/B/isolate-c"); -// httpClient.execute(httpGet); -// -// } catch (Exception ignore) { -// } -// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { -// HttpGet httpGet = new HttpGet("http://localhost:20000/B/isolate-c"); -// httpGet.addHeader("x-mse-tag", "gray"); -// httpClient.execute(httpGet); -// -// } catch (Exception ignore) { -// } -// -// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { -// HttpGet httpGet = new HttpGet("http://localhost:20000/B/params-c/hot"); -// httpClient.execute(httpGet); -// -// } catch (Exception ignore) { -// } -// try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { -// HttpGet httpGet = new HttpGet("http://localhost:20000/B/params-c/hot"); -// httpGet.addHeader("x-mse-tag", "gray"); -// httpClient.execute(httpGet); -// -// } catch (Exception ignore) { -// } - } - }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); + } catch (Exception ignore) { + } - // region 热点限流 - FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-params/hot"); - httpClient.execute(httpGet); - } catch (Exception ignore) { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-isolate"); + httpClient.execute(httpGet); + + } catch (Exception ignore) { + } + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-isolate"); + httpGet.addHeader("x-mse-tag", "gray"); + httpClient.execute(httpGet); + + } catch (Exception ignore) { + } } - } - }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); - // endregion 热点限流 + }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); - // region 隔离规则 - for (int i = 0; i < 8; i++) { - int finalI = i; + // region 热点限流 FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { @Override public void run() { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/isolate?i_id=" + finalI); + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-params/hot"); + httpClient.execute(httpGet); + } catch (Exception ignore) { + } + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/params/hot"); httpClient.execute(httpGet); + } catch (Exception ignore) { } } }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); + // endregion 热点限流 + + // region 隔离规则 + for (int i = 0; i < 8; i++) { + int finalI = i; + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/isolate?i_id=" + finalI); + httpClient.execute(httpGet); + } catch (Exception ignore) { + } + + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo-isolate?i_id=" + finalI); + httpClient.execute(httpGet); + } catch (Exception ignore) { + } + } + }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); + } + // endregion 隔离规则 } - // endregion 隔离规则 - } + if (enableSql) { + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { - if (enableMqInvoke) { - FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + int id = random.nextInt(5) + 1; + HttpGet httpGet = new HttpGet("http://localhost:20000/A/sql?command=query&id=" + id); + httpClient.execute(httpGet); + } catch (Exception ignore) { + } + } + }, 5000000, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo"); - httpClient.execute(httpGet); + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { - } catch (Exception ignore) { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGetUpdate = new HttpGet("http://localhost:20000/A/sql?command=update&name=name1Update&email=emailUpdate@demo.com&age=1&id=1"); + httpClient.execute(httpGetUpdate); + TimeUnit.MILLISECONDS.sleep(500); + HttpGet httpGetUpdate2 = new HttpGet("http://localhost:20000/A/sql?command=update&name=name1&email=email@demo.com&age=1&id=1"); + httpClient.execute(httpGetUpdate2); + } catch (Exception ignore) { + } } - } - }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); + }, 5000000, 50 * 1000000 / qps, TimeUnit.MICROSECONDS); + } + if (enableRpcInvoke) { + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { - FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo"); + httpClient.execute(httpGet); - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo?name=xiaoming"); - httpClient.execute(httpGet); - } catch (Exception ignore) { + } catch (Exception ignore) { + } } - } - }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); + }, 100, 1000000 / qps, TimeUnit.MICROSECONDS); - FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo"); - httpGet.addHeader("x-mse-tag", "gray"); - httpClient.execute(httpGet); + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { - } catch (Exception ignore) { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo?name=xiaoming"); + httpClient.execute(httpGet); + } catch (Exception ignore) { + } } - } - }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); + }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); + + FLOW_EXECUTOR.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGet = new HttpGet("http://localhost:20000/A/dubbo"); + httpGet.addHeader("x-mse-tag", "gray"); + httpClient.execute(httpGet); + + } catch (Exception ignore) { + } + } + }, 100, 10 * 1000000 / qps, TimeUnit.MICROSECONDS); + } } } } diff --git a/mse-simple-demo/helm/mse-simple-demo/Chart.yaml b/mse-simple-demo/helm/mse-simple-demo/Chart.yaml index 1786e8c9..580c73f4 100644 --- a/mse-simple-demo/helm/mse-simple-demo/Chart.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/Chart.yaml @@ -1,7 +1,7 @@ apiVersion: v2 name: mse-simple-demo -version: "1.2.0" -appVersion: "1.2.0" +version: "2.0.1" +appVersion: "2.0.1" home: https://mse.console.aliyun.com/ keywords: - mse diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/gateway-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/gateway-deployment.yaml index 12c052e8..91ca817f 100644 --- a/mse-simple-demo/helm/mse-simple-demo/templates/gateway-deployment.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/templates/gateway-deployment.yaml @@ -12,27 +12,37 @@ spec: metadata: labels: msePilotAutoEnable: "on" - msePilotCreateAppName: "spring-cloud-zuul" + msePilotCreateAppName: "{{ .Values.mse.application.prefix}}-spring-cloud-zuul" mseNamespace: "{{ .Values.mse.namespace }}" app: spring-cloud-zuul spec: containers: - - image: "{{ .Values.registry }}spring-cloud-zuul:1.2.0" + - image: "{{ .Values.registry }}spring-cloud-zuul:{{ .Values.images.version }}" imagePullPolicy: Always env: - name: nacos.host value: "{{ .Values.nacos.host }}" - name: nacos.namespace value: "{{ .Values.nacos.namespace }}" + - name: enable.auto + value: "true" + - name: enable.rpc.invoke + value: "true" + - name: enable.sql + value: "true" + - name: enable.sentinel.demo.flow + value: "true" {{ if .Values.MSE_DEBUG_HOST }} - name: MSE_DEBUG_HOST value: {{ .Values.MSE_DEBUG_HOST }} {{ end }} name: "spring-cloud-zuul" resources: + {{ if .Values.resources.enableRequests }} requests: cpu: 1 memory: 2Gi + {{ end }} limits: cpu: 1 memory: 2Gi diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/mysql-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/mysql-deployment.yaml new file mode 100644 index 00000000..f95cfea8 --- /dev/null +++ b/mse-simple-demo/helm/mse-simple-demo/templates/mysql-deployment.yaml @@ -0,0 +1,27 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: demo-mysql +spec: + selector: + matchLabels: + app: demo-mysql + replicas: 1 + strategy: + type: Recreate + template: + metadata: + labels: + app: demo-mysql + spec: + containers: + - args: + - --character-set-server=utf8mb4 + - --collation-server=utf8mb4_unicode_ci + env: + - name: MYSQL_ROOT_PASSWORD + value: root + image: {{ .Values.registry }}demo-mysql:{{ .Values.images.version }} + name: demo-mysql + ports: + - containerPort: 3306 \ No newline at end of file diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/mysql-service.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/mysql-service.yaml new file mode 100644 index 00000000..eda0db25 --- /dev/null +++ b/mse-simple-demo/helm/mse-simple-demo/templates/mysql-service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: demo-mysql + name: demo-mysql +spec: + ports: + - port: 3306 + targetPort: 3306 + selector: + app: demo-mysql \ No newline at end of file diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-deployment.yaml index dd3be54a..df105070 100644 --- a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-deployment.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-deployment.yaml @@ -12,13 +12,13 @@ spec: metadata: labels: msePilotAutoEnable: "on" - msePilotCreateAppName: "spring-cloud-a" + msePilotCreateAppName: "{{ .Values.mse.application.prefix}}-spring-cloud-a" mseNamespace: "{{ .Values.mse.namespace }}" app: "spring-cloud-a" spec: containers: - name: spring-cloud-a - image: "{{ .Values.registry }}spring-cloud-a:1.2.0" + image: "{{ .Values.registry }}spring-cloud-a:{{ .Values.images.version }}" imagePullPolicy: Always env: - name: nacos.host @@ -30,9 +30,11 @@ spec: value: {{ .Values.MSE_DEBUG_HOST }} {{ end }} resources: + {{ if .Values.resources.enableRequests }} requests: cpu: 1 memory: 2Gi + {{ end }} limits: cpu: 1 memory: 2Gi diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-gray-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-gray-deployment.yaml index ebac234d..5cb7eee0 100644 --- a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-gray-deployment.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-a-gray-deployment.yaml @@ -13,7 +13,7 @@ spec: metadata: labels: msePilotAutoEnable: "on" - msePilotCreateAppName: "spring-cloud-a" + msePilotCreateAppName: "{{ .Values.mse.application.prefix}}-spring-cloud-a" mseNamespace: "{{ .Values.mse.namespace }}" alicloud.service.tag: gray app: "spring-cloud-a" @@ -21,7 +21,7 @@ spec: spec: containers: - name: spring-cloud-a - image: "{{ .Values.registry }}spring-cloud-a:1.2.0" + image: "{{ .Values.registry }}spring-cloud-a:{{ .Values.images.version }}" imagePullPolicy: Always env: - name: nacos.host @@ -33,9 +33,11 @@ spec: value: {{ .Values.MSE_DEBUG_HOST }} {{ end }} resources: + {{ if .Values.resources.enableRequests }} requests: cpu: 1 memory: 2Gi + {{ end }} limits: cpu: 1 memory: 2Gi diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-b-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-b-deployment.yaml index 0b81e1cc..33ce54dd 100644 --- a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-b-deployment.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-b-deployment.yaml @@ -12,13 +12,13 @@ spec: metadata: labels: msePilotAutoEnable: "on" - msePilotCreateAppName: "spring-cloud-b" + msePilotCreateAppName: "{{ .Values.mse.application.prefix}}-spring-cloud-b" mseNamespace: "{{ .Values.mse.namespace }}" app: "spring-cloud-b" spec: containers: - name: spring-cloud-b - image: "{{ .Values.registry }}spring-cloud-b:1.2.0" + image: "{{ .Values.registry }}spring-cloud-b:{{ .Values.images.version }}" imagePullPolicy: Always env: - name: nacos.host @@ -30,9 +30,11 @@ spec: value: {{ .Values.MSE_DEBUG_HOST }} {{ end }} resources: + {{ if .Values.resources.enableRequests }} requests: cpu: 1 memory: 2Gi + {{ end }} limits: cpu: 1 memory: 2Gi diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-deployment.yaml index 6ffcf314..8d7d26c0 100644 --- a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-deployment.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-deployment.yaml @@ -12,13 +12,13 @@ spec: metadata: labels: msePilotAutoEnable: "on" - msePilotCreateAppName: "spring-cloud-c" + msePilotCreateAppName: "{{ .Values.mse.application.prefix}}-spring-cloud-c" mseNamespace: "{{ .Values.mse.namespace }}" app: "spring-cloud-c" spec: containers: - name: spring-cloud-c - image: "{{ .Values.registry }}spring-cloud-c:1.2.0" + image: "{{ .Values.registry }}spring-cloud-c:{{ .Values.images.version }}" imagePullPolicy: Always env: - name: nacos.host @@ -30,9 +30,11 @@ spec: value: {{ .Values.MSE_DEBUG_HOST }} {{ end }} resources: + {{ if .Values.resources.enableRequests }} requests: cpu: 1 memory: 2Gi + {{ end }} limits: cpu: 1 memory: 2Gi diff --git a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-gray-deployment.yaml b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-gray-deployment.yaml index 4973dc9f..8e1ca18f 100644 --- a/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-gray-deployment.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/templates/spring-cloud-c-gray-deployment.yaml @@ -13,7 +13,7 @@ spec: metadata: labels: msePilotAutoEnable: "on" - msePilotCreateAppName: "spring-cloud-c" + msePilotCreateAppName: "{{ .Values.mse.application.prefix}}-spring-cloud-c" mseNamespace: "{{ .Values.mse.namespace }}" alicloud.service.tag: gray app: "spring-cloud-c" @@ -21,7 +21,7 @@ spec: spec: containers: - name: spring-cloud-c - image: "{{ .Values.registry }}spring-cloud-c:1.2.0" + image: "{{ .Values.registry }}spring-cloud-c:{{ .Values.images.version }}" imagePullPolicy: Always env: - name: nacos.host @@ -33,9 +33,11 @@ spec: value: {{ .Values.MSE_DEBUG_HOST }} {{ end }} resources: + {{ if .Values.resources.enableRequests }} requests: cpu: 1 memory: 2Gi + {{ end }} limits: cpu: 1 memory: 2Gi diff --git a/mse-simple-demo/helm/mse-simple-demo/values.yaml b/mse-simple-demo/helm/mse-simple-demo/values.yaml index 5dcd75ab..7e381ab0 100644 --- a/mse-simple-demo/helm/mse-simple-demo/values.yaml +++ b/mse-simple-demo/helm/mse-simple-demo/values.yaml @@ -1,8 +1,13 @@ # 镜像地址的前缀 -registry: registry.cn-shanghai.aliyuncs.com/yizhan/ +registry: registry.cn-hangzhou.aliyuncs.com/mse-demo-hz/ + +images: + version: mse-2.0.1 mse: - namespace: default + namespace: mse-demo + application: + prefix: demo nacos: # Nacos地址 @@ -11,3 +16,6 @@ nacos: namespace: public autoscaling: false + +resources: + enableRequests: false \ No newline at end of file