增加protobuf样例
This commit is contained in:
parent
3d6a565452
commit
0ce48edd0f
53
pom.xml
53
pom.xml
@ -29,6 +29,12 @@
|
||||
<version>3.3.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
<version>3.19.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
@ -37,6 +43,13 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>kr.motd.maven</groupId>
|
||||
<artifactId>os-maven-plugin</artifactId>
|
||||
<version>1.5.0.Final</version>
|
||||
</extension>
|
||||
</extensions>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
@ -55,6 +68,46 @@
|
||||
<executable>${basedir}/src/main/native/build.sh</executable>
|
||||
</configuration>
|
||||
</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>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
23
src/main/java/com/zeekling/cn/rpc/protobuf/Client.java
Normal file
23
src/main/java/com/zeekling/cn/rpc/protobuf/Client.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
25
src/main/java/com/zeekling/cn/rpc/protobuf/Server.java
Normal file
25
src/main/java/com/zeekling/cn/rpc/protobuf/Server.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package com.zeekling.cn.rpc.protobuf;
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user