Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Use of gRPC of RPC (Java+Nodejs)

Shulou Source: shulou.com Published: 2022-06-03 06:48:55 09月23日 Update

This article introduces the process of using gRPC in Java. In general, it consists of the following three steps

1) define the services provided in the .proto file

2) compile the file using the protocol buffer compiler

3) use gRPC API to create the server and client, and make calls.

Download and install the required software

1) Protocol Buffers

Structured data serialization mechanism

Https://github.com/protocolbuffers/protobuf/releases

Examples of use:

Protoc-- java_out=./ * .proto

2) protoc-gen-grpc-java

Used to generate RPC communication code

Http://jcenter.bintray.com/io/grpc/protoc-gen-grpc-java/

Use example [use Protobuf-Plugin mechanism]:

Protoc--plugin=protoc-gen-grpc-java=protoc-gen-grpc-java-1.19.0-windows-x86_64.exe-- grpc-java_out=./ * .proto uses Maven to build gRPC1) modify pmo.xml UTF-8 3.7.0 1.19.0 io.grpc grpc-netty-shaded ${grpc.version} Io.grpc grpc-protobuf ${grpc.version} io.grpc grpc-stub ${grpc.version} com.google.protobuf protobuf-java ${protobuf.version} com. Google.protobuf protobuf-java-util ${protobuf.version} kr.motd.maven os-maven-plugin 1.6.1 org.xolstice.maven.plugins Protobuf-maven-plugin 0.6.1 False com.google.protobuf:protoc:$ {protobuf.version}: exe:$ {os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:$ {grpc.version}: exe:$ {os.detected.classifier} Compile compile-custom

By default, the plug-in looks for the proto file under the src/main/proto path. Any subfolder under the path will be used as the package path in the proto file.

2) create the proto file (HarLog.proto) syntax = "proto3"; option java_multiple_files = true;option java_package = "com.ultrapower.ioss.proto"; option java_outer_classname = "HarLogProto" under src/main/proto; 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) compile the file mvn protobuf:compile or mvn compile

The following two folders are generated under target\ generated-sources\ protobuf

Tags: Files services clients clients paths generation code folders compilation two data mechanisms directories examples generally speaking three parameters sequences cases plug-ins Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information macOS Microsoft MySQL