一、 参考资料
参考孙帅suns教程
https://www.bilibili.com/video/BV13M41157gU/?p=3&spm_id_from=333.880.my_history.page.click&vd_source=4cd1b6f268e2a29a11bea5d2568836ee
二、 服务端
项目目录
maven构建项目
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sht</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.32.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.32.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.32.1</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</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.12.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.32.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Product.proto
syntax = "proto3";
option java_multiple_files = false;
option java_package = "com.sht.server.proto";
option java_outer_classname = "ProductProto";
message ProductReq{
string pid = 1;
int32 weight = 2;
}
message ProductResp{
string result = 1;
}
service ProductService{
rpc add(ProductReq) returns(ProductResp){}
}
maven build compile编译proto文件为java文件
实现服务端服务
ProductService.java
package com.sht.server.service;
import com.sht.server.proto.ProductProto;
import com.sht.server.proto.ProductProto.ProductReq;
import com.sht.server.proto.ProductProto.ProductResp;
import com.sht.server.proto.ProductProto.ProductResp.Builder;
import com.sht.server.proto.ProductServiceGrpc;
import io.grpc.stub.StreamObserver;
public class ProductService extends ProductServiceGrpc.ProductServiceImplBase{
@Override
public void add(ProductReq request, StreamObserver<ProductResp> responseObserver) {
System.out.println("PID:"+request.getPid());
System.out.println("weight:"+request.getWeight());
Builder builder = ProductProto.ProductResp.newBuilder();
builder.setResult("success");
ProductResp productResp =builder.build();
responseObserver.onNext(productResp);
responseObserver.onCompleted();
}
}
对外暴露服务
App.java
package com.sht.server;
import java.io.IOException;
import com.sht.server.service.ProductService;
import io.grpc.Server;
import io.grpc.ServerBuilder;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
//GRPC服务端
ServerBuilder serverBuilder = ServerBuilder.forPort(9000);
serverBuilder.addService(new ProductService());
Server server = serverBuilder.build();
try {
server.start();
server.awaitTermination();
} catch (Exception e) {
System.out.println("RPC服务异常");
e.printStackTrace();
}
}
}
三、 客户端
调用服务
App.java
package com.sht.client;
import com.sht.server.proto.ProductProto;
import com.sht.server.proto.ProductProto.ProductReq;
import com.sht.server.proto.ProductProto.ProductResp;
import com.sht.server.proto.ProductProto.ProductReq.Builder;
import com.sht.server.proto.ProductServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
// 创建通信管道
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000).usePlaintext().build();
// 获得代理对象
ProductServiceGrpc.ProductServiceBlockingStub productService = ProductServiceGrpc
.newBlockingStub(managedChannel);
//准备参数
Builder builder = ProductProto.ProductReq.newBuilder();
builder.setPid("xxxxxxx");
builder.setWeight(123);
ProductReq productReq = builder.build();
//调用
ProductResp response = productService.add(productReq);
System.out.println("rpc调用结果:" + response.getResult());
//关闭通道
managedChannel.shutdown();
}
}