Qt gRPC 简单应用

原创
2018/04/28 19:27
阅读数 5.2K

Qt 想在windows下运行起grpc服务简直天坑,在数十次失败后,用以下方法运行成功。

通过以下网址下载msys2 64位版(32位在windows 10上没跑起来):

https://www.msys2.org/

安装完成后,在开始菜单找到并运行“MSYS2 MSYS”,在命令框运行以下命令(10M光纤下载安装了两个多小时,请耐心等待):

pacman -Syu

pacman -S mingw-w64-x86_64-grpc mingw-w64-x86_64-qt mingw-w64-x86_64-qt-creator

pacman -S mingw-w64-x86_64-clang

pacman -S mingw-w64-x86_64-qt5-static

“安装盘\msys64\mingw64\bin” 加下环境变量Path里。

运行msys64\mingw64\bin\qtcreator.exe,启动QtCreator,新建Console项目(命名为QtgRPCConsole)。

为避免以下错误:

配置文件里加入:

DEFINES += _WIN32_WINNT=0x600

右击项目选择“添加库...->外部库”,指向“msys64\mingw64\lib\libgrpc.dll.a”,取消"为debug版本添加‘d’作为后缀"的复选框选项,完成后如下图:

增加其他grpc和protobuffer库名到配置文件:

unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc_cronet.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc_csharp_ext.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc_plugin_support.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc_unsecure.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc++.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc++_cronet.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc++_error_details.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc++_reflection.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibgrpc++_unsecure.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibprotobuf.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibprotobuf-lite.dll
unix|win32: LIBS += -LD:/msys64/mingw64/lib/ -llibprotoc.dll

INCLUDEPATH += D:/msys64/mingw64/include
DEPENDPATH += D:/msys64/mingw64/include

新建protobuffer文件helloworld.proto:

syntax = "proto3";

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

通过protoc生成gRPC类(从mingw/bin下拷贝grpc_cpp_plugin.exe到项目目录下面):

protoc -I ./ --grpc_out=./ --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe ./helloworld.proto
protoc -I ./  --cpp_out=./ ./helloworld.proto

右击项目“添加现有文件...”选择生成的grpc头文件和类(helloworld.grpc.pb.cc、helloworld.pb.cc、helloworld.grpc.pb.h、helloworld.pb.h)到项目中。

编写服务端代码:

#include <QCoreApplication>

#include <iostream>
#include <memory>
#include <string>

#include <grpc++/grpc++.h>
#include "helloworld.pb.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;

  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. In this case it corresponds to an *synchronous* service.
  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[])
{
//    QCoreApplication a(argc, argv);
    RunServer();
    return 0;
}

运行结果如下:

注:在发布版本时选择静态编译,然后把grpc的动态链接库放入同文件夹即可执行exe文件。

展开阅读全文
打赏
1
2 收藏
分享
加载中
更多评论
打赏
0 评论
2 收藏
1
分享
返回顶部
顶部