简易使用
一. 包结构
二.maven引用
- parent的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>
<parent>
<groupId>com.zzc</groupId>
<artifactId>Demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.zzc.rpc</groupId>
<artifactId>rpc-design</artifactId>
<packaging>pom</packaging>
<modules>
<module>grpc</module>
<module>grpc-design</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<grpc.version>1.65.1</grpc.version>
<protostuff.version>1.8.0</protostuff.version>
<rpc-grpc-impl.version>1.3.14</rpc-grpc-impl.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-util</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-grpc-impl</artifactId>
<version>${rpc-grpc-impl.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 当前child模块的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>
<parent>
<groupId>com.zzc.rpc</groupId>
<artifactId>rpc-design</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.zzc.rpc.grpc</groupId>
<artifactId>grpc</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
</dependency>
<!-- log4j2日志门面 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<!-- log4j2日志框架 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- slf4j日志门面 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!--log4j2的适配器,为slf4j绑定日志框架 -->
<!-- 依赖org.slf4j:slf4j-api:1.7.25 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</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>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.22.4:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.65.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 备注:日志log4j2的版本,实际操作是日志单独做一个模块,并且进行拓展和日志分类等
<log4j.version>2.21.1</log4j.version>
<slf4j.version>1.7.36</slf4j.version>
三.创建协议
- 创建helloworld.proto文件
syntax = "proto3";
option java_multiple_files = true;
//生成java代码的package
option java_package = "com.zzc.rpc.grpc.entity";
//创建的javaBean的文件名
option java_outer_classname = "DemoProto";
//可以生成rpc接口
//option java_generic_services = true;
//声明一个服务名称
service DemoService {
//请求参数DemoRequest 响应参数DemoResponse
rpc Hello (DemoRequest) returns (DemoResponse);
}
message DemoRequest {
string name = 1;
}
message DemoResponse {
string msg = 1;
}
四.利用helloworld.proto生成java文件
- 在idea开发中,使用maven的compile操作进行生成。
五.demo代码
- 将生成的代码文件"DemoServiceGrpc.Java"拷贝出来
- 创建server端:“DemoServer”
package com.zzc.rpc.grpc;
import com.zzc.rpc.grpc.entity.DemoRequest;
import com.zzc.rpc.grpc.entity.DemoResponse;
import com.zzc.rpc.grpc.service.DemoServiceGrpc;
import io.grpc.BindableService;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DemoServer {
private int port = 8001;
private Server server;
private void start() {
try {
server = ServerBuilder.forPort(port)
.addService((BindableService) new HelloWorldImpl())
.build()
.start();
} catch (Exception e) {
log.error("start error.", e);
}
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
DemoServer.this.stop();
}
});
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
private void blockUntilShutdown() {
if (server != null) {
try {
server.awaitTermination();
} catch (InterruptedException e) {
log.error("blockUntilShutdown error.", e);
}
}
}
class HelloWorldImpl extends DemoServiceGrpc.DemoServiceImplBase {
@Override
public void hello(DemoRequest request, StreamObserver<DemoResponse> responseObserver) {
log.info("hello:{}", request.getName());
DemoResponse response = DemoResponse.newBuilder().setMsg("response:" + request.getName()).build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
public static void main(String[] args) {
DemoServer server = new DemoServer();
server.start();
server.blockUntilShutdown();
}
}
- 创建client端
package com.zzc.rpc.grpc;
import com.zzc.rpc.grpc.entity.DemoRequest;
import com.zzc.rpc.grpc.entity.DemoResponse;
import com.zzc.rpc.grpc.service.DemoServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
@Slf4j
public class DemoClient {
private final ManagedChannel channel;
private final DemoServiceGrpc.DemoServiceBlockingStub blockingStub;
public DemoClient(String host, int port) {
this.channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
this.blockingStub = DemoServiceGrpc.newBlockingStub(channel);
}
public void shutdown() {
try {
channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("");
}
}
public void send(String name) {
DemoRequest request = DemoRequest.newBuilder().setName(name).build();
DemoResponse response = blockingStub.hello(request);
log.info("response msg:{}", response.getMsg());
}
public static void main(String[] args) {
DemoClient client = new DemoClient("127.0.0.1", 8001);
for (int i = 0; i < 10; i ++) {
client.send("hello world!!! " + (i + 1));
}
client.shutdown();
}
}
附录
参考资料
https://github.com/grpc/grpc-java
https://github.com/protocolbuffers/protobuf/tree/main/examples
https://grpc.io/docs/languages/java/quickstart/