You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/03/30 06:26:50 UTC
[servicecomb-java-chassis] branch master updated:
[SCB-1831]Loadbalancer set endpoint can use Endpoint other the literal
string
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new bf71c2e [SCB-1831]Loadbalancer set endpoint can use Endpoint other the literal string
bf71c2e is described below
commit bf71c2ebaa9f8de115baa58b3c9ef7188b36c6c4
Author: liubao <bi...@qq.com>
AuthorDate: Sat Mar 28 17:13:57 2020 +0800
[SCB-1831]Loadbalancer set endpoint can use Endpoint other the literal string
---
.../demo/springmvc/client/TestDateTimeSchema.java | 32 +++++++++++++++++++++-
.../loadbalance/LoadbalanceHandler.java | 22 ++++++++++-----
.../invocation/context/InvocationContext.java | 5 +---
3 files changed, 47 insertions(+), 12 deletions(-)
diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java
index 4762f36..e9b0374 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java
@@ -17,13 +17,19 @@
package org.apache.servicecomb.demo.springmvc.client;
+import java.net.URI;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
+import javax.ws.rs.core.Response.Status;
+
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.core.Transport;
import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.foundation.common.cache.VersionedCache;
@@ -34,6 +40,7 @@ import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@@ -120,7 +127,7 @@ public class TestDateTimeSchema implements CategorizedTestCase {
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")));
}
- private void testDateTimeSchemaMulticast() {
+ private void testDateTimeSchemaMulticast() throws Exception {
DiscoveryContext context = new DiscoveryContext();
VersionedCache serversVersionedCache = discoveryTree.discovery(context, "springmvctest", "springmvc", "0+");
List<String> enpoints = serversVersionedCache.data();
@@ -130,9 +137,20 @@ public class TestDateTimeSchema implements CategorizedTestCase {
invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, endpoint);
Date date = new Date();
TestMgr.check(date.getTime(), dateTimeSchemaWithContextInf.getDate(invocationContext, date).getTime());
+
+ invocationContext = new InvocationContext();
+ invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, parseEndpoint(endpoint));
+ date = new Date();
+ TestMgr.check(date.getTime(), dateTimeSchemaWithContextInf.getDate(invocationContext, date).getTime());
}
}
+ private Endpoint parseEndpoint(String endpointUri) throws Exception {
+ URI formatUri = new URI(endpointUri);
+ Transport transport = SCBEngine.getInstance().getTransportManager().findTransport(formatUri.getScheme());
+ return new Endpoint(transport, endpointUri);
+ }
+
private void testDateTimeSchemaMulticastRestTemplate() throws Exception {
DiscoveryContext context = new DiscoveryContext();
VersionedCache serversVersionedCache = discoveryTree.discovery(context, "springmvctest", "springmvc", "0+");
@@ -152,6 +170,18 @@ public class TestDateTimeSchema implements CategorizedTestCase {
restTemplate
.exchange("cse://springmvc/dateTime/getDate?date={1}", HttpMethod.GET,
entity, Date.class, dateValue).getBody().getTime());
+
+ entity = new CseHttpEntity<>(null);
+ invocationContext = new InvocationContext();
+ invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, parseEndpoint(endpoint));
+ entity.setContext(invocationContext);
+
+ date = new Date();
+ dateValue = RestObjectMapperFactory.getRestObjectMapper().convertToString(date);
+ TestMgr.check(date.getTime(),
+ restTemplate
+ .exchange("cse://springmvc/dateTime/getDate?date={1}", HttpMethod.GET,
+ entity, Date.class, dateValue).getBody().getTime());
}
}
}
diff --git a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
index a50225c..a1eea05 100644
--- a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
+++ b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
@@ -224,11 +224,7 @@ public class LoadbalanceHandler implements Handler {
}
}
- private boolean defineEndpointAndHandle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
- String endpointUri = invocation.getLocalContext(SERVICECOMB_SERVER_ENDPOINT);
- if (endpointUri == null) {
- return false;
- }
+ private Endpoint parseEndpoint(String endpointUri) throws Exception {
URI formatUri = new URI(endpointUri);
Transport transport = SCBEngine.getInstance().getTransportManager().findTransport(formatUri.getScheme());
if (transport == null) {
@@ -236,8 +232,20 @@ public class LoadbalanceHandler implements Handler {
throw new InvocationException(Status.BAD_REQUEST,
"the endpoint's transport is not found.");
}
- Endpoint endpoint = new Endpoint(transport, endpointUri);
- invocation.setEndpoint(endpoint);
+ return new Endpoint(transport, endpointUri);
+ }
+
+ private boolean defineEndpointAndHandle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
+ Object endpoint = invocation.getLocalContext(SERVICECOMB_SERVER_ENDPOINT);
+ if (endpoint == null) {
+ return false;
+ }
+ if (endpoint instanceof String) {
+ // compatible to old usage
+ endpoint = parseEndpoint((String) endpoint);
+ }
+
+ invocation.setEndpoint((Endpoint) endpoint);
invocation.next(resp -> {
asyncResp.handle(resp);
});
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
index 80e5a53..009fc94 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
@@ -24,17 +24,14 @@ import java.util.Map.Entry;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.StatusType;
-import org.slf4j.Marker;
-
/**
- * 设置特定的Cse Context数据
+ * InvocationContext is used to pass data between microservices or in microservice different layer.
*/
public class InvocationContext {
private static HttpStatusManager statusMgr = new HttpStatusManager();
protected StatusType httpStatus;
- // value只能是简单类型
protected Map<String, String> context = new HashMap<>();
protected Map<String, Object> localContext = new HashMap<>();