CPP中的gRPC提供TLS支持

时间:2015-09-26 00:08:38

标签: c++ grpc

在CPP中使用TLS的gRPC服务器的任何示例??

我正在尝试构建一个gRPC应用程序。如果客户端想要通过TLS而不是TCP连接,则服务器应提供TLS支持。

这是我的服务器

void RunServer() {
    std::string server_address("0.0.0.0:50051");
    GreeterServiceImpl service;
    ServerBuilder builder;
    std::shared_ptr<ServerCredentials> creds;

    if(enable_ssl)
    {
            grpc::SslServerCredentialsOptions::PemKeyCertPair pkcp ={"a","b"};
            grpc::SslServerCredentialsOptions ssl_opts;
            ssl_opts.pem_root_certs="";
            ssl_opts.pem_key_cert_pairs.push_back(pkcp);
            creds = grpc::SslServerCredentials(ssl_opts);
    }
    else
            creds=grpc::InsecureServerCredentials();
    // Listen on the given address without any authentication mechanism.
    builder.AddListeningPort(server_address, creds);
    // 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());

错误: 对grpc :: SslServerCredetials(grpc :: ssl_opts)的未定义引用 我已经包含了所有必要的文件..

1 个答案:

答案 0 :(得分:1)

您的代码看起来正确。如果您要从examples/cpp/helloworld进行调整,则需要在Makefile中将-lgrpc++_unsecure更改为-lgrpc++

为了他人的利益,可以在https://github.com/grpc/grpc/blob/master/test/cpp/interop/server_helper.cc#L50找到使用tls / ssl代码的示例

相关问题