汉字魔法师
118.67M · 2026-02-04
在早期的服务通信中,我们最常见的方案无非两种:
一种是 HTTP + JSON,简单直观,但性能和规范性始终是瓶颈;
另一种是自己造 RPC 轮子,成本高、维护难、协议混乱。
gRPC 的出现,本质上就是在解决一个问题:
如何让跨进程、跨机器的服务调用,像本地函数调用一样自然?
gRPC 是 Google 开源的一套高性能 RPC 框架,核心特点有三个:
这三个设计,决定了 gRPC 天生就不是“写给小脚本用的”,而是面向中大型系统、微服务架构的通信基础设施。
优点:
缺点:
一句话总结:
gRPC 非常适合“服务与服务之间”,但并不适合“直接暴露给人类使用”。
下面用一个最简单的 HelloService,演示完整流程。
sudo apt-get update
sudo apt-get install -y build-essential autoconf libtool pkg-config
# 安装 protobuf 编译器
sudo apt-get install -y libprotobuf-dev protobuf-compiler
# 安装 gRPC 库和插件
sudo apt-get install -y libgrpc++-dev libgrpc-dev
syntax = "proto3";
package hello;
service HelloService {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
生成 C++ 代码:
protoc --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` hello.proto
protoc --cpp_out=. hello.proto
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "hello.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
class HelloServiceImpl final : public hello::HelloService::Service {
public:
Status SayHello(ServerContext* context,
const hello::HelloRequest* request,
hello::HelloReply* reply) override {
reply->set_message("Hello, " + request->name());
return Status::OK;
}
};
int main() {
std::string server_address("0.0.0.0:50051");
HelloServiceImpl service;
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();
return 0;
}
编译并启动服务
# 单文件编译
g++ -std=c++11 main.cpp hello.pb.cc hello.grpc.pb.cc
-o hello_server
`pkg-config --cflags --libs grpc++ grpc protobuf`
-lgrpc++_reflection
-lpthread
# 运行
./hello_server
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "hello.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
class HelloClient {
public:
HelloClient(std::shared_ptr<Channel> channel)
: stub_(hello::HelloService::NewStub(channel)) {}
std::string SayHello(const std::string& name) {
hello::HelloRequest request;
request.set_name(name);
hello::HelloReply reply;
ClientContext context;
Status status = stub_->SayHello(&context, request, &reply);
if (status.ok()) {
return reply.message();
} else {
return "RPC failed";
}
}
private:
std::unique_ptr<hello::HelloService::Stub> stub_;
};
int main() {
HelloClient client(
grpc::CreateChannel("localhost:50051",
grpc::InsecureChannelCredentials()));
std::cout << client.SayHello("gRPC C++") << std::endl;
return 0;
}
编译并运行
# 单文件编译
g++ -std=c++11 main.cpp hello.pb.cc hello.grpc.pb.cc
-o hello_client
`pkg-config --cflags --libs grpc++ grpc protobuf`
-lgrpc++_reflection
-lpthread
# 运行
./hello_client
启动服务端,再运行客户端,你会看到:
Hello, gRPC C++
很多人学 gRPC,卡在“看懂文档,却不知道怎么落地”。
真正跑通一次 C++ 服务端和客户端,你会发现:
gRPC 并不神秘,它只是把“服务调用”这件事,做得更像工程,而不是脚本。
如果你正在做 分布式系统、微服务、基础设施,gRPC 值得你认真掌握。