温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

RPC之gRPC的使用(Java+Nodejs)

发布时间:2020-06-13 22:37:52 来源:网络 阅读:2625 作者:mybabe0312 栏目:软件技术

本文介绍在Java中使用gRPC的过程。一般来说,主要包含以下的三个步骤
1)在.proto文件中定义提供的服务
2)使用protocol buffer编译器编译文件
3)使用gRPC API来创建服务端和客户端,并进行调用。

下载和安装需要的软件

1)Protocol Buffers
结构化数据序列化机制
https://github.com/protocolbuffers/protobuf/releases
使用示例:

protoc --java_out=./ *.proto

2)protoc-gen-grpc-java
用于生成RPC通信代码
http://jcenter.bintray.com/io/grpc/protoc-gen-grpc-java/
使用示例【使用Protobuf-Plugin机制】:

protoc --plugin=protoc-gen-grpc-java=protoc-gen-grpc-java-1.19.0-windows-x86_64.exe --grpc-java_out=./ *.proto

使用Maven构建gRPC

1)修改pmo.xml

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <protobuf.version>3.7.0</protobuf.version>
        <grpc.version>1.19.0</grpc.version>
    </properties>

    <dependencies>      
        <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>
        <!-- 确保运行时的版本号与protoc的版本号匹配(或更新) -->
        <dependency>
          <groupId>com.google.protobuf</groupId>
          <artifactId>protobuf-java</artifactId>
          <version>${protobuf.version}</version>
        </dependency>
        <!-- 如果您想使用像JsonFormat这样的特性 ,则添加以下依赖-->
        <dependency>
          <groupId>com.google.protobuf</groupId>
          <artifactId>protobuf-java-util</artifactId>
          <version>${protobuf.version}</version>
        </dependency>
    </dependencies>
    <build>
        <extensions>
            <extension>
                <!-- 该插件的说明参考 https://github.com/trustin/os-maven-plugin -->
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <!-- proto文件的所在路径 -->
                    <!-- <protoSourceRoot>${project.basedir}/src/main/resources/proto</protoSourceRoot> -->
                    <!-- 编译后java文件的输出路径,默认为${project.build.directory}/generated-sources/protobuf/java -->
                    <!--<outputDirectory> ${project.build.directory}/generated-sources/protobuf/java</outputDirectory> -->
                    <!-- 制定protoc编译器路径 -->
                    <!-- <protocExecutable></protocExecutable> -->
                    <!-- 定义生成的java文件输出路径 -->
                    <!--<outputDirectory>${project.build.sourceDirectory}</outputDirectory> -->
                    <!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件 -->
                    <clearOutputDirectory>false</clearOutputDirectory>
                    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

默认情况下,该插件会查找 src/main/proto路径下的proto文件。该在路径下的任何子文件夹都会作为proto文件中使用的包路径。

2)在src/main/proto下创建proto文件(HarLog.proto)

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.ultrapower.ioss.proto";
option java_outer_classname = "HarLogProto";

package com.ultrapower.grpc.har;

message HarLogResovleRequest{
    string url = 1 ;
    string file_name = 2;
    string file_dir = 3;
    string context = 4;
}

message HarLogResovleResponse{
    int32 code = 1 ;
}

service HarLogService{
    rpc ResovleHarLog(HarLogResovleRequest) returns (HarLogResovleResponse);
    rpc BatchResovleHarLog(HarLogResovleRequest) returns (HarLogResovleResponse);

}

3)编译文件

mvn protobuf:compile 
或
mvn compile

在target\generated-sources\protobuf下会生成以下两个文件夹
RPC之gRPC的使用(Java+Nodejs)
其中java文件夹下面包含了我们定义的message,而grpc-java下存放的是服务端和客户端都要使用的service。
基于上面的proto文件,生成了两个java文件

1,HarLogServiceGrpc.java   位于grpc-java下
2,HarLogProto.java等其他文件位于java下

4)服务端服务实现类代码

package com.ultrapower.ioss.proto;

public class HarLogServiceImpl extends HarLogServiceGrpc.HarLogServiceImplBase {

    @Override
    public void resovleHarLog(com.ultrapower.ioss.proto.HarLogResovleRequest request,
            io.grpc.stub.StreamObserver<com.ultrapower.ioss.proto.HarLogResovleResponse> responseObserver) {
        System.out.println(request.getUrl());
        System.out.println(request.getFileName());
        System.out.println(request.getFileDir());
        System.out.println(request.getContext());

        responseObserver.onNext(HarLogResovleResponse.newBuilder().setCode(200).build());
        responseObserver.onCompleted();
    }

    @Override
    public void batchResovleHarLog(com.ultrapower.ioss.proto.HarLogResovleRequest request,
            io.grpc.stub.StreamObserver<com.ultrapower.ioss.proto.HarLogResovleResponse> responseObserver) {
        responseObserver.onNext(HarLogResovleResponse.newBuilder().setCode(200).build());
        responseObserver.onCompleted();
    }
}

