You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2019/08/13 12:31:21 UTC

[dubbo] 01/13: add rest demo

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch cloud-native
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit a12a5398850dac857c1021fa751801f993035287
Author: ken.lj <ke...@gmail.com>
AuthorDate: Mon Aug 12 16:52:04 2019 +0800

    add rest demo
---
 dubbo-bootstrap/pom.xml                            | 13 +++-
 .../bootstrap/DubboServiceConsumerBootstrap.java   |  8 ++-
 .../bootstrap/DubboServiceProviderBootstrap.java   | 30 +++++++--
 .../java/org/apache/dubbo/bootstrap/rest/User.java | 77 ++++++++++++++++++++++
 .../apache/dubbo/bootstrap/rest/UserService.java   | 47 +++++++++++++
 .../dubbo/bootstrap/rest/UserServiceImpl.java      | 38 +++++++++++
 6 files changed, 205 insertions(+), 8 deletions(-)

diff --git a/dubbo-bootstrap/pom.xml b/dubbo-bootstrap/pom.xml
index 0fd354d..2566294 100644
--- a/dubbo-bootstrap/pom.xml
+++ b/dubbo-bootstrap/pom.xml
@@ -33,6 +33,12 @@
             <version>${project.parent.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-registry-consul</artifactId>
+            <version>${project.parent.version}</version>
+            <scope>test</scope>
+        </dependency>
 
         <dependency>
             <groupId>org.apache.dubbo</groupId>
@@ -75,7 +81,12 @@
             <version>${project.parent.version}</version>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-rpc-rest</artifactId>
+            <version>${project.parent.version}</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.dubbo</groupId>
             <artifactId>dubbo-rpc-hessian</artifactId>
diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
index 0f7a958..def1d07 100644
--- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceConsumerBootstrap.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.bootstrap;
 
+import org.apache.dubbo.bootstrap.rest.UserService;
 import org.apache.dubbo.config.ReferenceConfig;
 import org.apache.dubbo.config.context.ConfigManager;
 
@@ -31,10 +32,11 @@ public class DubboServiceConsumerBootstrap {
         new DubboBootstrap()
                 .application("dubbo-consumer-demo")
                 // Zookeeper
-                .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service&subscribed-services=dubbo-provider-demo"))
+//                .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service&subscribed-services=dubbo-provider-demo"))
                 // Nacos
-//                .registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry-type=service&subscribed-services=dubbo-provider-demo"))
-                .reference("ref", builder -> builder.interfaceClass(EchoService.class))
+                .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry-type=service&subscribed-services=dubbo-provider-demo"))
+                .reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
+                .reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
                 .onlyRegisterProvider(true)
                 .start()
                 .await();
diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
index 15d1628..fc2242e 100644
--- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java
@@ -16,6 +16,11 @@
  */
 package org.apache.dubbo.bootstrap;
 
+import org.apache.dubbo.bootstrap.rest.UserService;
+import org.apache.dubbo.bootstrap.rest.UserServiceImpl;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.builders.RegistryBuilder;
+
 /**
  * Dubbo Provider Bootstrap
  *
@@ -24,17 +29,34 @@ package org.apache.dubbo.bootstrap;
 public class DubboServiceProviderBootstrap {
 
     public static void main(String[] args) {
+        ProtocolConfig restProtocol = new ProtocolConfig();
+        restProtocol.setName("rest");
+        restProtocol.setId("rest");
+        restProtocol.setPort(-1);
+
         new DubboBootstrap()
                 .application("dubbo-provider-demo")
                 // Zookeeper in service registry type
-                .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service"))
+//                .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service"))
                 // Nacos
 //                .registry("zookeeper", builder -> builder.address("nacos://127.0.0.1:8848?registry-type=service"))
-//                .registry(RegistryBuilder.newBuilder().address("etcd3://127.0.0.1:2379?registry-type=service").build())
+                .registry(RegistryBuilder.newBuilder().address("consul://127.0.0.1:8500?registry-type=service").build())
                 .protocol(builder -> builder.port(-1).name("dubbo"))
-                .protocol(builder -> builder.port(-1).name("hessian"))
-                .service(builder -> builder.id("test").interfaceClass(EchoService.class).ref(new EchoServiceImpl()))
+                .service(builder -> builder.id("echo").interfaceClass(EchoService.class).ref(new EchoServiceImpl()))
+                .service(builder -> builder.id("user").interfaceClass(UserService.class).ref(new UserServiceImpl()).addProtocol(restProtocol))
                 .start()
                 .await();
     }
+
+    private static void testSCCallDubbo() {
+
+    }
+
+    private static void testDubboCallSC() {
+
+    }
+
+    private static void testDubboTansormation() {
+
+    }
 }
diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/User.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/User.java
new file mode 100644
index 0000000..993862a
--- /dev/null
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/User.java
@@ -0,0 +1,77 @@
+/*
+ *
+ *   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.dubbo.bootstrap.rest;
+
+import org.codehaus.jackson.annotate.JsonProperty;
+
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.io.Serializable;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.FIELD)
+public class User implements Serializable {
+
+    @NotNull
+    @Min(1L)
+    private Long id;
+
+    @JsonProperty("username")
+    @XmlElement(name = "username")
+    @NotNull
+    @Size(min = 6, max = 50)
+    private String name;
+
+    public User() {
+    }
+
+    public User(Long id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String toString() {
+        return "User (" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                ')';
+    }
+}
diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/UserService.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/UserService.java
new file mode 100644
index 0000000..514a0bf
--- /dev/null
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/UserService.java
@@ -0,0 +1,47 @@
+/*
+ *
+ *   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.dubbo.bootstrap.rest;
+
+
+import org.apache.dubbo.rpc.protocol.rest.support.ContentType;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("users")
+@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
+@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
+@Api(value = "UserService")
+public interface UserService {
+
+    @GET
+    @Path("{id : \\d+}")
+    @ApiOperation(value = "getUser")
+    User getUser(@ApiParam(value = "id") @PathParam("id") Long id);
+
+    Long registerUser(User user);
+}
diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/UserServiceImpl.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/UserServiceImpl.java
new file mode 100644
index 0000000..01259eb
--- /dev/null
+++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/rest/UserServiceImpl.java
@@ -0,0 +1,38 @@
+/*
+ *
+ *   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.dubbo.bootstrap.rest;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+public class UserServiceImpl implements UserService {
+
+    private final AtomicLong idGen = new AtomicLong();
+
+    @Override
+    public User getUser(Long id) {
+        return new User(id, "username" + id);
+    }
+
+
+    @Override
+    public Long registerUser(User user) {
+//        System.out.println("Username is " + user.getName());
+        return idGen.incrementAndGet();
+    }
+}