Ruby 版服务器框架
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
前提条件
服务器实现所需的 Gem:
- google-protobuf(本教程中使用的是 3.2.X)
- grpc(本教程中使用的是 1.2.X)
下载服务定义,然后创建以下目录结构:
[base_dir]
├── certificates
├── lib
├── protos
└── booking_service.proto
└── server.rb
根据接口说明生成 Ruby 库:
$ cd [base_dir]
$ grpc_tools_ruby_protoc -I protos --ruby_out=lib --grpc_out=lib protos/booking_service.proto
实现服务器
如果您需要框架代码,请咨询 Google 联系人。
在不使用 TLS 的情况下测试服务器
对于初始测试,可以停用 TLS:
$ cd [base_dir]
$ ruby server.rb --disable_tls
这不适合用于生产环境!
如需在服务器上启用 TLS,需要以下文件:
certificates/server.pem
服务器的证书链(采用 PEM 格式)
certificates/server.key
服务器证书链的私钥
certificates/trusted_client_roots.pem
在对客户端进行身份验证时信任的根证书
在对客户端进行身份验证时,会使用一组可信客户端根证书。您可以选择从 Mozilla 等权威机构获取这组受信任的根证书,也可以安装 Google Internet Authority G2 目前推荐的一组根证书。在后一种情况下,您有时可能需要手动更新根证书。
最终目录结构
[base_dir]
├── certificates
├── server.pem
├── server.key
└── trusted_client_roots.pem
├── lib
├── booking_service_pb.rb
└── booking_service_services_pb.rb
├── protos
└── booking_service.proto
└── server.rb
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThis guide outlines the necessary steps to set up a Ruby-based gRPC server, including the required gems: \u003ccode\u003egoogle-protobuf\u003c/code\u003e and \u003ccode\u003egrpc\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou'll need to download the service definition file (\u003ccode\u003ebooking_service.proto\u003c/code\u003e) and organize it within a specific directory structure that includes \u003ccode\u003ecertificates\u003c/code\u003e, \u003ccode\u003elib\u003c/code\u003e, \u003ccode\u003eprotos\u003c/code\u003e, and \u003ccode\u003eserver.rb\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eGenerating the necessary Ruby libraries from the service definition file using the \u003ccode\u003egrpc_tools_ruby_protoc\u003c/code\u003e command is required to implement the server.\u003c/p\u003e\n"],["\u003cp\u003eThe server can be initially tested without TLS using the command \u003ccode\u003eruby server.rb --disable_tls\u003c/code\u003e, but this is not recommended for production environments.\u003c/p\u003e\n"],["\u003cp\u003eEnabling TLS in production requires configuring the server with \u003ccode\u003eserver.pem\u003c/code\u003e, \u003ccode\u003eserver.key\u003c/code\u003e, and \u003ccode\u003etrusted_client_roots.pem\u003c/code\u003e files within the \u003ccode\u003ecertificates\u003c/code\u003e directory, to ensure secure communication.\u003c/p\u003e\n"]]],["The implementation requires the `google-protobuf` and `grpc` gems. Download the service definition, create the specified directory structure, and generate Ruby libraries using `grpc_tools_ruby_protoc`. TLS can be initially disabled using `--disable_tls` for testing. Production requires `server.pem`, `server.key`, and `trusted_client_roots.pem` within the certificates directory for TLS. The `trusted_client_roots.pem` may come from an authority like Mozilla or Google Internet Authority G2.\n"],null,["# Server skeleton for Ruby\n\nPrerequisites\n-------------\n\nGems required for the server implementation:\n\n- google-protobuf (3.2.X used in this tutorial)\n- grpc (1.2.X used in this tutorial)\n\nDownload the [service definition](https://dl.google.com/mapsbooking/apitemplate/v1alpha/booking_service.proto) and create this directory structure: \n\n [base_dir]\n ├── certificates\n ├── lib\n ├── protos\n └── booking_service.proto\n └── server.rb\n\nGenerate Ruby libraries from the interface description: \n\n $ cd [base_dir]\n $ grpc_tools_ruby_protoc -I protos --ruby_out=lib --grpc_out=lib protos/booking_service.proto\n\nImplement the server\n--------------------\n\nIf you need the skeleton code, ask a Google POC.\n\nTest the server without TLS\n---------------------------\n\nFor initial testing, TLS can be disabled: \n\n $ cd [base_dir]\n $ ruby server.rb --disable_tls\n\nThis is unsuitable for production use!\n\nConfigure production certificates\n---------------------------------\n\nTo enable TLS on the server, the following files are required:\n\n- `certificates/server.pem` the certificate chain for the server in in PEM format\n- `certificates/server.key` the private key for the server certificate chain\n- `certificates/trusted_client_roots.pem` the root certificates that are trusted when authenticating clients\n\nThe set of trusted client root certificates is used when authenticating the\nclient. You can choose to obtain this set of trusted roots from an authority\nlike [Mozilla](https://wiki.mozilla.org/CA:IncludedCAs) or install the [set of\nroots currently recommended by the Google Internet Authority\nG2](https://pki.goog/roots.pem). In the latter case, you may have to\nmanually update the root certificate at times.\n\nFinal directory structure\n-------------------------\n\n [base_dir]\n ├── certificates\n ├── server.pem\n ├── server.key\n └── trusted_client_roots.pem\n ├── lib\n ├── booking_service_pb.rb\n └── booking_service_services_pb.rb\n ├── protos\n └── booking_service.proto\n └── server.rb"]]