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 d95dc03 [SCB-2365]handler configuration support *.hanlder.xm instead
of cse.*.handler.xml (#2666)
d95dc03 is described below
commit d95dc03bf10bf8648f7bbb1654dc01a680921707
Author: lbc97 <[email protected]>
AuthorDate: Fri Dec 24 14:07:25 2021 +0800
[SCB-2365]handler configuration support *.hanlder.xm instead of
cse.*.handler.xml (#2666)
---
.../core/handler/HandlerConfigUtils.java | 6 +-
.../servicecomb/demo/controller/Controller.java | 2 +
.../microservices/springmvc/controller.yaml | 23 ++++--
.../demo/springmvc/SpringmvcClient.java | 10 ++-
demo/demo-springmvc/springmvc-server/pom.xml | 4 +-
.../springmvc/handler/ProviderTestHandler.java} | 24 ++++--
.../demo/springmvc/server/ControllerImpl.java | 8 ++
.../demo/springmvc/server/WeakSpringmvc.java | 5 ++
.../src/main/resources/config/lbc.handler.xml | 21 ++++++
.../src/main/resources/microservice.yaml | 2 +-
.../common/config/PaaSResourceUtils.java | 10 ---
.../foundation/common/config/impl/XmlLoader.java | 87 ----------------------
12 files changed, 84 insertions(+), 118 deletions(-)
diff --git
a/core/src/main/java/org/apache/servicecomb/core/handler/HandlerConfigUtils.java
b/core/src/main/java/org/apache/servicecomb/core/handler/HandlerConfigUtils.java
index cbd354c..88c8dd8 100644
---
a/core/src/main/java/org/apache/servicecomb/core/handler/HandlerConfigUtils.java
+++
b/core/src/main/java/org/apache/servicecomb/core/handler/HandlerConfigUtils.java
@@ -31,8 +31,10 @@ public final class HandlerConfigUtils {
private static Config loadConfig() throws Exception {
Config config = new Config();
- List<Resource> resList =
-
PaaSResourceUtils.getSortedResources("classpath*:config/cse.handler.xml",
".handler.xml");
+ List<Resource> resList = PaaSResourceUtils.
+ getResources(new String[] {"classpath*:config/*.handler.xml"});
+ PaaSResourceUtils.sortResources(resList, ".handler.xml");
+
for (Resource res : resList) {
Config tmpConfig = XmlLoaderUtils.load(res, Config.class);
config.mergeFrom(tmpConfig);
diff --git
a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
index f8752fb..b82cd34 100644
---
a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
+++
b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
@@ -27,4 +27,6 @@ public interface Controller {
String sayHi(String name);
String sayHei(String name);
+
+ String sayHello1(String name);
}
diff --git
a/demo/demo-schema/src/main/resources/microservices/springmvc/controller.yaml
b/demo/demo-schema/src/main/resources/microservices/springmvc/controller.yaml
index 9bea47b..be2feb0 100644
---
a/demo/demo-schema/src/main/resources/microservices/springmvc/controller.yaml
+++
b/demo/demo-schema/src/main/resources/microservices/springmvc/controller.yaml
@@ -22,7 +22,7 @@ info:
title: rest test
version: 1.0.0
x-java-interface: org.apache.servicecomb.demo.controller.Controller
-
+
# the domain of the service
#host: api.uber.com
@@ -34,7 +34,7 @@ info:
basePath: /springmvc/controller
produces:
- application/json
-
+
paths:
/add:
get:
@@ -50,13 +50,13 @@ paths:
required: true
type: integer
format: int32
- responses:
+ responses:
"200":
description: add numer
- schema:
+ schema:
type: integer
format: int32
-
+
/sayhello/{name}:
post:
operationId: sayHello
@@ -114,6 +114,19 @@ paths:
description: say hei
schema:
type: string
+ /sayHello1:
+ get:
+ operationId: sayHello1
+ parameters:
+ - name: name
+ in: query
+ required: true
+ type: string
+ responses:
+ "200":
+ description: check the handler is effective
+ schema:
+ type: string
definitions:
Person:
diff --git
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcClient.java
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcClient.java
index 2bbd2b6..7a023c8 100644
---
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcClient.java
+++
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/SpringmvcClient.java
@@ -101,17 +101,23 @@ public class SpringmvcClient {
} catch (Exception e) {
TestMgr.check("true", "true");
}
-
+ testHandler(microserviceName);
CodeFirstRestTemplateSpringmvc codeFirstClient =
BeanUtils.getContext().getBean(CodeFirstRestTemplateSpringmvc.class);
codeFirstClient.testCodeFirst(restTemplate, "springmvc",
"/codeFirstSpringmvc/");
codeFirstClient.testCodeFirst(templateUrlWithProviderPrefix, "springmvc",
"/pojo/rest/codeFirstSpringmvc/");
-
testAllTransport(microserviceName);
testRestTransport(microserviceName, prefix);
CategorizedTestCaseRunner.runCategorizedTestCase(microserviceName);
}
+ private static void testHandler(String microserviceName) {
+ changeTransport(microserviceName, "rest");
+ String prefix = "cse://springmvc";
+ String result = templateUrlWithServiceName.getForObject(prefix +
"/controller/sayHello1?name=tom", String.class);
+ TestMgr.check("Hello tom,v", result);
+ }
+
private static void testHttpClientsIsOk() {
TestMgr.check(HttpClients.getClient("registry") != null, true);
TestMgr.check(HttpClients.getClient("registry-watch") != null, true);
diff --git a/demo/demo-springmvc/springmvc-server/pom.xml
b/demo/demo-springmvc/springmvc-server/pom.xml
index a27242d..2bac085 100644
--- a/demo/demo-springmvc/springmvc-server/pom.xml
+++ b/demo/demo-springmvc/springmvc-server/pom.xml
@@ -41,7 +41,7 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
</dependency>
- <dependency>
+ <dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-invocation-validator</artifactId>
</dependency>
@@ -49,12 +49,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>metrics-prometheus</artifactId>
</dependency>
-
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>config-cc</artifactId>
</dependency>
-
<dependency>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-signature</artifactId>
diff --git
a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/handler/ProviderTestHandler.java
similarity index 50%
copy from
demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
copy to
demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/handler/ProviderTestHandler.java
index f8752fb..e83f1c9 100644
---
a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/controller/Controller.java
+++
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/handler/ProviderTestHandler.java
@@ -14,17 +14,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package org.apache.servicecomb.demo.springmvc.handler;
-package org.apache.servicecomb.demo.controller;
-public interface Controller {
- int add(int a, int b);
+import org.apache.servicecomb.core.Handler;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
- String sayHello(String name);
- String saySomething(String prefix, Person user);
+public class ProviderTestHandler implements Handler {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(ProviderTestHandler.class);
- String sayHi(String name);
-
- String sayHei(String name);
+ @Override
+ public void handle(Invocation invocation, AsyncResponse asyncResp) throws
Exception {
+ invocation.addContext("k", "v");
+ invocation.next(asyncResp);
+ }
}
diff --git
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
index 483c4f0..8d7dfb3 100644
---
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
+++
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
@@ -26,6 +26,7 @@ import javax.ws.rs.core.Response.Status;
import org.apache.servicecomb.demo.controller.Person;
import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -36,6 +37,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
+import io.swagger.annotations.ApiOperation;
+
// This class tests "contract first", the controller.yaml will override
annotations defined in class.
@RestSchema(schemaId = "controller")
@@ -77,4 +80,9 @@ public class ControllerImpl {
public String sayHei(@RequestHeader("name") String name) {
return "hei " + name;
}
+
+ @RequestMapping(path = "/sayHello1", method = RequestMethod.GET)
+ public String sayHello1(@RequestParam("name") String name) {
+ return "Hello " + name + "," +
ContextUtils.getInvocationContext().getContext("k");
+ }
}
diff --git
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/WeakSpringmvc.java
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/WeakSpringmvc.java
index bb75d6a..35ee2dc 100644
---
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/WeakSpringmvc.java
+++
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/WeakSpringmvc.java
@@ -19,14 +19,18 @@ package org.apache.servicecomb.demo.springmvc.server;
import java.util.List;
+import javax.servlet.ServletContext;
+
import org.apache.servicecomb.demo.model.SpecialNameModel;
import org.apache.servicecomb.demo.server.GenericsModel;
import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.context.ContextLoader;
import io.swagger.annotations.ApiOperation;
@@ -56,4 +60,5 @@ public class WeakSpringmvc {
public SpecialNameModel specialNameModel(@RequestParam("code") int code,
@RequestBody SpecialNameModel model) {
return model;
}
+
}
diff --git
a/demo/demo-springmvc/springmvc-server/src/main/resources/config/lbc.handler.xml
b/demo/demo-springmvc/springmvc-server/src/main/resources/config/lbc.handler.xml
new file mode 100644
index 0000000..e9e3e15
--- /dev/null
+++
b/demo/demo-springmvc/springmvc-server/src/main/resources/config/lbc.handler.xml
@@ -0,0 +1,21 @@
+<!--
+ ~ 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.
+ -->
+
+<config>
+ <handler id="test-provider"
+ class="org.apache.servicecomb.demo.springmvc.handler.ProviderTestHandler"/>
+</config>
diff --git
a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
index 8696bff..5e3e9cb 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
@@ -73,7 +73,7 @@ servicecomb:
handler:
chain:
Provider:
- default: bizkeeper-provider, qps-flowcontrol-provider
+ default: test-provider, bizkeeper-provider, qps-flowcontrol-provider
flowcontrol:
strategy: MyStrategy
Provider:
diff --git
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/PaaSResourceUtils.java
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/PaaSResourceUtils.java
index 3325334..10a7345 100644
---
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/PaaSResourceUtils.java
+++
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/PaaSResourceUtils.java
@@ -34,8 +34,6 @@ import
org.springframework.core.io.support.ResourcePatternResolver;
public class PaaSResourceUtils extends org.springframework.util.ResourceUtils {
public static final String PROPERTIES_SUFFIX = ".properties";
- public static final String XML_SUFFIX = ".xml";
-
private static ResourcePatternResolver resourcePatternResolver = new
PathMatchingResourcePatternResolver();
/**
@@ -108,10 +106,6 @@ public class PaaSResourceUtils extends
org.springframework.util.ResourceUtils {
sortResources(resList, PROPERTIES_SUFFIX);
}
- public static void sortXmls(List<Resource> resList) {
- sortResources(resList, XML_SUFFIX);
- }
-
public static List<Resource> getSortedResources(String locationPattern,
String suffix) {
if (StringUtils.isEmpty(locationPattern)) {
throw new RuntimeException("Resource path must not be null or empty");
@@ -136,8 +130,4 @@ public class PaaSResourceUtils extends
org.springframework.util.ResourceUtils {
PropertiesLoader loader = new
PropertiesLoader(Arrays.asList(locationPattern));
return loader.load();
}
-
- public static List<Resource> getSortedXmls(String locationPattern) {
- return getSortedResources(locationPattern, XML_SUFFIX);
- }
}
diff --git
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/impl/XmlLoader.java
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/impl/XmlLoader.java
deleted file mode 100644
index 254b1ed..0000000
---
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/config/impl/XmlLoader.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * 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.foundation.common.config.impl;
-
-import java.util.List;
-
-import org.apache.servicecomb.foundation.common.config.PaaSResourceUtils;
-import org.springframework.core.io.Resource;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-/**
- * 无逻辑append xml
- */
-public class XmlLoader extends AbstractLoader {
-
- private String suffix;
-
- public XmlLoader(List<String> locationPatternList) {
- this(locationPatternList, PaaSResourceUtils.XML_SUFFIX);
- }
-
- public XmlLoader(List<String> locationPatternList, String suffix) {
- super(locationPatternList);
- this.suffix = suffix;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public <T> T load() throws Exception {
- Document doc = XmlLoaderUtils.newDoc();
- Element root = null;
- for (String locationPattern : locationPatternList) {
- List<Resource> resList =
PaaSResourceUtils.getSortedResources(locationPattern, suffix);
- for (Resource res : resList) {
- Document tmpDoc = XmlLoaderUtils.load(res);
- Element tmpRoot = tmpDoc.getDocumentElement();
-
- if (root == null) {
- root = (Element) doc.importNode(tmpRoot, false);
- doc.appendChild(root);
- }
-
- NodeList nodeList = tmpRoot.getChildNodes();
- for (int idx = 0; idx < nodeList.getLength(); idx++) {
- Node child = nodeList.item(idx);
-
- if (!Element.class.isInstance(child)) {
- continue;
- }
-
- Element clone = (Element) doc.importNode(child, true);
- Element exist = findAndSetExist(clone);
- if (exist == null) {
- root.appendChild(clone);
- continue;
- }
-
- // merge attr and children
- XmlLoaderUtils.mergeElement(clone, exist);
- }
- }
- }
- return (T) doc;
- }
-
- protected Element findAndSetExist(Element ele) {
- return null;
- }
-}