From 0ce48edd0f711a23302553603fd772b437eea62b Mon Sep 17 00:00:00 2001 From: zeekling Date: Wed, 24 Jan 2024 00:25:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0protobuf=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 53 +++++++++++++++++++ .../cn/rpc/protobuf/BusinessServerPBImpl.java | 14 +++++ .../cn/rpc/protobuf/BusinessServicePB.java | 10 ++++ .../com/zeekling/cn/rpc/protobuf/Client.java | 23 ++++++++ .../com/zeekling/cn/rpc/protobuf/Server.java | 25 +++++++++ .../cn/rpc/protobuf/package-info.java | 1 + .../cn/rpc/protobuf/proto/BusinessProto.proto | 15 ++++++ .../protobuf/proto/BusinessServiceProto.proto | 11 ++++ 8 files changed, 152 insertions(+) create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServerPBImpl.java create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServicePB.java create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/Client.java create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/Server.java create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/package-info.java create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessProto.proto create mode 100644 src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessServiceProto.proto diff --git a/pom.xml b/pom.xml index c70248e..1c2ee46 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,12 @@ 3.3.6 + + com.google.protobuf + protobuf-java + 3.19.1 + + junit junit @@ -37,6 +43,13 @@ + + + kr.motd.maven + os-maven-plugin + 1.5.0.Final + + exec-maven-plugin @@ -55,6 +68,46 @@ ${basedir}/src/main/native/build.sh + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.5.0 + + com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier} + protoc-java + + ${project.basedir}/src/main/java/com/zeekling/cn/rpc/protobuf/proto + + ${project.basedir}/target/generated-sources/protobuf + + + + + compile + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.4 + + + add-source + generate-sources + + add-source + + + + ${basedir}/target/generated-sources/protobuf + + + + + + maven-compiler-plugin 3.8.1 diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServerPBImpl.java b/src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServerPBImpl.java new file mode 100644 index 0000000..3c1c2e2 --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServerPBImpl.java @@ -0,0 +1,14 @@ +package com.zeekling.cn.rpc.protobuf; + +import com.google.protobuf.RpcController; +import com.google.protobuf.ServiceException; +import com.zeekling.cn.rpc.protobuf.proto.Business; + +public class BusinessServerPBImpl implements BusinessServicePB { + + @Override + public Business.BusinessResponseProto buy(RpcController controller, Business.BusinessRequestProto request) throws ServiceException { + String result = request.getBrand() + " " + request.getNum() + " " + request.getPrice(); + return Business.BusinessResponseProto.newBuilder().setResult(result).build(); + } +} diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServicePB.java b/src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServicePB.java new file mode 100644 index 0000000..1cfaeac --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/BusinessServicePB.java @@ -0,0 +1,10 @@ +package com.zeekling.cn.rpc.protobuf; + + +import com.zeekling.cn.rpc.protobuf.proto.BusinessService; + +public interface BusinessServicePB extends BusinessService.BusinessServiceProto.BlockingInterface { + + public static final long versionID = 1L; + +} diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/Client.java b/src/main/java/com/zeekling/cn/rpc/protobuf/Client.java new file mode 100644 index 0000000..8922f2f --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/Client.java @@ -0,0 +1,23 @@ +package com.zeekling.cn.rpc.protobuf; + +import com.google.protobuf.ServiceException; +import com.zeekling.cn.rpc.protobuf.proto.Business; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.ipc.ProtobufRpcEngine; +import org.apache.hadoop.ipc.RPC; + +import java.io.IOException; +import java.net.InetSocketAddress; + +public class Client { + + public static void main(String[] args) throws IOException, ServiceException { + + Configuration conf = new Configuration(); + RPC.setProtocolEngine(conf, BusinessServicePB.class, ProtobufRpcEngine.class); + BusinessServicePB proxy = RPC.getProxy(BusinessServicePB.class, BusinessServicePB.versionID, new InetSocketAddress("localhost", 12345), conf); + Business.BusinessResponseProto result = proxy.buy(null, Business.BusinessRequestProto.newBuilder().setBrand("apple").setNum(1).setPrice(12300).build()); + System.out.println(result); + } + +} diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/Server.java b/src/main/java/com/zeekling/cn/rpc/protobuf/Server.java new file mode 100644 index 0000000..2b6df35 --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/Server.java @@ -0,0 +1,25 @@ +package com.zeekling.cn.rpc.protobuf; + +import java.io.IOException; + +import com.zeekling.cn.rpc.protobuf.proto.BusinessService; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.ipc.ProtobufRpcEngine; +import org.apache.hadoop.ipc.RPC; + +public class Server { + + public static void main(String[] args) throws IOException { + Configuration conf = new Configuration(); + RPC.setProtocolEngine(conf, BusinessServicePB.class, ProtobufRpcEngine.class); + + RPC.Server server = new RPC.Builder(conf) + .setProtocol(BusinessServicePB.class) + .setInstance(BusinessService.BusinessServiceProto.newReflectiveBlockingService(new BusinessServerPBImpl())) + .setBindAddress("localhost") + .setPort(12345) + .build(); + server.start(); + } + +} diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/package-info.java b/src/main/java/com/zeekling/cn/rpc/protobuf/package-info.java new file mode 100644 index 0000000..e028d6b --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/package-info.java @@ -0,0 +1 @@ +package com.zeekling.cn.rpc.protobuf; \ No newline at end of file diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessProto.proto b/src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessProto.proto new file mode 100644 index 0000000..d9337f4 --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessProto.proto @@ -0,0 +1,15 @@ +syntax = "proto2"; +option java_package = "com.zeekling.cn.rpc.protobuf.proto"; +option java_outer_classname = "Business"; +option java_generic_services = true; +option java_generate_equals_and_hash = true; + +message BusinessRequestProto { + required string brand = 1; + required int64 num = 2; + required double price = 3; +} + +message BusinessResponseProto { + required string result = 1; +} diff --git a/src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessServiceProto.proto b/src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessServiceProto.proto new file mode 100644 index 0000000..aca6023 --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/protobuf/proto/BusinessServiceProto.proto @@ -0,0 +1,11 @@ +syntax = "proto2"; + +import "BusinessProto.proto"; +option java_package = "com.zeekling.cn.rpc.protobuf.proto"; +option java_outer_classname = "BusinessService"; +option java_generic_services = true; +option java_generate_equals_and_hash = true; + +service BusinessServiceProto { + rpc buy(BusinessRequestProto) returns (BusinessResponseProto); +} \ No newline at end of file