首页 /  技术专区  /  gRPC 宽屏模式 >

gRPC用法Demo

什么是RPC?

  • RPC(Remote Procedure Call)远程过程调用,简单的理解是一个节点请求另一个节点提供的服务

什么是gRPC?

gRPC是谷歌的一套rpc框架。

在gRPC中,客户端应用程序可以直接在其他计算机上的服务器应用程序上调用方法,就好像它是本地对象一样,从而使您更轻松地创建分布式应用程序和服务。与许多RPC系统一样,gRPC围绕定义服务的思想,指定可通过其参数和返回类型远程调用的方法。在服务器端,服务器实现此接口并运行gRPC服务器以处理客户端调用。在客户端,客户端具有一个存根(在某些语言中仅称为客户端),提供与服务器相同的方法。

image.png

1、新建maven项目

image.png

2、pom.xml文件配置

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <grpc.version>1.36.1</grpc.version>
    <protobuf.version>3.12.0</protobuf.version>
    <protoc.version>3.12.0</protoc.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-bom</artifactId>
            <version>${grpc.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty-shaded</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java-util</artifactId>
        <version>${protobuf.version}</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:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                <protoSourceRoot>src/main/resources/proto</protoSourceRoot>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

protoSourcesRoot是proto文件路径。

image.png

3、我们在proto文件夹下面新建add.proto文件。

syntax = "proto3";

package grpc;
option java_package = "a.b.c";
option java_outer_classname = "AddServiceProto";
option java_multiple_files = true;

service AddService{
    rpc add(AddRequest) returns (AddReply){}
}

message AddRequest{
    int32 a = 1;
    int32 b = 2;
}

message AddReply{
    int32 res = 1;
}

4、进行下图的操作

image.png

5、src/main/java中创建a.b.c包,并在下面创建AddServer.java服务端文件

package a.b.c;

import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.IOException;

public class AddServer extends AddServiceGrpc.AddServiceImplBase{

    public static void main(String[] args) throws IOException {
        ServerBuilder.forPort(9999)
                .addService(new AddServer())
                .build()
                .start();
        System.out.println("server start at 9999");

        while(true);
    }

    public void add(AddRequest request, StreamObserver<AddReply> responseObserver) {
        int res = myAdd(request.getA(), request.getB());
        responseObserver.onNext(AddReply.newBuilder().setRes(res).build());
        responseObserver.onCompleted();
    }

    private int myAdd(int a, int b){
        return a + b;
    }
}

6、接着创建服务端代码AddClient.java

package a.b.c;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class AddClient {

    ManagedChannel channel;
    AddServiceGrpc.AddServiceBlockingStub stub;

    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        AddClient client = new AddClient();
        AddReply reply = client.stub.add(AddRequest.newBuilder()
                .setA(a)
                .setB(b)
                .build());
        System.out.println(reply.getRes());
    }

    public AddClient(){
        channel = ManagedChannelBuilder
                .forAddress("127.0.0.1", 9999)
                .usePlaintext()
                .build();
        stub = AddServiceGrpc.newBlockingStub(channel);
    }
}

7、首先启动服务端

image.png

8、然后启动客户端

image.png


下一篇: Vert.x简介
头像
0/200
图片验证码