gRPC中Java和node进行异构通信-互为客户端和服务端

265 篇文章 20 订阅 ¥9.90 ¥99.00
155 篇文章 2 订阅 ¥9.90 ¥99.00
31 篇文章 1 订阅

场景

gPRC简介以及Java中使用gPRC实现客户端与服务端通信(附代码下载):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108711541

上面介绍了在Java中使用gPRC进行通信的搭建方式。

如果是使用node作为客户端与Java的服务端进行通信 ,或者是node作为服务端与Java客户端进行通信怎么办。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

使用Java进行gPRC服务端和客户端的搭建参照上面,在上面搭建好的基础上,

打开WebStorm新建project,然后新建package.json

{
  "name": "grpc-examples",
  "version": "0.1.0",
  "dependencies": {
    "@grpc/proto-loader": "^0.1.0",
    "async": "^1.5.2",
    "google-protobuf": "^3.0.0",
    "grpc": "^1.11.0",
    "lodash": "^4.6.1",
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的示例,包括客户端服务端。首先,我们需要安装gRPC和protobuf库。 服务端代码: ```cpp #include <iostream> #include <memory> #include <string> #include <grpcpp/grpcpp.h> #include "helloworld.grpc.pb.h" using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using helloworld::HelloRequest; using helloworld::HelloReply; using helloworld::Greeter; // Logic and data behind the server's behavior. class GreeterServiceImpl final : public Greeter::Service { Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { std::string prefix("Hello "); reply->set_message(prefix + request->name()); return Status::OK; } }; void RunServer() { std::string server_address("0.0.0.0:50051"); GreeterServiceImpl service; grpc::ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); // Register "service" as the instance through which we'll communicate with clients. builder.RegisterService(&service); // Finally assemble the server. std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait(); } int main(int argc, char** argv) { RunServer(); return 0; } ``` 客户端代码: ```cpp #include <iostream> #include <memory> #include <string> #include <grpcpp/grpcpp.h> #include "helloworld.grpc.pb.h" using grpc::Channel; using grpc::ClientContext; using grpc::Status; using helloworld::HelloRequest; using helloworld::HelloReply; using helloworld::Greeter; class GreeterClient { public: GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} std::string SayHello(const std::string& user) { // Data we are sending to the server. HelloRequest request; request.set_name(user); // Container for the data we expect from the server. HelloReply reply; // Context for the client. It could be used to convey extra information to // the server and/or tweak certain RPC behaviors. ClientContext context; // The actual RPC. Status status = stub_->SayHello(&context, request, &reply); // Act upon its status. if (status.ok()) { return reply.message(); } else { std::cout << status.error_code() << ": " << status.error_message() << std::endl; return "RPC failed"; } } private: std::unique_ptr<Greeter::Stub> stub_; }; int main(int argc, char** argv) { // Create a channel to the server. std::shared_ptr<Channel> channel = grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()); GreeterClient greeter(channel); std::string user("world"); std::string reply = greeter.SayHello(user); std::cout << "Greeter received: " << reply << std::endl; return 0; } ``` 编译命令: ```bash $ g++ -std=c++11 -I./include -L./lib -o server server.cpp helloworld.grpc.pb.cc helloworld.pb.cc -lgrpc++ -lgrpc -lprotobuf -lpthread $ g++ -std=c++11 -I./include -L./lib -o client client.cpp helloworld.grpc.pb.cc helloworld.pb.cc -lgrpc++ -lgrpc -lprotobuf -lpthread ``` 运行命令: ```bash $ ./server $ ./client ``` 运行结果: ```bash # server console Server listening on 0.0.0.0:50051 # client console Greeter received: Hello world ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值