{"id":433,"date":"2024-02-29T22:17:45","date_gmt":"2024-02-29T14:17:45","guid":{"rendered":"https:\/\/www.gonait.com\/?p=433"},"modified":"2024-03-16T19:03:25","modified_gmt":"2024-03-16T11:03:25","slug":"grpc%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.gonait.com\/?p=433","title":{"rendered":"gRPC\u6846\u67b6\u5b66\u4e60\u8bb0\u5f55"},"content":{"rendered":"<h1>gRPC\u9996\u6b21\u4f7f\u7528\u95ee\u9898<\/h1>\n<ul>\n<li>\u62a5\u9519\u4fe1\u606f1\uff1a<img decoding=\"async\" src=\"https:\/\/note.youdao.com\/yws\/res\/2532\/WEBRESOURCE9a4ca0efd0bd5d8c2d86ac27fa80c17c\" alt=\"\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_17085708151420.png\" \/>\n<ul>\n<li>\u9020\u6210\u539f\u56e0\uff1a\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u91c7\u7528\u6a21\u5f0f\u4e0d\u540c\uff0c\u670d\u52a1\u7aef\u91c7\u7528PLAINTEXT\u6a21\u5f0f\uff0c\u5ba2\u6237\u7aef\u9ed8\u8ba4\u91c7\u7528TSL\u6a21\u5f0f\u3002<\/li>\n<li>\u89e3\u51b3\u65b9\u6848\uff1a<a href=\"https:\/\/www.cnblogs.com\/fengyege\/p\/16936305.html\">\u53c2\u8003<\/a>\uff0c\u6dfb\u52a0\u914d\u7f6e\u4fe1\u606f\uff1a<code>grpc.client.\u6ce8\u518c\u670d\u52a1\u540d.negotiationType=PLAINTEXT<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u62a5\u9519\u4fe1\u606f2\uff1a<code>Runnable threw exception java.lang.NoSuchMethodError: com.google.protobuf.GeneratedMessageV3.isStringEmpty(Ljava\/lang\/Object;)Z<\/code><img decoding=\"async\" src=\"https:\/\/note.youdao.com\/yws\/res\/2541\/WEBRESOURCE6ccf59f9d6d3bb6da27e129a6d5acff5\" alt=\"\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_17085797339551.png\" \/>\n<ul>\n<li>\u9020\u6210\u539f\u56e0\uff1a<code>protobuf-java<\/code>\u4f9d\u8d56\u7248\u672c\u51b2\u7a81\uff0c\u7ecf\u6392\u67e5\u9879\u76ee\u5f15\u7528\u4e86MySQL\u8fde\u63a5\u76f8\u5173\u4f9d\u8d56<code>mysql-connector-java<\/code>\u4e14\u8f83\u5148\u58f0\u660e\uff0c\u5176\u5185\u5305\u542b\u4f4e\u7248\u672c\u7684<code>protobuf-java<\/code>(Maven\u4f9d\u8d56\u91c7\u7528\u4e86\u6b64\u4f4e\u7248\u672c)\uff0c\u5bfc\u81f4\u4e0e\u7ed9\u5b9a\u7684gRPC SDK\u6240\u9700\u8981\u7684\u7248\u672c\u51b2\u7a81\u800c\u8fd0\u884c\u65f6\u62a5\u9519\u3002<\/li>\n<li>\u89e3\u51b3\u65b9\u6848\uff1a\u7edf\u4e00\u4f9d\u8d56\u7248\u672c\uff0c\u6392\u9664mysql\u4f9d\u8d56\u4e2d\u7684<code>protobuf-java<\/code>\u6216\u8f83\u5148\u58f0\u660e\u9700\u8981\u7248\u672c\u7684<code>protobuf-java<\/code>\u4f9d\u8d56\u3002<br \/>\n<h1>gRPC \u9ad8\u6027\u80fdRPC\u6846\u67b6<\/h1>\n<\/li>\n<\/ul>\n<\/li>\n<li>RPC \u7684\u5168\u79f0\u662f Remote Procedure Call Protocol\uff0c\u4e2d\u6587\u540d\u662f\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u534f\u8bae\u3002<br \/>\n<h2>gRPC\u7b80\u4ecb<\/h2>\n<blockquote>\n<ul>\n<li>google\u5f00\u6e90\u7684\u9ad8\u6027\u80fdRPC\u6846\u67b6<\/li>\n<li>\u652f\u6301\u5f02\u6784\u7f16\u7a0b\u8bed\u8a00\u7684\u8c03\u7528<\/li>\n<li>gRPC\u6838\u5fc3\u8bbe\u8ba1\u601d\u8def\uff1a\n<ul>\n<li>\u7f51\u7edc\u901a\u4fe1\uff1a\u81ea\u5df1\u5c01\u88c5\u4e86\u7f51\u7edc\u901a\u4fe1\u7684\u90e8\u5206\uff0c\u5305\u62ec\u591a\u79cd\u8bed\u8a00\u7684\u7f51\u7edc\u901a\u4fe1\u5c01\u88c5\uff0c\u5982C\u3001Go\u3001Java<\/li>\n<li>\u534f\u8bae\uff1ahttp2\uff0c\u4f20\u8f93\u4e8c\u8fdb\u5236\u6570\u636e\u5185\u5bb9\uff0c\u652f\u6301\u53cc\u5de5\u901a\u4fe1<\/li>\n<li>\u5e8f\u5217\u5316\uff1a\u57fa\u4e8e\u6587\u672c-json\uff0c\u57fa\u4e8e\u4e8c\u8fdb\u5236-Java\u539f\u751f\u5e8f\u5217\u5316\uff0cgRPC\u7684\u5e8f\u5217\u5316\u91c7\u7528\u7684\u662f\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u65b9\u5f0f-protobuf<\/li>\n<li>\u4ee3\u7406\u7684\u521b\u5efa\uff1a\u8ba9\u8c03\u7528\u8005\u50cf\u8c03\u7528\u672c\u5730\u65b9\u6cd5\u4e00\u6837\u8c03\u7528\u8fdc\u7a0b\u7684\u65b9\u6cd5\uff0cstub\u5b58\u6839<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/blockquote>\n<\/li>\n<\/ul>\n<h2>gRPC\u4f18\u70b9<\/h2>\n<ul>\n<li>\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/li>\n<li>\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u5bf9\u4e3b\u6d41\u8bed\u8a00\u539f\u751f\u652f\u6301-C\u3001Go\u3001Java<\/li>\n<li>\u652f\u6301\u591a\u5e73\u53f0\u8fd0\u884c <\/li>\n<li>\u9ad8\u6548\u7684\u5e8f\u5217\u5316\u65b9\u5f0fprotobuf<\/li>\n<li>\u4f7f\u7528http2<\/li>\n<\/ul>\n<h2>HTTP \u534f\u8bae<\/h2>\n<ul>\n<li>HTTP1.0\u534f\u8bae\uff1a\n<ul>\n<li>\u8bf7\u6c42\u54cd\u5e94\u6a21\u5f0f\uff0c\u77ed\u8fde\u63a5\u534f\u8bae\uff08\u65e0\u72b6\u6001\u534f\u8bae\uff09\uff0c\u53ef\u4ee5\u7528WEB\u5f00\u53d1\u7684HttpSession\u89e3\u51b3\u65e0\u72b6\u6001\u95ee\u9898\u3002<\/li>\n<li>http\u534f\u8bae\u662f\u57fa\u4e8etcp\u534f\u8bae\u4e4b\u4e0a\u7684\uff0ctcp\u662f\u957f\u8fde\u63a5\uff0c\u800chttp\u662f\u77ed\u8fde\u63a5\u7684\u539f\u56e0\u662f\u670d\u52a1\u5668\u8d44\u6e90\u53ca\u786c\u4ef6\u73af\u5883\u6709\u9650\u3002 <\/li>\n<li>\u4f20\u8f93\u6570\u636e\u7ed3\u6784\u4e3a\u6587\u672c\u7ed3\u6784\uff0c\u5355\u5de5\u901a\u4fe1\uff0c\u65e0\u6cd5\u5b9e\u73b0\u63a8\u9001\uff0c\u53ef\u91c7\u7528\u5ba2\u6237\u7aef\u8f6e\u8be2\u7684\u65b9\u5f0f\u53d8\u76f8\u5b9e\u73b0\u63a8\u9001\u3002<\/li>\n<\/ul>\n<\/li>\n<li>HTTP1.1\u534f\u8bae\uff1a\n<ul>\n<li>\u8bf7\u6c42\u54cd\u5e94\u6a21\u5f0f\uff0c\u53ef\u5b9e\u73b0\u6709\u9650\u957f\u8fde\u63a5\uff0c\u901a\u8fc7\u5934\u4fe1\u606f\u4e2d\u7684keepalive\u6765\u4fdd\u6301\u4e00\u6bb5\u65f6\u95f4\u7684\u957f\u8fde\u63a5<\/li>\n<li>\u884d\u751f\u4e00\u79cd\u5347\u7ea7\u7684\u6280\u672f\u65b9\u5f0fWebSocket\u534f\u8bae\uff0c\u53ef\u4ee5\u53cc\u5de5\uff0c\u5b9e\u73b0\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u63a8\u9001<\/li>\n<\/ul>\n<\/li>\n<li>HTTP2.0\u534f\u8bae\uff1a\n<ul>\n<li>\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u534f\u8bae\uff0c\u6548\u7387\u9ad8\u4e8eHTTP1.x\u534f\u8bae\uff0c\u53ef\u8bfb\u6027\u8f83\u5dee<\/li>\n<li>\u53ef\u4ee5\u5b9e\u73b0\u53cc\u5de5\u901a\u4fe1<\/li>\n<li>\u4e00\u4e2a\u8bf7\u6c42\u53ef\u4ee5\u8bf7\u6c42\u591a\u6b21\u6216\u591a\u4e2a\u6570\u636e\uff0c\u591a\u8def\u590d\u7528<\/li>\n<\/ul>\n<\/li>\n<li>HTTP2.0\u534f\u8bae\u7684\u4e09\u4e2a\u6982\u5ff5\uff1a\n<ul>\n<li>\u6570\u636e\u6d41 stream<\/li>\n<li>\u6d88\u606f   message<\/li>\n<li>\u5e27     frame<\/li>\n<li>\u5176\u4ed6\u76f8\u5173\u6982\u5ff5\uff1a<\/li>\n<li>\u6570\u636e\u6d41\u7684\u4f18\u5148\u7ea7\uff0c\u901a\u8fc7\u8bbe\u7f6estream\u7684\u6743\u91cd\u6765\u9650\u5236\u4e0d\u540c\u6d41\u7684\u4f20\u8f93\u987a\u5e8f<\/li>\n<li>\u6d41\u63a7\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u592a\u5feb\uff0c\u670d\u52a1\u7aef\u5904\u7406\u4e0d\u8fc7\u6765\uff0c\u901a\u77e5\u5ba2\u6237\u7aef\u6682\u505c\u53d1\u9001\u6570\u636e\u6d41<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Protocol Buffers(ProtoBuf)<\/h2>\n<ul>\n<li>\u4e00\u79cd\u4e0e\u7f16\u7a0b\u8bed\u8a00\u65e0\u5173(IDL)\uff0c\u4e0e\u5177\u4f53\u5e73\u53f0\u65e0\u5173(OS)\uff0c\u5b9a\u4e49\u7684\u4e00\u79cd\u4e2d\u95f4\u8bed\u8a00\uff0c\u53ef\u4ee5\u65b9\u4fbf\u518dclient\u548cserver\u95f4\u8fdb\u884cRPC\u6570\u636e\u4f20\u8f93<\/li>\n<li>protobuf\u8bed\u8a00\u4e24\u4e2a\u7248\u672c\uff1aproto2\uff0cproto3(\u4e3b\u6d41\u5e94\u7528)<\/li>\n<li>\u9700\u8981\u5b89\u88c5protobuf\u7684\u7f16\u8bd1\u5668\u6765\u5c06protobuf\u7684IDL\u8bed\u8a00\u8f6c\u5316\u4e3a\u5177\u4f53\u7684\u67d0\u79cd\u5f00\u53d1\u8bed\u8a00<\/li>\n<li>\n<\/li>\n<\/ul>\n<h2>ProtoBuf\u8bed\u6cd5\u8be6\u89e3<\/h2>\n<ul>\n<li>\u6587\u4ef6\u683c\u5f0f\uff1a*.proto\uff0cHello.proto<\/li>\n<li>\u7248\u672c\u8bbe\u5b9a\uff1a<code>syntax=&quot;proto3&quot;;<\/code><\/li>\n<li>\u6ce8\u91ca\uff1a\u5355\u884c\u6ce8\u91ca\uff1a<code>\/\/ \u6ce8\u91ca\u5185\u5bb9<\/code>\uff0c\u591a\u884c\u6ce8\u91ca\uff1a<code>\/* \u6ce8\u91ca\u5185\u5bb9 *\/<\/code><\/li>\n<li>\u4e0eJava\u8bed\u8a00\u76f8\u5173\u7684\u8bed\u6cd5\uff1a\n<pre><code class=\"language-java\"># \u540e\u7eedprotobuf\u751f\u6210\u7684Java\u4ee3\u7801\u662f\u4e00\u4e2a\u6e90\u6587\u4ef6\u8fd8\u662f\u591a\u4e2a\u6e90\u6587\u4ef6\noption java_multiple_files=true\n# \u6307\u5b9aJava\u7c7b\u7684\u5305\u7ed3\u6784\noption java_package=\"\"\n# \u6307\u5b9a\u751f\u6210\u7684\u5916\u90e8\u7c7b\u7684\u540d\u5b57(\u5185\u90e8\u7c7b\u624d\u662f\u540e\u7eed\u5f00\u53d1\u4e2d\u771f\u6b63\u4f7f\u7528\u7684)\noption java_outer_classname=\"\"<\/code><\/pre>\n<\/li>\n<li>\u903b\u8f91\u5305\uff1a<code>package xxx<\/code> protobuf\u5bf9\u4e8e\u6587\u4ef6\u7684\u7ba1\u7406<\/li>\n<li>\u5bfc\u5165\uff1a<code>import &quot;xxx\/xxx.proto&quot;<\/code>\uff0c\u5728\u5f53\u524dproto\u4e2d\u5bfc\u5165\u5176\u4ed6proto<\/li>\n<li>\u6570\u636e\u7c7b\u578b<\/li>\n<li>\u6d88\u606fMessage\n<pre><code class=\"language-protobuf\">message UserRequest{\n  string username = 1;\n  string password = 2;\n  int32 age = 3;\n}<\/code><\/pre>\n<ul>\n<li>\u7f16\u53f7\u4ece1\u52302^29-1\u4e4b\u95f4\uff0c\u6ce8\u610f\uff1a19000-19999\u8fd9\u4e2a\u533a\u95f4\u5185\u7684\u7f16\u53f7\u4e0d\u80fd\u7528\uff0c\u88abprotobuf\u4fdd\u7559\u4e86<\/li>\n<li>singular\uff1a\u5bf9\u5e940\u62161\u4e2a\u503c\uff1anull\u6216&quot;\u67d0\u4e2a\u503c&quot;<\/li>\n<li>repeated\uff1a\u4fee\u9970\u96c6\u5408\u5b57\u6bb5\u7684\u5173\u952e\u5b57<\/li>\n<li>\u6d88\u606f\u53ef\u4ee5\u5d4c\u5957\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6d88\u606f<\/li>\n<\/ul>\n<\/li>\n<li>\u670d\u52a1Service<\/li>\n<li>\n<\/li>\n<\/ul>\n<h2>gRPC\u56db\u79cd\u901a\u4fe1\u65b9\u5f0f<\/h2>\n<ul>\n<li>\u7b80\u5355rpc \u4e00\u5143rpc (Unary RPC)\uff1a\u4e00\u8bf7\u6c42\u4e00\u54cd\u5e94<\/li>\n<li>\u670d\u52a1\u7aef\u6d41\u5f0frpc (Server Streaming RPC)<\/li>\n<li>\u5ba2\u6237\u7aef\u6d41\u5f0frpc (Client Streaming RPC)<\/li>\n<li>\u53cc\u5411\u6d41\u5f0frpc   (Bi-directional Streaming RPC)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>gRPC\u9996\u6b21\u4f7f\u7528\u95ee\u9898 \u62a5\u9519\u4fe1\u606f1\uff1a \u9020\u6210 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":442,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"acf":[],"views":771,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5.png","uagb_featured_image_src":{"full":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5.png",750,500,false],"thumbnail":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5-150x150.png",150,150,true],"medium":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5-300x200.png",300,200,true],"medium_large":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5.png",750,500,false],"large":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5.png",750,500,false],"1536x1536":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5.png",750,500,false],"2048x2048":["https:\/\/www.gonait.com\/wp-content\/uploads\/2024\/02\/aaf561ff8baa328cc51bc622436f24d5.png",750,500,false]},"uagb_author_info":{"display_name":"\u53e4\u4e43\u7279","author_link":"https:\/\/www.gonait.com\/?author=2"},"uagb_comment_info":0,"uagb_excerpt":"gRPC\u9996\u6b21\u4f7f\u7528\u95ee\u9898 \u62a5\u9519\u4fe1\u606f1\uff1a \u9020\u6210 [&hellip;]","_links":{"self":[{"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/posts\/433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gonait.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=433"}],"version-history":[{"count":2,"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/posts\/433\/revisions\/575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gonait.com\/index.php?rest_route=\/wp\/v2\/media\/442"}],"wp:attachment":[{"href":"https:\/\/www.gonait.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gonait.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gonait.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}