Skip to content

Commit

Permalink
[#3727]adjust router match same with governance and fix edge version …
Browse files Browse the repository at this point in the history
…rule test cases (#4009)
  • Loading branch information
liubao68 authored Nov 3, 2023
1 parent 4344043 commit ec57988
Show file tree
Hide file tree
Showing 26 changed files with 334 additions and 498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ private void onOpenAPIChanged(String application, String serviceName) {
executor.execute(() -> {
synchronized (referenceConfigsLocks.computeIfAbsent(parser.getAppId(), key -> new ConcurrentHashMapEx<>())
.computeIfAbsent(parser.getMicroserviceName(), key -> new Object())) {
MicroserviceReferenceConfig temp = referenceConfigs.get(parser.getAppId())
.get(parser.getMicroserviceName());
if (temp != null) {
try {
MicroserviceReferenceConfig temp = referenceConfigs.get(parser.getAppId())
.get(parser.getMicroserviceName());
if (temp != null) {
result.complete(temp);
return;
}
temp = buildMicroserviceReferenceConfig(scbEngine, parser.getAppId(),
parser.getMicroserviceName());
referenceConfigs.get(parser.getAppId()).put(parser.getMicroserviceName(), temp);
result.complete(temp);
return;
} catch (Exception e) {
result.completeExceptionally(e);
}
temp = buildMicroserviceReferenceConfig(scbEngine, parser.getAppId(),
parser.getMicroserviceName());
referenceConfigs.get(parser.getAppId()).put(parser.getMicroserviceName(), temp);
result.complete(temp);
}
});
return result;
Expand Down
20 changes: 20 additions & 0 deletions demo/demo-edge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# About edge service API compatibility

* Edge service use the latest version of the microservice meta. e.g. For business 1.0.0, 1.1.0, 2.0.0 have the following APIs:

* 1.0.0: /business/v1/add
* 1.1.0: /business/v1/add, /business/v1/dec
* 2.0.0: /business/v2/add, /business/v2/dec

If users invoke /business/v1/add, edge service will give NOT FOUND, because 2.0.0 microservice meta do not have this API. Even using router to route all /business/v1/* requests to 1.1.0, path locating happens before load balance.

* It's very important to keep your API compatibility cross versions if these versions need work together. e.g.

* 1.0.0: /business/v1/add
* 1.1.0: /business/v1/add, /business/v1/dec
* 2.0.0: /business/v1/add, /business/v1/dec, /business/v2/add, /business/v2/dec

Together with router, /business/v1/add will go correctly to 1.0.0 or 1.1.0, and /business/v2/add will go correctly to 2.0.0. Without router, /business/v2/add may route to 1.0.0 or 1.1.0 and NOT FOUND is reported.



Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,18 @@

package org.apache.servicecomb.demo.edge.business;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.servicecomb.demo.edge.model.AppClientDataRsp;
import org.apache.servicecomb.demo.edge.model.ChannelRequestBase;
import org.apache.servicecomb.demo.edge.model.DependTypeA;
import org.apache.servicecomb.demo.edge.model.RecursiveSelfType;
import org.apache.servicecomb.demo.edge.model.ResultWithInstance;
import org.apache.servicecomb.demo.edge.model.User;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

@RestSchema(schemaId = "news-v2")
@RequestMapping(path = "/business/v2")
@RestSchema(schemaId = "news-v1")
@RequestMapping(path = "/business/v1")
public class Impl {
private Environment environment;

Expand All @@ -58,12 +37,6 @@ public void setEnvironment(Environment environment) {
this.environment = environment;
}

File tempDir = new File("target/downloadTemp");

public Impl() throws IOException {
FileUtils.forceMkdir(tempDir);
}

@RequestMapping(path = "/channel/news/subscribe", method = RequestMethod.POST)
public AppClientDataRsp subscribeNewsColumn(@RequestBody ChannelRequestBase request) {
AppClientDataRsp response = new AppClientDataRsp();
Expand All @@ -81,45 +54,4 @@ public ResultWithInstance add(int x, int y) {
public ResultWithInstance dec(int x, int y) {
return ResultWithInstance.create(x - y, environment);
}

@GetMapping(path = "/download")
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = File.class)), description = ""),
})
public ResponseEntity<InputStream> download() throws IOException {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=download.txt")
.body(new ByteArrayInputStream("download".getBytes(StandardCharsets.UTF_8)));
}

protected File createBigFile() throws IOException {
File file = new File(tempDir, "bigFile.txt");
file.delete();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.setLength(10 * 1024 * 1024);
randomAccessFile.close();
return file;
}

@GetMapping(path = "/bigFile")
public File bigFile() throws IOException {
return createBigFile();
}

@PostMapping(path = "recursiveSelf")
public RecursiveSelfType recursiveSelf(@RequestBody RecursiveSelfType value) {
return value;
}

@PostMapping(path = "dependType")
public DependTypeA dependType(@RequestBody DependTypeA value) {
return value;
}

@PostMapping(path = "encrypt")
public User encrypt(@RequestBody User value) {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.servicecomb.demo.edge.business;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.servicecomb.demo.edge.model.AppClientDataRsp;
import org.apache.servicecomb.demo.edge.model.ChannelRequestBase;
import org.apache.servicecomb.demo.edge.model.DependTypeA;
import org.apache.servicecomb.demo.edge.model.RecursiveSelfType;
import org.apache.servicecomb.demo.edge.model.ResultWithInstance;
import org.apache.servicecomb.demo.edge.model.User;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

@RestSchema(schemaId = "news-v2")
@RequestMapping(path = "/business/v2")
public class ImplV2 {
private Environment environment;

@Autowired
public void setEnvironment(Environment environment) {
this.environment = environment;
}

File tempDir = new File("target/downloadTemp");

public ImplV2() throws IOException {
FileUtils.forceMkdir(tempDir);
}

@RequestMapping(path = "/channel/news/subscribe", method = RequestMethod.POST)
public AppClientDataRsp subscribeNewsColumn(@RequestBody ChannelRequestBase request) {
AppClientDataRsp response = new AppClientDataRsp();
String rsp = "result from 2.0.0";
response.setRsp(rsp);
return response;
}

@RequestMapping(path = "/add", method = RequestMethod.GET)
public ResultWithInstance add(int x, int y) {
return ResultWithInstance.create(x + y, environment);
}

@RequestMapping(path = "/dec", method = RequestMethod.GET)
public ResultWithInstance dec(int x, int y) {
return ResultWithInstance.create(x - y, environment);
}

@GetMapping(path = "/download")
@ApiResponses({
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = File.class)), description = ""),
})
public ResponseEntity<InputStream> download() throws IOException {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=download.txt")
.body(new ByteArrayInputStream("download".getBytes(StandardCharsets.UTF_8)));
}

protected File createBigFile() throws IOException {
File file = new File(tempDir, "bigFile.txt");
file.delete();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.setLength(10 * 1024 * 1024);
randomAccessFile.close();
return file;
}

@GetMapping(path = "/bigFile")
public File bigFile() throws IOException {
return createBigFile();
}

@PostMapping(path = "recursiveSelf")
public RecursiveSelfType recursiveSelf(@RequestBody RecursiveSelfType value) {
return value;
}

@PostMapping(path = "dependType")
public DependTypeA dependType(@RequestBody DependTypeA value) {
return value;
}

@PostMapping(path = "encrypt")
public User encrypt(@RequestBody User value) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.demo.edge.model.AppClientDataRsp;
import org.apache.servicecomb.demo.edge.model.ChannelRequestBase;
import org.apache.servicecomb.demo.edge.model.DependTypeA;
import org.apache.servicecomb.demo.edge.model.DependTypeB;
Expand All @@ -42,6 +43,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand All @@ -56,9 +58,9 @@ public class Consumer {

String edgePrefix;

// List<ResultWithInstance> addV1Result = new ArrayList<>();
List<ResultWithInstance> addV1Result = new ArrayList<>();

// List<ResultWithInstance> decV1Result = new ArrayList<>();
List<ResultWithInstance> decV1Result = new ArrayList<>();

List<ResultWithInstance> addV2Result = new ArrayList<>();

Expand Down Expand Up @@ -94,29 +96,28 @@ public void run(String prefix) {
testDownload();
testDownloadBigFile();
testErrorCode();
// TODO: we use router to test this feature.
// invoke("/v1/add", 2, 1, addV1Result);
// invoke("/v1/add", 3, 1, addV1Result);
// invoke("/v1/add", 4, 1, addV1Result);
// invoke("/v1/add", 5, 1, addV1Result);
//
// invoke("/v1/dec", 2, 1, decV1Result);
// invoke("/v1/dec", 3, 1, decV1Result);

invoke("/v1/add", 2, 1, addV1Result);
invoke("/v1/add", 3, 1, addV1Result);
invoke("/v1/add", 4, 1, addV1Result);
invoke("/v1/add", 5, 1, addV1Result);

invoke("/v1/dec", 2, 1, decV1Result);
invoke("/v1/dec", 3, 1, decV1Result);

invoke("/v2/add", 2, 1, addV2Result);
invoke("/v2/add", 3, 1, addV2Result);

invoke("/v2/dec", 2, 1, decV2Result);
invoke("/v2/dec", 3, 1, decV2Result);

// TODO: we use router to test this feature.
// printResults("v1/add", addV1Result);
// printResults("v1/dec", decV1Result);
printResults("v1/add", addV1Result);
printResults("v1/dec", decV1Result);
printResults("v2/add", addV2Result);
printResults("v2/dec", decV2Result);

// checkResult("v1/add", addV1Result, "1.0.0", "1.1.0");
// checkResult("v1/dec", decV1Result, "1.1.0");
checkResult("v1/add", addV1Result, "1.0.0", "1.1.0");
checkResult("v1/dec", decV1Result, "1.1.0");
checkResult("v2/add", addV2Result, "2.0.0");
checkResult("v2/dec", decV2Result, "2.0.0");
}
Expand Down Expand Up @@ -282,21 +283,20 @@ private URIEndpointObject prepareEdge(String prefix) {
}

protected void invokeBusiness(String urlPrefix, ChannelRequestBase request) {
// since 3.0.0, do not support this feature.
// after 3.0.0, the client will load schema once after startup and will never change, and there
// isn't version rule concept.

// TODO: we use router to test this feature.
// for (int i = 0; i < 3; i++) {
// String url = urlPrefix + "/channel/news/subscribe";
//
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
//
// HttpEntity<ChannelRequestBase> entity = new HttpEntity<>(request, headers);
//
// ResponseEntity<AppClientDataRsp> response = template.postForEntity(url, entity, AppClientDataRsp.class);
// Assert.isTrue(response.getBody().getRsp().equals("result from 1.1.0"), response.getBody().getRsp());
// }
List<String> result = new ArrayList<>(6);
for (int i = 0; i < 6; i++) {
String url = urlPrefix + "/channel/news/subscribe";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<ChannelRequestBase> entity = new HttpEntity<>(request, headers);

ResponseEntity<AppClientDataRsp> response = template.postForEntity(url, entity, AppClientDataRsp.class);
result.add(response.getBody().getRsp());
}
Assert.isTrue(result.contains("result from 2.0.0"), "invokeBusiness not balance");
Assert.isTrue(result.contains("result from 1.1.0"), "invokeBusiness not balance");
Assert.isTrue(result.contains("result from 1.0.0"), "invokeBusiness not balance");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public static void runTest() throws Exception {
new Consumer().run("rest");
System.out.println("Running url dispatcher.");
new Consumer().run("url");
System.out.println("Running http dispatcher.");
new Consumer().run("http");
// Common Http Dispatcher do not have OperationMeta, can not use router.
// System.out.println("Running http dispatcher.");
// new Consumer().run("http");

System.out.println("All test case finished.");

Expand Down
Loading

0 comments on commit ec57988

Please sign in to comment.