Skip to content

.proto文件 ​

c++
syntax = "proto3";
package hello;
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
  string message = 1;
}
message HelloReply {
  string message = 1;
}

grpc_client ​

c++
#include <string>
#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "demo.grpc.pb.h"
using grpc::ClientContext;
using grpc::Channel;
using grpc::Status;
using hello::HelloReply;
using hello::HelloRequest;
using hello::Greeter;
// 客户端
class Client {
public:
    Client(std::shared_ptr<Channel> channel)
        :stub_(Greeter::NewStub(channel)) {
    }
    std::string SayHello(std::string name) {
        ClientContext context; // 客户端上下文对象
        HelloReply reply; //在demo.proto里我们自定义的需要序列化传输的数据结构
        HelloRequest request; //在demo.proto里我们自定义的需要序列化传输的数据结构
        request.set_message(name); 
        Status status = stub_->SayHello(&context, request, &reply); // 阻塞 直到服务端响应 status状态码
        if (status.ok()) {
            return reply.message();
        }
        else {
            return "failure " + status.error_message();
        }
    }
private:
    std::unique_ptr<Greeter::Stub> stub_;//grpc连接对象
};
int main(int argc, char* argv[]) {
    //连接grpc服务器 
    std::shared_ptr<Channel> channel = grpc::CreateChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials());
    //生成客户端对象
    Client client(channel);
    //调用SayHello方法 等待服务器响应result
    std::string result = client.SayHello("hello , llfc.club !");
    printf("服务器回复: [%s]\n", result.c_str());
    return 0;
}

grpc_server ​

c++
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <grpcpp/grpcpp.h>
#include "demo.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;

/*以下三个都由protoc生成*/
using hello::HelloRequest;
using hello::HelloReply;
using hello::Greeter;

class GreeterServiceImpl final : public hello::Greeter::Service {
    //实现我们自定义的SayHello接口 在grpc中每个服务方法都有三个参数
    
    grpc::Status SayHello(
        grpc::ServerContext* context, 
        const hello::HelloRequest* request,
        hello::HelloReply* reply) override 
    
    {
        std::string prefix("hello world :  ");
        reply->set_message(prefix + request->message());
        return grpc::Status::OK;
    }
};


int main(int argc, char** argv) {
    std::string server_address("127.0.0.1:50051");
    //以下构造一个grpc服务
    GreeterServiceImpl service;
    grpc::ServerBuilder builder;
    // 监听给定的地址
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    
        
    builder.RegisterService(&service);
    // Finally assemble the server.
    std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;

    //启动一个线程定时关闭该服务
    /*
    std::thread([&server]() {
        int i = 0;
        std::cout << "服务15秒后关闭:\n";
        while (i++ < 15) {
            std::cout << i << "\n";
            Sleep(1000);
        }
        server->Shutdown();
        }).detach();
        */
    //启动服务
    server->Wait();
 
    return 0;
}

学 习 记 录