Skip to content

Commit

Permalink
sch demo update
Browse files Browse the repository at this point in the history
  • Loading branch information
lbc97 committed Feb 15, 2023
1 parent 8fce0d1 commit fcb5bc5
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 30 deletions.
2 changes: 1 addition & 1 deletion basic/consumer/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion basic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
</plugins>
</pluginManagement>
</build>
</project>
</project>
8 changes: 8 additions & 0 deletions canary/canary-consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-springmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
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;
}
}
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);
}
14 changes: 1 addition & 13 deletions canary/canary-consumer/src/main/resources/application.yaml
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
42 changes: 36 additions & 6 deletions canary/canary-consumer/src/main/resources/bootstrap.yaml
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
7 changes: 7 additions & 0 deletions canary/canary-gateway/Dockerfile
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"]
43 changes: 43 additions & 0 deletions canary/canary-gateway/pom.xml
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>
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();
}
}
}
46 changes: 46 additions & 0 deletions canary/canary-gateway/src/main/resources/bootstrap.yml
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}
8 changes: 8 additions & 0 deletions canary/canary-gateway/start.sh
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
Loading

0 comments on commit fcb5bc5

Please sign in to comment.