首页 > 其他分享 >从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议

从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议

时间:2022-12-26 11:01:48浏览次数:64  
标签:Dubbo java dubbo IDL Triple import apache org hello

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

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

前置条件

创建工程

  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 依赖和插件

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.dubbo</groupId>
           <artifactId>dubbo</artifactId>
           <version>3.0.8</version>
       </dependency>
       <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
        <type>pom</type>
        <version>3.0.8</version>
       </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.19.4</version>
        </dependency>
    </dependencies>
    
    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.19.4:exe:${os.detected.classifier}</protocArtifact>
                    <protocPlugins>
                        <protocPlugin>
                            <id>dubbo</id>
                            <groupId>org.apache.dubbo</groupId>
                            <artifactId>dubbo-compiler</artifactId>
                            <version>0.0.4.1-SNAPSHOT</version>
                            <mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass>
                        </protocPlugin>
                    </protocPlugins>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
  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.java
    Greeter.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<Greeter> 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<Greeter> 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 技能

标签:Dubbo,java,dubbo,IDL,Triple,import,apache,org,hello
From: https://www.cnblogs.com/apache-dubbo/p/17005231.html

相关文章

  • What's new in dubbo-go v3.0.4
    dubbogo社区发布了 ​​v3.0.4​​ ​​https://github.com/apache/dubbo-go/releases/tag/v3.0.4-rc1,本文列出相关重大​​feature、bugfix、性能提升项。1对接Pol......
  • 在Dubbo-go中使用TLS加密进行安全通信
    1背景Dubbo-go在Getty/Triple/Grpc三个通信层面支持TLS链路安全通信。2原理2.1证书机制:ps:可以先提前了解非对称加密机制。CA(CertificationAuthority)负责生成根证书......
  • Dubbo架构设计与源码解析(三)责任链模式
    作者:周可强一、责任链模式简介1、责任链模式定义责任链(ChainofResponsibility)模式的定义:为了避免请求发送者与多个请求处理者耦合在一起,于是将所有请求的处理者通过前一对......
  • Apache Dubbo 官方正式发布 Spring 6 & Spring Boot 3 支持
    Dubbo简介ApacheDubbo是一款RPC服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了Java、Golang等多语言SDK实现。使用Dubbo开发的微服务原生具......
  • Dubbo架构设计与源码解析(三)责任链模式
    作者:周可强一、责任链模式简介1、责任链模式定义责任链(ChainofResponsibility)模式的定义:为了避免请求发送者与多个请求处理者耦合在一起,于是将所有请求的处理者通过前......
  • What's new in Dubbo 3.1.4 and 3.2.0-beta.3
    在12月22日,Dubbo3.1.4和3.2.0-beta.3正式通过投票发布。本文将介绍发布的变化一览。Dubbo3.1.4版本是目前Dubbo3的最新稳定版本,我们建议所有的用户都升级到最......
  • 初识Dubbo
    目录​​Dubbo能够做什么​​​​Dubbo架构图​​​​入门案例​​​​在Dubbo中引入注册中心​​​​关于DubboContainer​​​​Dubbo多协议支持​​​​Dubbo多注册中心......
  • weidlxDeepRec:热门微博推荐框架性能提升实战
    微博推荐团队:陈雨、韩楠、蔡小娟、高家华1.项目背景热门微博是新浪微博的重要功能之一,包含热门流、热点流、频道流、小视频后推荐、视频社区等场景。推荐首页发现页推荐沉......
  • 如何基于 Spring Boot 快速开发一个 Dubbo 微服务应用
    Dubbo还提供了包括XML、API等多种启动与接入方式,更多开发方式和配置细节可参见配置手册。下载示例代码完整示例代码在dubbo-samples中。下载源码gitclone-bma......
  • 基于 Dubbo-Admin 实现根据请求条件路由
    Dubbo提供动态创建条件路由的服务治理能力,可以在无需重启应用的情况下,根据请求发起方、请求的方法条件路由。Dubbo可以通过XML配置,注解配置,动态配置实现动态根据请求条件路......