增加protobuf样例

This commit is contained in:
LingZhaoHui 2024-01-24 00:25:56 +08:00
parent 3d6a565452
commit 0ce48edd0f
Signed by: zeekling
GPG Key ID: D96E4E75267CA2CC
8 changed files with 152 additions and 0 deletions

53
pom.xml
View File

@ -29,6 +29,12 @@
<version>3.3.6</version> <version>3.3.6</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.19.1</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -37,6 +43,13 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>exec-maven-plugin</artifactId> <artifactId>exec-maven-plugin</artifactId>
@ -55,6 +68,46 @@
<executable>${basedir}/src/main/native/build.sh</executable> <executable>${basedir}/src/main/native/build.sh</executable>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>protoc-java</pluginId>
<!--读取proto文件路径-->
<protoSourceRoot>${project.basedir}/src/main/java/com/zeekling/cn/rpc/protobuf/proto</protoSourceRoot>
<!--生产的java文件路径-->
<outputDirectory>${project.basedir}/target/generated-sources/protobuf</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources/protobuf</source>
<!-- 我们可以通过在这里添加多个source节点来添加任意多个源文件夹 -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1 @@
package com.zeekling.cn.rpc.protobuf;

View File

@ -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;
}

View File

@ -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);
}