diff --git a/basic/consumer/src/main/resources/bootstrap.yml b/basic/consumer/src/main/resources/bootstrap.yml index 0852eee..65d7c20 100644 --- a/basic/consumer/src/main/resources/bootstrap.yml +++ b/basic/consumer/src/main/resources/bootstrap.yml @@ -40,4 +40,4 @@ spring: customLabel: ${spring.application.name} customLabelValue: ${INSTANCE_TAG:default} # 自定义配置,使用文本的key/value配置项作为yaml格式配置 - fileSource: governance.yaml,application.yaml + fileSource: governance.yaml,application.yaml \ No newline at end of file diff --git a/basic/pom.xml b/basic/pom.xml index c62e56d..402afba 100644 --- a/basic/pom.xml +++ b/basic/pom.xml @@ -63,4 +63,4 @@ - + \ No newline at end of file diff --git a/canary/canary-consumer/pom.xml b/canary/canary-consumer/pom.xml index e767bef..83a2a25 100644 --- a/canary/canary-consumer/pom.xml +++ b/canary/canary-consumer/pom.xml @@ -20,6 +20,14 @@ com.huaweicloud spring-cloud-starter-huawei-service-engine + + org.apache.servicecomb + provider-springmvc + + + org.springframework.cloud + spring-cloud-starter-openfeign + diff --git a/canary/canary-consumer/src/main/java/com/huaweicloud/sample/Application.java b/canary/canary-consumer/src/main/java/com/huaweicloud/sample/Application.java index 7b693a6..513c965 100644 --- a/canary/canary-consumer/src/main/java/com/huaweicloud/sample/Application.java +++ b/canary/canary-consumer/src/main/java/com/huaweicloud/sample/Application.java @@ -4,11 +4,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient +@EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/canary/canary-consumer/src/main/java/com/huaweicloud/sample/ConsumerController.java b/canary/canary-consumer/src/main/java/com/huaweicloud/sample/ConsumerController.java index 42f1619..63f3540 100644 --- a/canary/canary-consumer/src/main/java/com/huaweicloud/sample/ConsumerController.java +++ b/canary/canary-consumer/src/main/java/com/huaweicloud/sample/ConsumerController.java @@ -1,19 +1,47 @@ package com.huaweicloud.sample; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + import org.springframework.beans.factory.annotation.Autowired; +import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.RestTemplate; +import com.alibaba.fastjson.JSONObject; import com.netflix.config.DynamicPropertyFactory; @RestController +@RefreshScope public class ConsumerController { static Integer d = 0; @Autowired - private RestTemplate restTemplate; + private FeignThirdService feignThirdService; + + @Value("${cse.test:}") + private String test; + + @Autowired + private RestTemplate restTemplate; //cse调用 + + @Autowired + private RestTemplate restTemplate1 = RestTemplateBuilder.create(); //非CSE调用 @RequestMapping("/canary") public String getOrder(@RequestParam("id") String id) { @@ -21,12 +49,63 @@ public String getOrder(@RequestParam("id") String id) { return callServiceResult; } + @RequestMapping("/retry") + public String retry(@RequestParam("scode") String scode) { + String callServiceResult = restTemplate.getForObject("http://canary-provider/retry?scode=" + scode, String.class); + return callServiceResult; + } + + @RequestMapping("/cir") + public JSONObject cir(@RequestHeader(value = "times", required = false, defaultValue = "1") int times, + @RequestHeader(value = "failTimes", required = false, defaultValue = "0") int failTimes, + @RequestHeader(value = "statusCode", required = false, defaultValue = "0") int scode, HttpServletRequest request) { + JSONObject result = new JSONObject(); + Map map = new HashMap<>(); + Map params = new HashMap<>(); + restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){ + @Override + protected boolean hasError(HttpStatus statusCode) { + return false; + } + }); + for (int i = 0; i < times; i++) { + if (i < failTimes) { + HttpEntity entity = new HttpEntity<>(null,getHeaders(request)); + ResponseEntity responseEntity = restTemplate.exchange("http://canary-provider/cir?statusCode=" + scode, HttpMethod.GET, + entity,String.class); + map.put(i, responseEntity.getBody().toString()); + } else { + map.put(i,restTemplate.getForObject("http://canary-provider/cir?statusCode=" + 200, String.class,params)); + } + } + result.put("times",times); + result.put("map",map); + return result; + } + + @GetMapping("/sayHelloThird") + public String sayHelloThird(@RequestParam("name") String name) { + return restTemplate1.getForObject("http://localhost:8087/hello", String.class, name); + } + + @GetMapping("/sayHelloFeignThird") + public String sayHelloFeignThird(@RequestParam("name") String name) { + return feignThirdService.sayHelloFeignThird(name); + } + + @RequestMapping("/getConfig") public String getConfig() { - String result = DynamicPropertyFactory.getInstance() - .getStringProperty("servicecomb.routeRule.canary-provider", null, () -> { - ConsumerController.d++; - }).get(); - return result + " " + d; + return "test--> "+ test; + } + + public HttpHeaders getHeaders(HttpServletRequest request){ + HttpHeaders headers = new HttpHeaders(); + Enumeration er = request.getHeaderNames(); + while (er.hasMoreElements()){ + String key = er.nextElement().toString(); + headers.set(key,request.getHeader(key)); + } + return headers; } } diff --git a/canary/canary-consumer/src/main/java/com/huaweicloud/sample/FeignThirdService.java b/canary/canary-consumer/src/main/java/com/huaweicloud/sample/FeignThirdService.java new file mode 100644 index 0000000..4f2dd5a --- /dev/null +++ b/canary/canary-consumer/src/main/java/com/huaweicloud/sample/FeignThirdService.java @@ -0,0 +1,12 @@ +package com.huaweicloud.sample; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(value = "thirdService") +public interface FeignThirdService { + + @GetMapping("/hello") + String sayHelloFeignThird(@RequestParam("name") String name); +} diff --git a/canary/canary-consumer/src/main/resources/application.yaml b/canary/canary-consumer/src/main/resources/application.yaml index 2ea8100..6bc69fe 100644 --- a/canary/canary-consumer/src/main/resources/application.yaml +++ b/canary/canary-consumer/src/main/resources/application.yaml @@ -1,14 +1,2 @@ server: - port: 8091 - -servicecomb: - routeRule: - canary-provider: | - - precedence: 1 #优先级 - route: #路由规则 - - weight: 80 - tags: - version: 0.0.1 - - weight: 20 - tags: - version: 0.0.2 \ No newline at end of file + port: 8093 diff --git a/canary/canary-consumer/src/main/resources/bootstrap.yaml b/canary/canary-consumer/src/main/resources/bootstrap.yaml index 20f1a56..db0a1a6 100644 --- a/canary/canary-consumer/src/main/resources/bootstrap.yaml +++ b/canary/canary-consumer/src/main/resources/bootstrap.yaml @@ -1,18 +1,48 @@ spring: application: - # 微服务名称,本示例使用固定值(可替换成自己想要的名字),因为微服务名称会被客户端使用,不能轻易变化。 name: canary-consumer cloud: +# discovery: #第三方调用配置参考 用户可在8087端口启动第三方服务 +# client: +# simple: +# order: -100 +# instances: +# thirdService: +# - host: 127.0.0.1 +# port: 8087 +# instanceId: thirdService01 servicecomb: discovery: + appName: canary_demo # 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。 - appName: canary-application serviceName: ${spring.application.name} # 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。 address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100} # 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改版本号,防止契约注册失败。 version: ${CAS_INSTANCE_VERSION:0.0.1} - config: - # 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。 - serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110} - serverType: kie \ No newline at end of file + config: + serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110} + serverType: kie +#servicecomb: +# matchGroup: +# allOperation: | +# matches: +# - apiPath: +# prefix: "/" +# serviceName: thirdService +# instanceBulkhead: +# ## 隔离仓限制正在处理的请求数为5个,新来的请求等待1000毫秒没有获取到 +# ## 许可,将被拒绝。 +# allOperation: | +# maxConcurrentCalls: 5 +# maxWaitDuration: 1000 +# routeRule: +# canary-provider: | +# - precedence: 1 #优先级 +# route: #路由规则 +# - weight: 80 +# tags: +# version: 0.0.1 +# - weight: 20 +# tags: +# version: 0.0.2 \ No newline at end of file diff --git a/canary/canary-gateway/Dockerfile b/canary/canary-gateway/Dockerfile new file mode 100644 index 0000000..a6c0a83 --- /dev/null +++ b/canary/canary-gateway/Dockerfile @@ -0,0 +1,7 @@ +FROM openjdk:8u181-jdk-alpine + +WORKDIR /home/apps/ +ADD target/canary-gateway-1.0-SNAPSHOT.jar . +ADD start.sh . + +ENTRYPOINT ["sh", "/home/apps/start.sh"] \ No newline at end of file diff --git a/canary/canary-gateway/pom.xml b/canary/canary-gateway/pom.xml new file mode 100644 index 0000000..d794515 --- /dev/null +++ b/canary/canary-gateway/pom.xml @@ -0,0 +1,43 @@ + + + + canary-application + com.huaweicloud.samples + 1.0-SNAPSHOT + + 4.0.0 + + canary-gateway + jar + + + 8 + 8 + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + org.springframework.boot + spring-boot-starter-actuator + + + com.huaweicloud + spring-cloud-starter-huawei-service-engine-gateway + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/canary/canary-gateway/src/main/java/samples/GatewayApplication.java b/canary/canary-gateway/src/main/java/samples/GatewayApplication.java new file mode 100644 index 0000000..00b61c0 --- /dev/null +++ b/canary/canary-gateway/src/main/java/samples/GatewayApplication.java @@ -0,0 +1,34 @@ +/* + + * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. + + * 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 + * + * http://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 samples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class GatewayApplication { + public static void main(String[] args) throws Exception { + try { + SpringApplication.run(GatewayApplication.class, args); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/canary/canary-gateway/src/main/resources/bootstrap.yml b/canary/canary-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..b6043f0 --- /dev/null +++ b/canary/canary-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,46 @@ +# +## --------------------------------------------------------------------------- +## +## Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. +## +## 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 +## +## http://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. +## --------------------------------------------------------------------------- +server: + port: 8099 +spring: + application: + name: canary-gateway # 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。 + cloud: + gateway: + discovery: + locator: + enabled: true +# routes: +# -id : c + servicecomb: + discovery: + appName: canary_demo + # 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。 + serviceName: ${spring.application.name} + # 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。 + address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100} + # 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改版本号,防止契约注册失败。 + version: ${CAS_INSTANCE_VERSION:0.0.1} + config: + serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110} + serverType: kie + +# credentials: +# account: +# name: ${CSE_V2_ACCOUNT_NAME} +# password: ${CSE_V2_ACCOUNT_PASSWORD} diff --git a/canary/canary-gateway/start.sh b/canary/canary-gateway/start.sh new file mode 100644 index 0000000..53abf38 --- /dev/null +++ b/canary/canary-gateway/start.sh @@ -0,0 +1,8 @@ +JAR=canary-provider-beta-1.0-SNAPSHOT.jar +if [ ! -e $JAR ]; then + JAR=target/$JAR + if [ -e application.yaml ]; then + cp application.yaml ./target/ + fi +fi +java $CMDVAR -jar ./$JAR \ No newline at end of file diff --git a/canary/canary-provider-beta/src/main/resources/bootstrap.yaml b/canary/canary-provider-beta/src/main/resources/bootstrap.yaml index e077ef9..342fdba 100644 --- a/canary/canary-provider-beta/src/main/resources/bootstrap.yaml +++ b/canary/canary-provider-beta/src/main/resources/bootstrap.yaml @@ -1,5 +1,5 @@ server: - port: 8093 + port: 8094 spring: application: diff --git a/canary/canary-provider/pom.xml b/canary/canary-provider/pom.xml index d786927..931e1c2 100644 --- a/canary/canary-provider/pom.xml +++ b/canary/canary-provider/pom.xml @@ -16,6 +16,12 @@ org.springframework.boot spring-boot-starter-web + + + + + + com.huaweicloud spring-cloud-starter-huawei-service-engine diff --git a/canary/canary-provider/src/main/java/com/huaweicloud/sample/ProviderController.java b/canary/canary-provider/src/main/java/com/huaweicloud/sample/ProviderController.java index e0b2b36..e59f603 100644 --- a/canary/canary-provider/src/main/java/com/huaweicloud/sample/ProviderController.java +++ b/canary/canary-provider/src/main/java/com/huaweicloud/sample/ProviderController.java @@ -1,5 +1,7 @@ package com.huaweicloud.sample; +import javax.servlet.http.HttpServletResponse; + import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -12,11 +14,27 @@ @RestController public class ProviderController { - @Value("${server.port}") + @Value("${server.port:}") private Integer port; @RequestMapping("/provider") public String sayHello(@RequestParam("id") String id) { return "provider ---> " + id + " port -->" + port; } + + @RequestMapping("/retry") + public String retry(HttpServletResponse response, @RequestParam("scode") int scode) { + response.setStatus(scode); + return "retry"; + } + + @RequestMapping("/cir") + public String cir(HttpServletResponse response, @RequestParam("statusCode") int statusCode) { + if (statusCode == 200) { + return "ok"; + } else { + response.setStatus(statusCode); + return "err code is" + statusCode; + } + } } diff --git a/canary/canary-provider/src/main/resources/application.yaml b/canary/canary-provider/src/main/resources/application.yaml new file mode 100644 index 0000000..5d02e12 --- /dev/null +++ b/canary/canary-provider/src/main/resources/application.yaml @@ -0,0 +1,14 @@ +server: + port: 8092 + +#servicecomb: +# routeRule: +# canary-provider: | +# - precedence: 1 #优先级 +# route: #路由规则 +# - weight: 80 +# tags: +# version: 0.0.1 +# - weight: 20 +# tags: +# version: 0.0.2 \ No newline at end of file diff --git a/canary/pom.xml b/canary/pom.xml index 4195ca9..6550a18 100644 --- a/canary/pom.xml +++ b/canary/pom.xml @@ -11,13 +11,14 @@ 2.6.7 2021.0.3 - 1.10.0-2021.0.x + 1.10.8-2021.0.x canary-consumer canary-provider canary-provider-beta + canary-gateway