[英]gRPC in CPP providing TLS support
在 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) 的未定義引用我已經包含了所有必要的文件..
你的代碼看起來不錯。 如果您是從examples/cpp/helloworld
改編,則需要將 Makefile 中的-lgrpc++_unsecure
-lgrpc++
更改為-lgrpc++
。
為了他人的利益,可以在https://github.com/grpc/grpc/blob/master/test/cpp/interop/server_helper.cc#L50找到使用 tls/ssl 代碼的示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.