md_doc_server_reflection_tutorial.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.17"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>GRPC Objective-C: gRPC Server Reflection Tutorial</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="search/searchdata.js"></script>
  14. <script type="text/javascript" src="search/search.js"></script>
  15. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  16. </head>
  17. <body>
  18. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  19. <div id="titlearea">
  20. <table cellspacing="0" cellpadding="0">
  21. <tbody>
  22. <tr style="height: 56px;">
  23. <td id="projectalign" style="padding-left: 0.5em;">
  24. <div id="projectname">GRPC Objective-C
  25. &#160;<span id="projectnumber">1.36.1</span>
  26. </div>
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. <!-- end header part -->
  33. <!-- Generated by Doxygen 1.8.17 -->
  34. <script type="text/javascript">
  35. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  36. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  37. /* @license-end */
  38. </script>
  39. <script type="text/javascript" src="menudata.js"></script>
  40. <script type="text/javascript" src="menu.js"></script>
  41. <script type="text/javascript">
  42. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  43. $(function() {
  44. initMenu('',true,false,'search.php','Search');
  45. $(document).ready(function() { init_search(); });
  46. });
  47. /* @license-end */</script>
  48. <div id="main-nav"></div>
  49. <!-- window showing the filter options -->
  50. <div id="MSearchSelectWindow"
  51. onmouseover="return searchBox.OnSearchSelectShow()"
  52. onmouseout="return searchBox.OnSearchSelectHide()"
  53. onkeydown="return searchBox.OnSearchSelectKey(event)">
  54. </div>
  55. <!-- iframe showing the search results (closed by default) -->
  56. <div id="MSearchResultsWindow">
  57. <iframe src="javascript:void(0)" frameborder="0"
  58. name="MSearchResults" id="MSearchResults">
  59. </iframe>
  60. </div>
  61. </div><!-- top -->
  62. <div class="PageDoc"><div class="header">
  63. <div class="headertitle">
  64. <div class="title">gRPC Server Reflection Tutorial </div> </div>
  65. </div><!--header-->
  66. <div class="contents">
  67. <div class="textblock"><p>gRPC Server Reflection provides information about publicly-accessible gRPC services on a server, and assists clients at runtime to construct RPC requests and responses without precompiled service information. It is used by gRPC CLI, which can be used to introspect server protos and send/receive test RPCs.</p>
  68. <h1><a class="anchor" id="autotoc_md203"></a>
  69. Enable Server Reflection</h1>
  70. <h2><a class="anchor" id="autotoc_md204"></a>
  71. Enable server reflection in C++ servers</h2>
  72. <p>C++ Server Reflection is an add-on library, <code>libgrpc++_reflection</code>. To enable C++ server reflection, you can link this library to your server binary.</p>
  73. <p>Some platforms (e.g. Ubuntu 11.10 onwards) only link in libraries that directly contain symbols used by the application. On these platforms, LD flag <code>--no-as-needed</code> is needed for dynamic linking and <code>--whole-archive</code> is needed for static linking.</p>
  74. <p>This <a href="../examples/cpp/helloworld/Makefile#L37#L45">Makefile</a> demonstrates enabling c++ server reflection on Linux and MacOS.</p>
  75. <h1><a class="anchor" id="autotoc_md205"></a>
  76. Test services using Server Reflection</h1>
  77. <p>After enabling Server Reflection in a server application, you can use gRPC CLI to test its services.</p>
  78. <p>Instructions on how to use gRPC CLI can be found at <a class="el" href="command__line__tool_8md.html">command_line_tool.md</a>, or using <code>grpc_cli help</code> command.</p>
  79. <p>Here we use <code>examples/cpp/helloworld</code> as an example to show the use of gRPC Server Reflection and gRPC CLI. First, we need to build gRPC CLI and setup an example server with Server Reflection enabled.</p>
  80. <ul>
  81. <li><p class="startli">Setup an example server</p>
  82. <p class="startli">Server Reflection has already been enabled in the <a href="../examples/cpp/helloworld/Makefile">Makefile</a> of the helloworld example. We can simply make it and run the greeter_server.</p>
  83. </li>
  84. </ul>
  85. <div class="fragment"><div class="line">$ make -C examples/cpp/helloworld</div>
  86. <div class="line">$ examples/cpp/helloworld/greeter_server &amp;</div>
  87. </div><!-- fragment --><ul>
  88. <li>Build gRPC CLI</li>
  89. </ul>
  90. <div class="fragment"><div class="line">make grpc_cli</div>
  91. <div class="line">cd bins/opt</div>
  92. </div><!-- fragment --><p>gRPC CLI binary <code>grpc_cli</code> can be found at <code>bins/opt/</code> folder. This tool is still new and does not have a <code>make install</code> target yet.</p>
  93. <h2><a class="anchor" id="autotoc_md206"></a>
  94. List services</h2>
  95. <p><code>grpc_cli ls</code> command lists services and methods exposed at a given port</p>
  96. <ul>
  97. <li>List all the services exposed at a given port</li>
  98. </ul>
  99. <div class="fragment"><div class="line">$ grpc_cli ls localhost:50051</div>
  100. </div><!-- fragment --><p>output: </p><div class="fragment"><div class="line">helloworld.Greeter</div>
  101. <div class="line">grpc.reflection.v1alpha.ServerReflection</div>
  102. </div><!-- fragment --><ul>
  103. <li><p class="startli">List one service with details</p>
  104. <p class="startli"><code>grpc_cli ls</code> command inspects a service given its full name (in the format of &lt;package&gt;.&lt;service&gt;). It can print information with a long listing format when <code>-l</code> flag is set. This flag can be used to get more details about a service.</p>
  105. </li>
  106. </ul>
  107. <div class="fragment"><div class="line">$ grpc_cli ls localhost:50051 helloworld.Greeter -l</div>
  108. </div><!-- fragment --><p>output: </p><div class="fragment"><div class="line">filename: helloworld.proto</div>
  109. <div class="line">package: helloworld;</div>
  110. <div class="line">service Greeter {</div>
  111. <div class="line"> rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}</div>
  112. <div class="line">}</div>
  113. </div><!-- fragment --><h2><a class="anchor" id="autotoc_md207"></a>
  114. List methods</h2>
  115. <ul>
  116. <li><p class="startli">List one method with details</p>
  117. <p class="startli"><code>grpc_cli ls</code> command also inspects a method given its full name (in the format of &lt;package&gt;.&lt;service&gt;.&lt;method&gt;).</p>
  118. </li>
  119. </ul>
  120. <div class="fragment"><div class="line">$ grpc_cli ls localhost:50051 helloworld.Greeter.SayHello -l</div>
  121. </div><!-- fragment --><p>output: </p><div class="fragment"><div class="line">rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}</div>
  122. </div><!-- fragment --><h2><a class="anchor" id="autotoc_md208"></a>
  123. Inspect message types</h2>
  124. <p>We can use<code>grpc_cli type</code> command to inspect request/response types given the full name of the type (in the format of &lt;package&gt;.&lt;type&gt;).</p>
  125. <ul>
  126. <li>Get information about the request type</li>
  127. </ul>
  128. <div class="fragment"><div class="line">$ grpc_cli type localhost:50051 helloworld.HelloRequest</div>
  129. </div><!-- fragment --><p>output: </p><div class="fragment"><div class="line">message HelloRequest {</div>
  130. <div class="line"> optional string name = 1;</div>
  131. <div class="line">}</div>
  132. </div><!-- fragment --><h2><a class="anchor" id="autotoc_md209"></a>
  133. Call a remote method</h2>
  134. <p>We can send RPCs to a server and get responses using <code>grpc_cli call</code> command.</p>
  135. <ul>
  136. <li>Call a unary method</li>
  137. </ul>
  138. <div class="fragment"><div class="line">$ grpc_cli call localhost:50051 SayHello &quot;name: &#39;gRPC CLI&#39;&quot;</div>
  139. </div><!-- fragment --><p>output: </p><div class="fragment"><div class="line">message: &quot;Hello gRPC CLI&quot;</div>
  140. </div><!-- fragment --><h1><a class="anchor" id="autotoc_md210"></a>
  141. Use Server Reflection in a C++ client</h1>
  142. <p>Server Reflection can be used by clients to get information about gRPC services at runtime. We've provided a descriptor database called <a href="../test/cpp/util/proto_reflection_descriptor_database.h">grpc::ProtoReflectionDescriptorDatabase</a> which implements the <a href="https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor_database#DescriptorDatabase">google::protobuf::DescriptorDatabase</a> interface. It manages the communication between clients and reflection services and the storage of received information. Clients can use it as using a local descriptor database.</p>
  143. <ul>
  144. <li>To use Server Reflection with grpc::ProtoReflectionDescriptorDatabase, first initialize an instance with a grpc::Channel.</li>
  145. </ul>
  146. <div class="fragment"><div class="line">{c++}</div>
  147. <div class="line"> std::shared_ptr&lt;grpc::Channel&gt; channel =</div>
  148. <div class="line"> grpc::CreateChannel(server_address, server_cred);</div>
  149. <div class="line"> grpc::ProtoReflectionDescriptorDatabase reflection_db(channel);</div>
  150. </div><!-- fragment --><ul>
  151. <li>Then use this instance to feed a <a href="https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor#DescriptorPool">google::protobuf::DescriptorPool</a>.</li>
  152. </ul>
  153. <div class="fragment"><div class="line">{c++}</div>
  154. <div class="line"> google::protobuf::DescriptorPool desc_pool(&amp;reflection_db);</div>
  155. </div><!-- fragment --><ul>
  156. <li>Example usage of this descriptor pool<ul>
  157. <li><p class="startli">Get Service/method descriptors.</p>
  158. <p class="startli">```c++ const google::protobuf::ServiceDescriptor* service_desc = desc_pool-&gt;FindServiceByName("helloworld.Greeter"); const google::protobuf::MethodDescriptor* method_desc = desc_pool-&gt;FindMethodByName("helloworld.Greeter.SayHello"); ```</p>
  159. </li>
  160. <li><p class="startli">Get message type descriptors and create messages dynamically.</p>
  161. <p class="startli">```c++ const google::protobuf::Descriptor* request_desc = desc_pool-&gt;FindMessageTypeByName("helloworld.HelloRequest"); google::protobuf::DynamicMessageFactory dmf; google::protobuf::Message* request = dmf.GetPrototype(request_desc)-&gt;New(); ``` </p>
  162. </li>
  163. </ul>
  164. </li>
  165. </ul>
  166. </div></div><!-- contents -->
  167. </div><!-- PageDoc -->
  168. <!-- start footer part -->
  169. <hr class="footer"/><address class="footer"><small>
  170. Generated on Wed Mar 3 2021 19:20:09 for GRPC Objective-C by &#160;<a href="http://www.doxygen.org/index.html">
  171. <img class="footer" src="doxygen.png" alt="doxygen"/>
  172. </a> 1.8.17
  173. </small></address>
  174. </body>
  175. </html>