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/06/01 09:34:06 UTC
[servicecomb-java-chassis] branch master updated: [SCB-1958]local
registry support register `code first schema` for microservices
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 065fd1b [SCB-1958]local registry support register `code first schema` for microservices
065fd1b is described below
commit 065fd1ba0ee2e094a62f517f22d8959b293593e6
Author: liubao <bi...@qq.com>
AuthorDate: Mon Jun 1 11:35:15 2020 +0800
[SCB-1958]local registry support register `code first schema` for microservices
---
.../demo/localRegistryClient/Application.java | 11 +++++-
.../demo/localRegistryClient/CodeFirstService.java | 31 ++++++++++++++++
.../LocalRegistryServerTest.java | 9 +++++
.../src/main/resources/registry.yaml | 1 +
.../localRegistryServer/CodeFirstEndpoint.java | 42 ++++++++++++++++++++++
.../demo/CategorizedTestCaseRunner.java | 1 +
6 files changed, 94 insertions(+), 1 deletion(-)
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java
index fd40c66..d54220a 100644
--- a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/Application.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.demo.localRegistryClient;
import org.apache.servicecomb.demo.CategorizedTestCaseRunner;
import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.registry.RegistrationManager;
import org.apache.servicecomb.springboot2.starter.EnableServiceComb;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -30,6 +31,8 @@ public class Application {
public static void main(final String[] args) throws Exception {
new SpringApplicationBuilder().sources(Application.class).web(WebApplicationType.SERVLET).build().run(args);
+ registerSchema();
+
runTest();
TestMgr.summary();
@@ -38,7 +41,13 @@ public class Application {
}
}
- public static void runTest() throws Exception {
+ private static void registerSchema() {
+ RegistrationManager.INSTANCE.getSwaggerLoader().registerSwagger("demo-local-registry",
+ "demo-local-registry-server",
+ "CodeFirstEndpoint", CodeFirstService.class);
+ }
+
+ private static void runTest() throws Exception {
CategorizedTestCaseRunner.runCategorizedTestCase("demo-local-registry-server");
}
}
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/CodeFirstService.java b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/CodeFirstService.java
new file mode 100644
index 0000000..cbe329e
--- /dev/null
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/CodeFirstService.java
@@ -0,0 +1,31 @@
+/*
+ * 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.localRegistryClient;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+
+@Path("/register/url/codeFirst")
+@Produces("application/json")
+public interface CodeFirstService {
+ @GET
+ @Path("getName")
+ String getName(@QueryParam("name") String name);
+}
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java
index cee8dd9..058f091 100644
--- a/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/java/org/apache/servicecomb/demo/localRegistryClient/LocalRegistryServerTest.java
@@ -21,6 +21,7 @@ import java.util.List;
import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.apache.servicecomb.registry.DiscoveryManager;
import org.apache.servicecomb.registry.api.registry.Microservice;
@@ -29,11 +30,15 @@ import org.springframework.web.client.RestTemplate;
@Component
public class LocalRegistryServerTest implements CategorizedTestCase {
+ @RpcReference(microserviceName = "demo-local-registry-server", schemaId = "CodeFirstEndpoint")
+ private CodeFirstService codeFirstService;
+
RestTemplate template = RestTemplateBuilder.create();
@Override
public void testRestTransport() throws Exception {
testServerGetName();
+ testCodeFirstGetName();
testGetAllMicroservice();
}
@@ -50,6 +55,10 @@ public class LocalRegistryServerTest implements CategorizedTestCase {
TestMgr.check(2, expectedCount);
}
+ private void testCodeFirstGetName() {
+ TestMgr.check("2", codeFirstService.getName("2"));
+ }
+
private void testServerGetName() {
RestTemplate template = RestTemplateBuilder.create();
TestMgr.check("2", template
diff --git a/demo/demo-local-registry/demo-local-registry-client/src/main/resources/registry.yaml b/demo/demo-local-registry/demo-local-registry-client/src/main/resources/registry.yaml
index c5d3a94..2ad3c23 100644
--- a/demo/demo-local-registry/demo-local-registry-client/src/main/resources/registry.yaml
+++ b/demo/demo-local-registry/demo-local-registry-client/src/main/resources/registry.yaml
@@ -21,6 +21,7 @@ demo-local-registry-server:
appid: demo-local-registry
schemaIds:
- ServerEndpoint
+ - CodeFirstEndpoint
instances:
- endpoints:
- rest://localhost:8080
\ No newline at end of file
diff --git a/demo/demo-local-registry/demo-local-registry-server/src/main/java/org/apache/servicecomb/demo/localRegistryServer/CodeFirstEndpoint.java b/demo/demo-local-registry/demo-local-registry-server/src/main/java/org/apache/servicecomb/demo/localRegistryServer/CodeFirstEndpoint.java
new file mode 100644
index 0000000..781eed6
--- /dev/null
+++ b/demo/demo-local-registry/demo-local-registry-server/src/main/java/org/apache/servicecomb/demo/localRegistryServer/CodeFirstEndpoint.java
@@ -0,0 +1,42 @@
+/*
+ * 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.localRegistryServer;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@RestSchema(schemaId = "CodeFirstEndpoint")
+@RequestMapping(path = "/register/url/codeFirst", produces = MediaType.APPLICATION_JSON)
+public class CodeFirstEndpoint {
+ private static final Logger LOGGER
+ = LoggerFactory.getLogger(CodeFirstEndpoint.class);
+
+ @GetMapping(path = "/getName")
+ public String getName(@RequestParam(name = "name") String name) {
+ ((Invocation) ContextUtils.getInvocationContext()).getTraceIdLogger().info(LOGGER, "get name invoked.");
+ return name;
+ }
+}
diff --git a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CategorizedTestCaseRunner.java b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CategorizedTestCaseRunner.java
index 8f92c1d..b79120b 100644
--- a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CategorizedTestCaseRunner.java
+++ b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CategorizedTestCaseRunner.java
@@ -42,6 +42,7 @@ public class CategorizedTestCaseRunner {
testCase.testHighwayTransport();
}
} catch (Exception e) {
+ e.printStackTrace();
TestMgr.failed("run categorized test case " +
testCase.getClass().getName() +
" failed, reason " + e.getMessage(),