grpc之java简单使用
- 在idea新建java项目,在main目录下新建proto,然后添加carrier.proto
syntax = "proto3";
package carrier;
option java_package = "com.wms.carrier";
// The greeting service definition.
service Carrier {
// Sends a greeting
rpc GetCarrier (CarrierRequest) returns (CarrierMsg) {}
rpc GetCarriers (CarriersRequest) returns(CarriersReply){}
}
// The request message containing the user's name.
message CarrierRequest {
string Name = 1;
}
// The response message containing the greetings
message CarriersRequest{
int64 Page = 1;
int64 PageSize = 2;
}
message CarriersReply{
int64 Page = 1;
int64 PageSize = 2;
repeated CarrierMsg CarrierInfo = 3;
}
message CarrierMsg{
int64 Id = 1;
string Name = 2;
}
2. 添加pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>grpc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
编译完后,在target目录找到文件复制到java下面
3. 在pom文件中添加依赖
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.58.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.58.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.58.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-core -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>1.58.0</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.24.0</version>
</dependency>
</dependencies>
- 在carrier文件夹中新建carrierServiceImpl实现rpc服务
package com.wms.carrier;
import com.wms.carrier.CarrierGrpc;
import com.wms.carrier.CarrierOuterClass;
import io.grpc.stub.StreamObserver;
public class carrierServiceImpl extends CarrierGrpc.CarrierImplBase{
@Override
public void getCarrier(com.wms.carrier.CarrierOuterClass.CarrierRequest req, StreamObserver<com.wms.carrier.CarrierOuterClass.CarrierMsg> responseObserver) {
com.wms.carrier.CarrierOuterClass.CarrierMsg carrierMsg = CarrierOuterClass.CarrierMsg.newBuilder().setId(1).setName("testddd").build();
responseObserver.onNext(carrierMsg);
responseObserver.onCompleted();
}
}
- 项目主目录新建server.java
package com.wms;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import com.wms.carrier.carrierServiceImpl;
import java.io.IOException;
public class server {
private static final int port = 50052;
public static void main(String[] args) throws IOException, InterruptedException {
//设置service端口
Server server = ServerBuilder.forPort(port)
.addService(new carrierServiceImpl())
.build().start();
System.out.println("*************************************************");
System.out.println(" rpc");
System.out.println(String.format(" 服务端启动成功, 端口号: %d", port));
System.out.println("*************************************************");
server.awaitTermination();
}
}
点击启动 6. 新建carrierClient.java客户端和postman请求
package com.wms.client;
import com.wms.carrier.CarrierOuterClass;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import com.wms.carrier.CarrierGrpc;
public class carrierClient {
private static final String host = "192.168.2.127";
private static final int serverPort = 50052;
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, serverPort).
usePlaintext()//无需加密或认证
.build();
try {
CarrierGrpc.CarrierBlockingStub carrierService = CarrierGrpc.newBlockingStub(channel);
CarrierOuterClass.CarrierRequest carrierRequest = CarrierOuterClass.CarrierRequest.newBuilder()
.setName("test")
.build();
CarrierOuterClass.CarrierMsg carrierMsg = carrierService.getCarrier(carrierRequest);
System.out.println(carrierMsg.getName());
} finally {
channel.shutdown();
}
}
}