Skip to content

Commit

Permalink
[SCB-1958]local registry support register code first schema for mic…
Browse files Browse the repository at this point in the history
…roservices
  • Loading branch information
liubao68 committed Jun 1, 2020
1 parent 55784d1 commit 065fd1b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand All @@ -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();
Expand All @@ -38,7 +41,13 @@ public static void main(final String[] args) throws Exception {
}
}

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");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand All @@ -29,11 +30,15 @@

@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();
}

Expand All @@ -50,6 +55,10 @@ private void testGetAllMicroservice() {
TestMgr.check(2, expectedCount);
}

private void testCodeFirstGetName() {
TestMgr.check("2", codeFirstService.getName("2"));
}

private void testServerGetName() {
RestTemplate template = RestTemplateBuilder.create();
TestMgr.check("2", template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ demo-local-registry-server:
appid: demo-local-registry
schemaIds:
- ServerEndpoint
- CodeFirstEndpoint
instances:
- endpoints:
- rest://localhost:8080
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void runCategorizedTestCase(String microserviceName) throws Except
testCase.testHighwayTransport();
}
} catch (Exception e) {
e.printStackTrace();
TestMgr.failed("run categorized test case " +
testCase.getClass().getName() +
" failed, reason " + e.getMessage(),
Expand Down

0 comments on commit 065fd1b

Please sign in to comment.