-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
326 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,4 +63,4 @@ | |
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 85 additions & 6 deletions
91
canary/canary-consumer/src/main/java/com/huaweicloud/sample/ConsumerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,111 @@ | ||
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) { | ||
String callServiceResult = restTemplate.getForObject("http://canary-provider/provider?id=" + id, String.class); | ||
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<Integer, String> map = new HashMap<>(); | ||
Map<String, Object> 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<ResponseEntity> 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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
canary/canary-consumer/src/main/java/com/huaweicloud/sample/FeignThirdService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
port: 8093 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>canary-application</artifactId> | ||
<groupId>com.huaweicloud.samples</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>canary-gateway</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-gateway</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.huaweicloud</groupId> | ||
<artifactId>spring-cloud-starter-huawei-service-engine-gateway</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
34 changes: 34 additions & 0 deletions
34
canary/canary-gateway/src/main/java/samples/GatewayApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.