5)服务端启动类

package com.ultrapower.ioss.proto;

import java.io.IOException;

import io.grpc.Server;
import io.grpc.ServerBuilder;

public class RpcServerApp {

    private Server server;
    private int port = 50051;

    public void start() throws IOException {
        server = ServerBuilder.forPort(port)
                .addService(new HarLogServiceImpl())
                .build()
                .start();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                RpcServerApp.this.stop();
            }
        });
    }

    private void stop() {
        if (server != null) {
            server.shutdown();
        }
    }

    private void blockUntilShutdown() throws InterruptedException {
        if (server != null) {
            server.awaitTermination();
        }
    }

    public static void main(String[] args) throws Exception {
        final RpcServerApp server = new RpcServerApp();
        server.start();
        server.blockUntilShutdown();
    }

}

Nodejs端客户端

1)添加依赖

cnpm install --save grpc-tools
cnpm install --save google-protobuf 
cnpm install --save grpc
cnpm install --save @grpc/proto-loader
cnpm install --save @grpc/grpc-js

2)客户端代码

var grpc = require('grpc');
var PROTO_PATH = __dirname + '/../grpc/protos/HarLog.proto';
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
//protoDescriptor + "文件中定义的package"
var harlog = protoDescriptor.com.ultrapower.grpc.har;

/*
    创建RPC客户端
*/
function getGrpcClient(){
    //创建客户端
    let client = new harlog.HarLogService('localhost:50051',grpc.credentials.createInsecure());
    return client ; 
}

/*
    请求JAVA端解析生成的某个HAR文件
*/
function resovleHarLog(request){
    let client = getGrpcClient() ;
    //请求数据
    /*
    let request = {
            url:'',
            file_name:'',
            file_dir:'',
            context:''
    } ;
    */
    client.resovleHarLog(request,function(error,response){
        //console.log('Greeting:', response.code);
        console.log('Greeting:', JSON.stringify(response));
    })
}
/*
    请求JAVA端解析指定目录下所有的HAR文件
    @param dirPath 存放HAR文件的目录路径
*/
function batchResovleHarlog(request){
    let client = getGrpcClient() ;
    client.batchResovleHarLog(request,function(error,response){
        console.log('Greeting:', response.message);
    })
}

//测试
resovleHarLog({
            url:'www.baidu.com',
            file_name:'bai.jar',
            file_dir:'c://d',
            context:'dsfdfdsd'
    } ) ;

exports.resovleHarLog = resovleHarLog;
exports.batchResovleHarlog = batchResovleHarlog;

基于Java的客户端

package com.ultrapower.ioss.proto;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.NegotiationType;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;

public class Client {

    private final ManagedChannel channel;

    private final HarLogServiceGrpc.HarLogServiceBlockingStub blockingStub;

    /**
     * 创建服务端连接,并创建“桩”
     */
    public Client(String host, int port) {
        channel = NettyChannelBuilder.forAddress(host, port).negotiationType(NegotiationType.PLAINTEXT).build();
        blockingStub = HarLogServiceGrpc.newBlockingStub(channel);
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    /**
     * 向服务端发送请求
     */
    public void resovleHarLog() {
        try {
            HarLogResovleRequest request = HarLogResovleRequest.newBuilder().setUrl("www.baidu.com").setFileName("test")
                    .build();
            HarLogResovleResponse response = blockingStub.resovleHarLog(request);
            System.out.println("result from server: " + response.getCode());
        } catch (RuntimeException e) {
            System.out.println("RPC failed:" + e.getMessage());
            return;
        }
    }

    public static void main(String[] args) throws Exception {
        Client client = new Client("127.0.0.1", 50051);
        try {
            client.resovleHarLog();
        } finally {
            client.shutdown();
        }
    }
}

基于Nodejs的服务端

var grpc = require('grpc');
var PROTO_PATH = __dirname + '/../grpc/protos/HarLog.proto';
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
//protoDescriptor + "文件中定义的package"
var harlog = protoDescriptor.com.ultrapower.grpc.har;

/*
    远程调用的resovleHarLog方法.
    @param call 请求, 通过call.request可以得到请求参数
    @param callback,调用完成后返回给调用端的结果
*/
function resovleHarLog(call, callback) {
    let req = call.request ;
    console.log(JSON.stringify(req)) ;
  //callback(null, {message: 'Hello ' + call.request.name});
  callback(null, {code: 300});
}

function batchResovleHarLog(call, callback) {
    let req = call.request ;
    console.log(JSON.stringify(req)) ;
  //callback(null, {message: 'Hello again, ' + call.request.name});
  callback(null, {code: 300});
}

/*
    创建并启动服务端
*/
function startup() {
  var server = new grpc.Server();
  server.addProtoService(harlog.HarLogService.service,
                         {resovleHarLog: resovleHarLog, batchResovleHarLog: batchResovleHarLog});
  server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure());
  server.start();
}

//启动服务
startup() ;

exports.startup = startup;
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI