{"id":199270,"date":"2020-11-12T21:19:13","date_gmt":"2020-11-12T13:19:13","guid":{"rendered":"http:\/\/4563.org\/?p=199270"},"modified":"2020-11-23T09:20:28","modified_gmt":"2020-11-23T01:20:28","slug":"consul-grpc-%e5%81%a5%e5%ba%b7%e6%a3%80%e6%9f%a5%e5%a4%b1%e8%b4%a5%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=199270","title":{"rendered":"Consul GRPC \u5065\u5eb7\u68c0\u67e5\u5931\u8d25\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Consul GRPC \u5065\u5eb7\u68c0\u67e5\u5931\u8d25\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wunonglin <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>Consul \u6ce8\u518c\u7684\u4fe1\u606f<\/p>\n<pre><code>reg := api.AgentServiceRegistration{  ID:      strconv.FormatInt(time.Now().UnixNano(), 10),  Name:    name,  Address: listen,  Port:    port,  Check:   &amp;api.AgentServiceCheck{   GRPC:     fmt.Sprintf(\"%v:%v\/%v\", listen, port, name),   Interval: \"5s\",   Notes:    \"Consul check service health status.\",   DeregisterCriticalServiceAfter: \"10s\",  }, } <\/code><\/pre>\n<p>\u6211\u6309\u7167 https:\/\/www.consul.io\/docs\/discovery\/checks \u91cc\u7684 grpc \u68c0\u67e5\u91cc\uff0c\u5199\u4e86\u4e2a proto<\/p>\n<pre><code>syntax = \"proto3\";  package grpc_health_v1; option go_package = \".;grpc_health_v1\";  message HealthCheckRequest {     string service = 1; }  message HealthCheckResponse {     enum ServingStatus {         UNKNOWN = 0;         SERVING = 1;         NOT_SERVING = 2;         SERVICE_UNKNOWN = 3;  \/\/ Used only by the Watch method.     }     ServingStatus status = 1; }  service Health {     rpc Check(HealthCheckRequest) returns (HealthCheckResponse);      rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse); }  <\/code><\/pre>\n<p>\u6211\u7528 BloomRPC \u548c\u81ea\u5df1\u5199\u7684 client \u90fd\u80fd\u6b63\u5e38\u8bbf\u95ee\u8fd9\u4e2a Check \u65b9\u6cd5\uff0c\u4f46\u662f\u6ce8\u518c\u4e0a\u4e86 Consul \u5374\u663e\u793a\u5931\u8d25<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4194485\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : axex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             grpc \u7684 health check \u662f\u4e0d\u9700\u8981\u4f60\u5b9e\u73b0\u7684\uff0cgrpc \u7684\u5e93\u4e2d\u6709 google.golang.org\/grpc\/health\/grpc_health_v1\uff0c\u4f60\u76f4\u63a5 import \u7136\u540e RegisterHealthServer(grpcServer, health.NewServer())\u5c31\u884c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4194486\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wunonglin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @axex #1 <br \/>\u6211\u73b0\u5728\u6539\u4e86\u4e0b\u4ee3\u7801\u5f15\u5165\u4e86\uff0c\u4f46\u662f&amp;api.AgentServiceCheck \u91cc\u7684 GRPC \u662f\u8981\u600e\u4e48\u586b\uff0c<\/p>\n<p>\u662f ip:port \u8fd8\u662f ip:port\/service_name \u8fd8\u662f ip:port\/\/grpc_health_v1.Health \uff1f\u6211\u90fd\u8bd5\u4e86\u90fd\u4e0d\u884c                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4194487\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : axex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wunonglin ip:port \u5c31\u884c\u4e86\uff0c\u5982\u679c\u4e0d\u884c\u7684\u68c0\u67e5\u4e00\u4e0b ip\uff0c\u770b\u770b consul \u80fd\u5426\u6b63\u5e38\u8bbf\u95ee                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4194488\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wunonglin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @axex #3 <\/p>\n<p>grpc: addrConn.createTransport failed to connect to {127.0.0.1:8080 0 &lt;nil&gt;}. Err :connection error: desc = &#8220;transport: Error while dialing dial tcp 127.0.0.1:8080: connect: connection refused&#8221;. Reconnecting&#8230;<\/p>\n<p>\u63d0\u793a\u8fd9\u4e2a\uff0c\u4f46\u662f\u6211\u81ea\u5df1\u5199\u4e86\u4e00\u4e2a client \u6d4b\u8bd5\u7684\u8bdd\u662f\u53ef\u4ee5\u8fde\u63a5\u4e0a\u6765\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4194489\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : axex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wunonglin consul \u548c grpc server \u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u9762\uff1f\u4e0d\u662f\u7684\u8bdd consul \u662f\u65e0\u6cd5\u8bbf\u95ee\u7684\uff0c\u6700\u597d\u6539\u6210\u5185\u7f51 ip \u6216\u8005\u6539\u7528 ttl \u68c0\u67e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4194490\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wunonglin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @axex #5 <\/p>\n<p>OK \u4e86\uff0c\u6211 tm \u4eba\u50bb\u4e86\uff0cconsul \u662f\u5728 docker \u4e0a\u7684\uff0c\u7136\u540e\u6211\u586b\u7684 check \u5730\u5740\u662f 127\uff0c\uff0c\u6211\u5728\u672c\u5730\u90e8\u7f72\u4e86\u4e2a consul \u53ef\u4ee5\u4e86 <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/504J5BO2-75.png\" alt=\"Consul GRPC \u5065\u5eb7\u68c0\u67e5\u5931\u8d25\u95ee\u9898\"> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4194491\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wunonglin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @axex #5 consul \u4ed6\u8fd9\u6587\u6863\u6709\u5751\u5427\uff0c\u4e5f\u6ca1\u8bf4\u76f4\u63a5\u5f15\u7528\u5b98\u65b9\u7684 func\uff0c\u8fd8\u4ee5\u4e3a\u8981\u81ea\u5df1\u5199\uff0c\u641c\u4e86\u7f51\u4e0a\u7684\u6559\u7a0b\uff0c\u538b\u6839\u6ca1\u4eba\u63d0\u8fc7\uff0c\u6211\u786c\u662f\u7814\u7a76\u4e86 2 \u5929\uff0c\u6d6a\u8d39\u4e00\u4e2a\u7f8e\u597d\u7684\u5468\u672b                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Consul GRPC \u5065\u5eb7\u68c0\u67e5\u5931&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/199270"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=199270"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/199270\/revisions"}],"predecessor-version":[{"id":199272,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/199270\/revisions\/199272"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=199270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=199270"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=199270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}