最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

世界热推荐:从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议

来源:博客园

使用 IDL 定义服务具有更好的跨语言友好性,然而 Triple 协议并不是和 IDL 强绑定的,也可以使用 Java Interface + Pojo 的方式定义服务并启用 Triple 协议,具体可参见示例。

更多 Triple 和 IDL 使用方式,请参考官方示例


(资料图片仅供参考)

前置条件

  • JDK 版本 >= 8
  • 已安装 Maven

创建工程

  1. 首先创建一个空的 maven 工程

    $ mvn archetype:generate                                \    -DgroupId=org.apache.dubbo                          \    -DartifactId=tri-stub-demo                          \    -DarchetypeArtifactId=maven-archetype-quickstart    \    -DarchetypeVersion=1.4                              \    -DarchetypeGroupId=org.apache.maven.archetypes      \    -Dversion=1.0-SNAPSHOT
  2. 切换到工程目录

    $ cd tri-stub-demo
  3. pom.xml中设置 JDK 版本,添加 Dubbo 依赖和插件

        UTF-8    1.8    1.8          junit       junit       4.13       test             org.apache.dubbo       dubbo       3.0.8          org.apache.dubbo    dubbo-dependencies-zookeeper-curator5    pom    3.0.8               com.google.protobuf        protobuf-java        3.19.4                            kr.motd.maven            os-maven-plugin            1.6.1                                    org.xolstice.maven.plugins            protobuf-maven-plugin            0.6.1                            com.google.protobuf:protoc:3.19.4:exe:${os.detected.classifier}                                                            dubbo                        org.apache.dubbo                        dubbo-compiler                        0.0.4.1-SNAPSHOT                        org.apache.dubbo.gen.tri.Dubbo3TripleGenerator                                                                                                                        compile                                                            
  4. 添加接口定义文件src/main/proto/hello.proto,Dubbo 使用 Protobuf 作为 IDL

    syntax = "proto3";option java_multiple_files = true;option java_package = "org.apache.dubbo.hello";option java_outer_classname = "HelloWorldProto";option objc_class_prefix = "HLW";package helloworld;message HelloRequest {    string name = 1;}message HelloReply {    string message = 1;}service Greeter{    rpc greet(HelloRequest) returns (HelloReply);}
  5. 编译 IDL

    $ mvn clean install

    编译成功后,可以看到target/generated-sources/protobuf/java目录下生成了代码文件

    $ ls org/apache/dubbo/hello/DubboGreeterTriple.java    HelloReply.java            HelloRequest.java          HelloWorldProto.javaGreeter.java               HelloReplyOrBuilder.java   HelloRequestOrBuilder.java
  6. 添加服务端接口实现src/main/java/org/apache/dubbo/GreeterImpl.java

    package org.apache.dubbo;import org.apache.dubbo.hello.DubboGreeterTriple;import org.apache.dubbo.hello.HelloReply;import org.apache.dubbo.hello.HelloRequest;public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {   @Override   public HelloReply greet(HelloRequest request) {      return HelloReply.newBuilder()      .setMessage("Hello," + request.getName() + "!")      .build();   }}
  7. 添加服务端启动类 src/main/java/org/apache/dubbo/MyDubboServer.java

    package org.apache.dubbo;import org.apache.dubbo.common.constants.CommonConstants;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.hello.Greeter;import java.io.IOException;public class MyDubboServer {   public static void main(String[] args) throws IOException {       ServiceConfig service = new ServiceConfig<>();       service.setInterface(Greeter.class);       service.setRef(new GreeterImpl());       DubboBootstrap bootstrap = DubboBootstrap.getInstance();       bootstrap.application(new ApplicationConfig("tri-stub-server"))               .registry(new RegistryConfig("multicast://127.0.0.1:2181"))               .protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))               .service(service)               .start();       System.out.println("Dubbo triple stub server started");       System.in.read();   }}
  8. 添加客户端启动类src/main/java/org/apache/dubbo/MyDubboClient.java

    package org.apache.dubbo;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.config.bootstrap.DubboBootstrap;import org.apache.dubbo.hello.Greeter;import org.apache.dubbo.hello.HelloReply;import org.apache.dubbo.hello.HelloRequest;public class MyDubboClient {   public static void main(String[] args) {      DubboBootstrap bootstrap = DubboBootstrap.getInstance();      ReferenceConfig ref = new ReferenceConfig<>();      ref.setInterface(Greeter.class);      ref.setProtocol(CommonConstants.TRIPLE);      ref.setProxy(CommonConstants.NATIVE_STUB);      ref.setTimeout(3000);      bootstrap.application(new ApplicationConfig("tri-stub-client"))         .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))         .reference(ref)         .start();      Greeter greeter = ref.get();      HelloRequest request = HelloRequest.newBuilder().setName("Demo").build();      HelloReply reply = greeter.greet(request);      System.out.println("Received reply:" + reply);    }}
  9. 编译代码

    $ mvn clean install
  10. 启动服务端

$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboServer"Dubbo triple stub server started
  1. 打开新的终端,启动客户端
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboClient"Received reply:message: "Hello,Demo!"

欢迎在 https://github.com/apache/dubbo 给 Dubbo Star。搜索关注官方微信公众号:Apache Dubbo,了解更多业界最新动态,掌握大厂面试必备 Dubbo 技能

关键词: 前置条件 可以使用 编译代码