Windows配置grpc
Windows配置grpc
- 方法一
- 1. 使用git下载grph
- 下载速度慢可以使用国内镜像
- 1.1 更新子模块
- 2. 使用Cmake进行编译
- 2.1 GUI编译
- 2.2 命令行直接编译
- 3. 使用Visual Studio 生成解决方法
- 方法二
- 1. 安装 vcpkg
- 3.配置vckg的环境变量
- 2. 使用 vcpkg 安装 gRPC
- 3. 安装 Protobuf
- 4. 配置 CMake
- 5. 让 vcpkg 自动集成
- 6. 编译 gRPC 项目
- 测试是否安装成功
- Visual Studio 配置 grpc
- 第一个grpc demo
- 1. 创建demo.proto文件,写入一下内容:
- 2. 编译demo.proto文件,生成的proc.exe生成proto的头文件和源文件
- 3. 生成grpc类需要的pb文件,因为要序列化数据。
- 5. 分别建立server和client的项目
- 4. 将生成的proto的头文件和源文件,grpc类需要的pb文件拷贝到server和client的项目
方法一
1. 使用git下载grph
git clone -b v1.70.0 --recurse-submodules https://github.com/grpc/grpc.git
# 指定版本下载
# git clone -b v1.41.0 https://github.com/grpc/grpc
下载速度慢可以使用国内镜像
git clone --recurse-submodules https://gitclone.com/github.com/grpc/grpc.git
1.1 更新子模块
git submodule update --init --recursive
2. 使用Cmake进行编译
2.1 GUI编译
选自己的vs版本
默认设置 (可修改文件存放位置)
2.2 命令行直接编译
mkdir -p cmake/build
cd cmake/build
cmake ../..
3. 使用Visual Studio 生成解决方法
打开项目中
右击重新生成解决方法
方法二
1. 安装 vcpkg
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
bootstrap-vcpkg.bat
3.配置vckg的环境变量
setx Path "%Path%;C:\new_path"
2. 使用 vcpkg 安装 gRPC
vcpkg install grpc:x64-windows
3. 安装 Protobuf
vcpkg install protobuf:x64-windows
4. 配置 CMake
在 CMakeLists.txt 中添加:
find_package(Protobuf REQUIRED)
find_package(gRPC CONFIG REQUIRED)add_executable(my_server server.cpp)
target_link_libraries(my_server PRIVATE gRPC::grpc++ protobuf::libprotobuf)
5. 让 vcpkg 自动集成
vcpkg integrate install
6. 编译 gRPC 项目
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=D:/vcpkg/scripts/buildsystems/vcpkg.cmake
# 生成Release版本:cmake --build . --config Release -j 4
cmake --build .
测试是否安装成功
进入 示例目录 并运行 greeter_server 和 greeter_client:
cd examples/cpp/helloworld
如果你使用 CMake:
mkdir build && cd build
cmake ..
make -j # 或者使用VS生成解决方法
如果你使用 vcpkg:
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=D:/vcpkg/scripts/buildsystems/vcpkg.cmake
# 生成Release版本:cmake --build . --config Release -j 4
cmake --build . # # 或者使用VS生成解决方法
运行 gRPC 示例
cd Debug
./greeter_server
./greeter_client
Visual Studio 配置 grpc
- 配置包含目录
- 配置库目录
- 配置依赖项
abseil_dll.lib
absl_decode_rust_punycode.lib
absl_demangle_rust.lib
absl_flags_commandlineflag.lib
absl_flags_commandlineflag_internal.lib
absl_flags_config.lib
absl_flags_internal.lib
absl_flags_marshalling.lib
absl_flags_parse.lib
absl_flags_private_handle_accessor.lib
absl_flags_program_name.lib
absl_flags_reflection.lib
absl_flags_usage.lib
absl_flags_usage_internal.lib
absl_log_flags.lib
absl_poison.lib
absl_utf8_for_code_point.lib
cares.lib
address_sorting.lib
gpr.lib
grpc.lib
grpc_authorization_provider.lib
grpc_plugin_support.lib
grpc_unsecure.lib
grpc++.lib
grpc++_alts.lib
grpc++_error_details.lib
grpc++_reflection.lib
grpc++_unsecure.lib
grpcpp_channelz.lib
upb_base_lib.lib
upb_json_lib.lib
upb_mem_lib.lib
upb_message_lib.lib
upb_mini_descriptor_lib.lib
upb_textformat_lib.lib
upb_wire_lib.lib
re2.lib
zlibd.lib
libprotobufd.lib
libprotobuf-lited.lib
libprotocd.lib
第一个grpc demo
1. 创建demo.proto文件,写入一下内容:
syntax = "proto3";
package hello;
service Greeter {rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {string message = 1;
}
message HelloReply {string message = 1;
}
2. 编译demo.proto文件,生成的proc.exe生成proto的头文件和源文件
protoc -I="." --grpc_out="." --plugin=protoc-gen-grpc="D:\OpenSource\vcpkg-2025.01.13\packages\grpc_x64-windows\tools\grpc\grpc_cpp_plugin.exe" "demo.proto"
3. 生成grpc类需要的pb文件,因为要序列化数据。
protoc -I="." --grpc_out="." "demo.proto"
5. 分别建立server和client的项目
server.cpp
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "D:\Workshops\vs_shop\grpc_learning\demo_server\demo.grpc.pb.h"using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using hello::HelloRequest;
using hello::HelloReply;
using hello::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("llfc grpc server has received : ");reply->set_message(prefix + request->message());return Status::OK;}
};
void RunServer() {std::string server_address("127.0.0.1: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) {RunServer();return 0;
}
client.cpp
#include <string>
#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "D:\Workshops\vs_shop\grpc_learning\demo_server\demo.grpc.pb.h"
using grpc::ClientContext;
using grpc::Channel;
using grpc::Status;
using hello::HelloReply;
using hello::HelloRequest;
using hello::Greeter;
// static method : Greeter::NewStub
class FCClient {
public:FCClient(std::shared_ptr<Channel> channel):stub_(Greeter::NewStub(channel)) {}std::string SayHello(std::string name) {ClientContext context;HelloReply reply;HelloRequest request;request.set_message(name);Status status = stub_->SayHello(&context, request, &reply);if (status.ok()) {return reply.message();}else {return "failure " + status.error_message();}}
private:std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char* argv[]) {auto channel = grpc::CreateChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials());FCClient client(channel);// block until get result from RPC serverstd::string result = client.SayHello("hello , llfc.club !");printf("get result [%s]\n", result.c_str());std::getchar();return 0;
}
4. 将生成的proto的头文件和源文件,grpc类需要的pb文件拷贝到server和client的项目
server项目
client的项目