当前位置: 首页 > 后端技术 > Java

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

时间:2023-04-02 10:35:25 Java

使用IDL定义服务具有更好的跨语言友好性。但是Triple协议并没有和IDL强绑定,也可以使用JavaInterface+Pojo定义服务,同样的方式启用Triple协议,详见示例。Triple和I??DL的更多使用方法参考官方示例前提条件JDK版本>=8已安装Maven创建项目首先创建一个空的maven项目$mvnarchetype: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切换到项目目录$cdtri-stub-demo在pom中设置JDK版本.xml,添加Dubbo依赖和插件UTF-81.81.8junitjunit<版本>4.13测试org.apache.dubbodubbo3.0.8org.apache.dubbodubbo-dependencies-zookeeper-curator5pom3.0.8com.google.protobufprotobuf-java3.19.4kr.motd.mavenos-maven-plugin1.6.1org.xolstice.maven.pluginsprotobuf-maven-plugin0.6.1com.google.protobuf:protoc:3.19.4:exe:${os.detected.classifier}<协议插件><协议插件>dubboorg.apache.dubbodubbo编译器0.0.4.1-SNAPSHOTorg.apache.dubbo.gen.tri.Dubbo3TripleGeneratorcompile添加接口定义文件src/main/proto/hello.proto,Dubbo使用Protobuf作为IDLsyntax="proto3";optionjava_multiple_files=true;optionjava_package="org.apache.dubbo.hello";optionjava_outer_classname="HelloWorldProto";optionobjc_class_prefix="HLW";packagehelloworld;messageHelloRequest{stringname=1;}messageHelloReply{stringmessage=1;}serviceGreeter{rpcgreet(HelloRequest)returns(HelloReply);}编译IDL$mvncleaninstall编译成功后可以看到target/generated-sources在/protobuf/java目录下生成代码文件$lsorg/apache/dubbo/hello/DubboGreeterTriple.javaHelloReply.javaHelloRequest.javaHelloWorldProto.javaGreeter.javaHelloReplyOrBuilder.javaHelloRequestOrBuilder.java添加服务端接口现src/main/java/org/apache/dubbo/GreeterImpl.javapackageorg.apache.dubbo;importorg.apache.dubbo.hello.DubboGreeterTriple;importorg.apache.dubbo.hello.HelloReply;importorg.apache.dubbo.hello.HelloRequest;publicclassGreeterImplextendsDubboGreeterTriple.GreeterImplBase{@OverridepublicHelloReplygreet(HelloRequestrequest){returnHelloReply.newBuilder().setMessage("你好,"+request.getName()+"!").build();}}添加服务端启动类src/main/java/org/apache/dubbo/MyDubboServer.javapackageorg.apache.dubbo;importorg.apache.dubbo.common.constants.CommonConstants;importorg.apache.dubbo.config.ApplicationConfig;导入org.apache.dubbo.config.ProtocolConfig;导入org.apache.dubbo.config.RegistryConfig;导入org.apache.dubbo.config.ServiceConfig;导入org.apache.dubbo.config.bootstrap.DubboBootstrap;导入org.apache.dubbo.hello.Greeter;importjava.io.IOException;publicclassMyDubboServer{publicstaticvoidmain(String[]args)throwsIOException{ServiceConfigservice=newServiceConfig<>();service.setInterface(Greeter.class);service.setRef(newGreeterImpl());DubboBootstrapbootstrap=DubboBootstrap.getInstance();bootstrap.application(newApplicationConfig("tri-stub-server")).registry(newRegistryConfig("multicast://127.0.0.1:2181")).protocol(newProtocolConfig(CommonConstants.TRIPLE,50051)).service(服务).start();System.out.println("Dubbotriplestubserver已启动");系统.in.read();}}添加客户端启动类src/main/java/org/apache/dubbo/MyDubboClient.javapackageorg.apache.dubbo;importorg.apache.dubbo.common.constants.CommonConstants;importorg.apache.dubbo.config。ApplicationConfig;导入org.apache。dubbo.config.ReferenceConfig;导入org.apache.dubbo.config.RegistryConfig;导入org.apache.dubbo.config.bootstrap.DubboBootstrap;导入org.apache.dubbo.hello.Greeter;导入org.apache.dubbo.hello。HelloReply;导入org.apache.dubbo.hello.HelloRequest;公共类MyDubboClient{publicstaticvoidmain(String[]args){DubboBootstrapbootstrap=DubboBootstrap.getInstance();ReferenceConfigref=newReferenceConfig<>();ref.setInterface(Greeter.class);ref.setProtocol(CommonConstants.TRIPLE);ref.setProxy(CommonConstants.NATIVE_STUB);ref.setTimeout(3000);bootstrap.application(newApplicationConfig("tri-stub-client")).registry(newRegistryConfig("zookeeper://127.0.0.1:2181")).reference(ref).start();问候语greeter=ref.get();HelloRequestrequest=HelloRequest.newBuilder().setName("Demo").build();你好回复代表ly=greeter.greet(请求);System.out.println("收到回复:"+reply);}}编译代码$mvncleaninstallstartserver$mvnorg.codehaus.mojo:exec-maven-plugin:3.0.0:java-Dexec.mainClass="org.apache.dubbo.MyDubboServer"Dubbotriplestubserverstarted打开一个新终端并启动客户端$mvnorg.codehaus.mojo:exec-maven-plugin:3.0.0:java-Dexec.mainClass="org.apache.dubbo.MyDubboClient"Receivedreply:message:"Hello,Demo!"欢迎搜索Dubbo之星https://github.com/apache/dubbo官方微信公众号:ApacheDubbo,了解更多行业最新动态,掌握各大公司面试必备的Dubbo技能

最新推荐
猜你喜欢