本篇文章为你整理了从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议()的详细内容,包含有 从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议,希望能帮助你了解 从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议。
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
添加接口定义文件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);
编译成功后,可以看到target/generated-sources/protobuf/java 目录下生成了代码文件
$ ls org/apache/dubbo/hello/
DubboGreeterTriple.java HelloReply.java HelloRequest.java HelloWorldProto.java
Greeter.java HelloReplyOrBuilder.java HelloRequestOrBuilder.java
添加服务端接口实现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();
添加服务端启动类 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();
添加客户端启动类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);
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.main Dubbo triple stub server started
打开新的终端,启动客户端
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.main Received reply:message: "Hello,Demo!"
欢迎在 https://github.com/apache/dubbo 给 Dubbo Star。
搜索关注官方微信公众号:Apache Dubbo,了解更多业界最新动态,掌握大厂面试必备 Dubbo 技能
以上就是从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议()的详细内容,想要了解更多 从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。