You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by li...@apache.org on 2022/07/15 04:46:09 UTC

[dubbo-samples] branch master updated: support metadata service for triple protocol (#445)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 69af42d3 support metadata service for triple protocol (#445)
69af42d3 is described below

commit 69af42d32475d1bae0ee4dfd930e77ee6dc0ae4a
Author: huazhongming <cr...@apache.org>
AuthorDate: Fri Jul 15 12:46:05 2022 +0800

    support metadata service for triple protocol (#445)
    
    Signed-off-by: crazyhzm <cr...@apache.org>
---
 .../sample/tri/metadata/MetadataGreeterImpl.java   | 102 +++++++++++++++++++++
 .../sample/tri/metadata/TriMetadataClient.java     |  73 +++++++++++++++
 .../sample/tri/metadata/TriMetadataServer.java     |  79 ++++++++++++++++
 .../tri/direct/TriDirectMetadataClientTest.java    |  49 ++++++++++
 4 files changed, 303 insertions(+)

diff --git a/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/MetadataGreeterImpl.java b/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/MetadataGreeterImpl.java
new file mode 100644
index 00000000..ed3bc2f2
--- /dev/null
+++ b/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/MetadataGreeterImpl.java
@@ -0,0 +1,102 @@
+/*
+ *  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.sample.tri.metadata;
+
+import org.apache.dubbo.common.stream.StreamObserver;
+import org.apache.dubbo.sample.tri.Greeter;
+import org.apache.dubbo.sample.tri.GreeterReply;
+import org.apache.dubbo.sample.tri.GreeterRequest;
+import org.apache.dubbo.sample.tri.stub.GreeterImpl;
+
+public class MetadataGreeterImpl implements Greeter {
+    private final Greeter delegate;
+
+    public MetadataGreeterImpl() {
+        this.delegate = new GreeterImpl("tri-metadata");
+    }
+
+    @Override
+    public GreeterReply greet(GreeterRequest request) {
+        return delegate.greet(request);
+    }
+
+    @Override
+    public GreeterReply upperCaseGreet(GreeterRequest request) {
+        return delegate.upperCaseGreet(request);
+    }
+
+    @Override
+    public GreeterReply greetWithAttachment(GreeterRequest request) {
+        return delegate.greetWithAttachment(request);
+    }
+
+    @Override
+    public GreeterReply greetReturnBigAttachment(GreeterRequest request) {
+        return delegate.greetReturnBigAttachment(request);
+    }
+
+    @Override
+    public GreeterReply greetException(GreeterRequest request) {
+        return delegate.greetException(request);
+    }
+
+    @Override
+    public GreeterReply queryCancelResult(GreeterRequest request) {
+        return delegate.queryCancelResult(request);
+    }
+
+    @Override
+    public void greetServerStream(GreeterRequest request, StreamObserver<GreeterReply> responseObserver) {
+        delegate.greetServerStream(request, responseObserver);
+    }
+
+    @Override
+    public void cancelServerStream(GreeterRequest request, StreamObserver<GreeterReply> responseObserver) {
+        delegate.cancelServerStream(request, responseObserver);
+    }
+
+    @Override
+    public StreamObserver<GreeterRequest> greetStream(StreamObserver<GreeterReply> responseObserver) {
+        return delegate.greetStream(responseObserver);
+    }
+
+    @Override
+    public StreamObserver<GreeterRequest> cancelBiStream(StreamObserver<GreeterReply> responseObserver) {
+        return delegate.cancelBiStream(responseObserver);
+    }
+
+    @Override
+    public StreamObserver<GreeterRequest> cancelBiStream2(StreamObserver<GreeterReply> responseObserver) {
+        return delegate.cancelBiStream2(responseObserver);
+    }
+
+    @Override
+    public StreamObserver<GreeterRequest> compressorBiStream(StreamObserver<GreeterReply> responseObserver) {
+        return delegate.compressorBiStream(responseObserver);
+    }
+
+    @Override
+    public StreamObserver<GreeterRequest> clientCompressorBiStream(StreamObserver<GreeterReply> responseObserver) {
+        return delegate.clientCompressorBiStream(responseObserver);
+    }
+
+    @Override
+    public StreamObserver<GreeterRequest> serverCompressorBiStream(StreamObserver<GreeterReply> responseObserver) {
+        return delegate.serverCompressorBiStream(responseObserver);
+    }
+}
\ No newline at end of file
diff --git a/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/TriMetadataClient.java b/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/TriMetadataClient.java
new file mode 100644
index 00000000..fcc62f95
--- /dev/null
+++ b/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/TriMetadataClient.java
@@ -0,0 +1,73 @@
+/*
+ *  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.sample.tri.metadata;
+
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.sample.tri.Greeter;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.sample.tri.util.TriSampleClient;
+import org.apache.dubbo.sample.tri.util.TriSampleConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class TriMetadataClient extends TriSampleClient {
+    private static final Logger LOGGER = LoggerFactory.getLogger(TriMetadataClient.class);
+
+    public TriMetadataClient() {
+        this(false, null);
+    }
+
+    public TriMetadataClient(boolean direct, String directUrl) {
+        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
+        ReferenceConfig<Greeter> ref = new ReferenceConfig<>();
+        ref.setInterface(Greeter.class);
+        ref.setProtocol(CommonConstants.TRIPLE);
+        ref.setProxy(CommonConstants.NATIVE_STUB);
+        ref.setTimeout(3000);
+
+        if (!direct) {
+            bootstrap.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS));
+        } else {
+            ref.setUrl(directUrl);
+        }
+
+        ApplicationConfig applicationConfig = new ApplicationConfig("tri-metadata-consumer");
+        applicationConfig.setMetadataServiceProtocol(CommonConstants.TRIPLE);
+
+        bootstrap.application(applicationConfig)
+                .reference(ref)
+                .start();
+        setGreeter(ref.get());
+        setClientName("tri-metadata");
+    }
+
+    public static void main(String[] args) throws IOException {
+        final TriMetadataClient consumer = new TriMetadataClient();
+        consumer.unary();
+        consumer.stream();
+        consumer.serverStream();
+        System.in.read();
+    }
+
+}
diff --git a/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/TriMetadataServer.java b/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/TriMetadataServer.java
new file mode 100644
index 00000000..b39d52c2
--- /dev/null
+++ b/dubbo-samples-triple/src/main/java/org/apache/dubbo/sample/tri/metadata/TriMetadataServer.java
@@ -0,0 +1,79 @@
+/*
+ *  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.sample.tri.metadata;
+
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.context.Lifecycle;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.sample.tri.Greeter;
+import org.apache.dubbo.sample.tri.util.EmbeddedZooKeeper;
+import org.apache.dubbo.sample.tri.util.TriSampleConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+
+public class TriMetadataServer implements Lifecycle {
+    private static final Logger LOGGER = LoggerFactory.getLogger(TriMetadataServer.class);
+    private final int port;
+
+    public TriMetadataServer(int port) {
+        this.port = port;
+    }
+
+    public static void main(String[] args) throws IOException {
+        TriMetadataServer server = new TriMetadataServer(TriSampleConstants.SERVER_PORT);
+        server.initialize();
+        server.start();
+        System.in.read();
+    }
+
+    @Override
+    public void initialize() throws IllegalStateException {
+        new EmbeddedZooKeeper(TriSampleConstants.ZK_PORT, false).start();
+    }
+
+    @Override
+    public void start() throws IllegalStateException {
+        ServiceConfig<Greeter> service = new ServiceConfig<>();
+        service.setInterface(Greeter.class);
+        service.setRef(new MetadataGreeterImpl());
+
+        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
+
+        ApplicationConfig applicationConfig = new ApplicationConfig("tri-metadata-server");
+        applicationConfig.setMetadataServiceProtocol(CommonConstants.TRIPLE);
+
+        bootstrap.application(applicationConfig)
+                .registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
+                .protocol(new ProtocolConfig(CommonConstants.TRIPLE, port))
+                .service(service)
+                .start();
+    }
+
+    @Override
+    public void destroy() throws IllegalStateException {
+        DubboBootstrap.getInstance().stop();
+    }
+}
diff --git a/dubbo-samples-triple/src/test/java/org/apache/dubbo/sample/tri/direct/TriDirectMetadataClientTest.java b/dubbo-samples-triple/src/test/java/org/apache/dubbo/sample/tri/direct/TriDirectMetadataClientTest.java
new file mode 100644
index 00000000..1820e5fb
--- /dev/null
+++ b/dubbo-samples-triple/src/test/java/org/apache/dubbo/sample/tri/direct/TriDirectMetadataClientTest.java
@@ -0,0 +1,49 @@
+/*
+ *  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.sample.tri.direct;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.sample.tri.BaseClientTest;
+import org.apache.dubbo.sample.tri.Greeter;
+import org.apache.dubbo.sample.tri.util.TriSampleConstants;
+import org.junit.BeforeClass;
+
+public class TriDirectMetadataClientTest extends BaseClientTest {
+
+    @BeforeClass
+    public static void init() {
+        ReferenceConfig<Greeter> ref = new ReferenceConfig<>();
+        ref.setInterface(Greeter.class);
+        ref.setUrl(TriSampleConstants.DEFAULT_ADDRESS);
+        ref.setProtocol(CommonConstants.TRIPLE);
+        ref.setTimeout(3000);
+
+        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
+        ApplicationConfig applicationConfig = new ApplicationConfig(TriDirectMetadataClientTest.class.getName());
+        applicationConfig.setMetadataServiceProtocol(CommonConstants.TRIPLE);
+        applicationConfig.setMetadataServicePort(TriSampleConstants.CONSUMER_METADATA_SERVICE_PORT);
+        bootstrap.application(applicationConfig)
+                .reference(ref)
+                .start();
+        delegate = ref.get();
+        appDubboBootstrap = bootstrap;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org