{"id":10006093,"date":"2022-08-03T09:14:51","date_gmt":"2022-08-03T01:14:51","guid":{"rendered":"https:\/\/ityet.com\/?p=10006093"},"modified":"2022-08-03T09:14:51","modified_gmt":"2022-08-03T01:14:51","slug":"mosn-%e5%8f%8d%e5%90%91%e9%80%9a%e9%81%93%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006093","title":{"rendered":"MOSN \u53cd\u5411\u901a\u9053\u8be6\u89e3"},"content":{"rendered":"<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260230\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6587\uff5c\u90d1\u6cfd\u8d85\uff08GitHub ID\uff1aCodingSinger )<\/p>\n<p>\u5b57\u8282\u8df3\u52a8\u9ad8\u7ea7\u5de5\u7a0b\u5e08<\/p>\n<p><em>\u70ed\u8877\u4e8e\u5fae\u670d\u52a1\u548c ServiceMesh \u5f00\u6e90\u793e\u533a<\/em><\/p>\n<p><strong>\u672c\u6587 6802 \u5b57\uff0c\u9605\u8bfb\u00a015 \u5206\u949f<\/strong><\/p>\n<h2>Part.1&#8211;\u8d21\u732e\u8005\u524d\u8a00<\/h2>\n<p>\u8bf4\u8d77\u6765\u975e\u5e38\u7684\u6293\u9a6c\uff0c\u5f53\u65f6\u548c MOSN \u7684\u76f8\u9047\u662f\u5728\u7ed9\u4e8e\u96e8\u8d1f\u8d23\u7684\u5f00\u6e90\u9879\u76ee Dubbo-go \u8d21\u732e\u4ee3\u7801\u90a3\u9635\u3002\u5728\u81ea\u5df1\u987a\u5229\u5f53\u4e0a\u4e86 Dubbo \u5f00\u6e90\u793e\u533a\u7684 Committer \u4e4b\u540e\uff0c\u5fc3\u60f3\u7740\u80fd\u66f4\u6df1\u5165\u7684\u5b66\u4e60 Golang \u8bed\u8a00\uff0c\u673a\u7f18\u5de7\u5408\u4e4b\u4e0b\u78b0\u5230\u4e86 MOSN \u7684\u8001\u5927\u54e5\u70c8\u5143 <em>\uff08\u4e5f\u662f\u5143\u603b\u9886\u6211\u8fdb\u4e86 MOSN \u793e\u533a\u7684\u5927\u95e8\uff09<\/em> \u3002<\/p>\n<p>\u4f5c\u4e3a\u4e00\u6b3e\u76ee\u6807\u5bf9\u9f50 Envoy \u7684\u9ad8\u6027\u80fd\u53ef\u6269\u5c55\u5b89\u5168\u7f51\u7edc\u4ee3\u7406\uff0cMOSN \u652f\u6301\u7684\u751f\u6001\u80fd\u529b\u66f4\u8d34\u8fd1\u56fd\u5185\u4e92\u8054\u7f51\u516c\u53f8\u7684\u6280\u672f\u6808\uff0c\u5e76\u4e14\u5bf9\u65b0\u529f\u80fd\u7684\u54cd\u5e94\u4e5f\u5f88\u8fc5\u901f\u3002\u5176\u6b21 MOSN \u6709\u7740\u5f88\u591a\u503c\u5f97\u501f\u9274\u7684\u5de7\u5999\u8bbe\u8ba1\u548c\u8fdb\u9636\u7684\u4f7f\u7528\u6280\u5de7\uff0c\u80fd\u5145\u5206\u6ee1\u8db3\u81ea\u5df1\u5728\u5de5\u4f5c\u4e4b\u5916\u6df1\u5165\u5b66\u4e60 Golang \u8bed\u8a00\u7684\u8bc9\u6c42\u3002<\/p>\n<p>\u76ee\u524d\uff0c\u6211\u5728\u793e\u533a\u91cc\u9646\u7eed\u53c2\u4e0e\u4e86 EDF Scheduler\u3001LAR\u3001WRR \u8d1f\u8f7d\u5747\u8861\u3001DSL \u8def\u7531\u80fd\u529b\u3001UDS Listener\u3001Plugin \u6a21\u5f0f\u7684 Filter \u6269\u5c55\u4ee5\u53ca\u53cd\u5411\u901a\u9053\u7b49\u4e00\u4e9b\u6bd4\u8f83\u5927\u7684 feature \u80fd\u529b\u5efa\u8bbe\u3002\u518d\u6b21\u611f\u8c22\u96e8\u54e5\u3001\u5143\u603b\u3001\u9e4f\u603b\u3001\u6bc5\u677e\u7b49\u793e\u533a\u5185\u4e00\u4f17\u5927\u4f6c\u4eec\u5e2e\u6211\u8003\u7a76\u65b9\u6848\u5e76\u4e14\u5e2e\u6211 Review \u4ee3\u7801\u3002<\/p>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e4b\u524d\u65b0\u5408\u5165 master \u5206\u652f\u7684\u300c<strong>\u53cd\u5411\u901a\u9053<\/strong>\u300d\u7684\u4f7f\u7528\u573a\u666f\u548c\u8bbe\u8ba1\u539f\u7406\uff0c\u6b22\u8fce\u5927\u5bb6\u7559\u8a00\u63a2\u8ba8\u3002<\/p>\n<h3>MOSN \u9879\u76ee\u6982\u8ff0<\/h3>\n<p>MOSN\uff08Modular Open Smart Network\uff09\u662f\u4e00\u6b3e\u4e3b\u8981\u4f7f\u7528 Go \u8bed\u8a00\u5f00\u53d1\u7684\u4e91\u539f\u751f\u7f51\u7edc\u4ee3\u7406\u5e73\u53f0\uff0c\u7531\u8682\u8681\u96c6\u56e2\u5f00\u6e90\u5e76\u7ecf\u8fc7\u53cc 11 \u5927\u4fc3\u51e0\u5341\u4e07\u5bb9\u5668\u7684\u751f\u4ea7\u7ea7\u9a8c\u8bc1\uff0c\u5177\u5907<strong>\u9ad8\u6027\u80fd<\/strong>\u3001<strong>\u6613\u6269\u5c55<\/strong>\u7684\u7279\u70b9\u3002MOSN \u53ef\u4ee5\u548c Istio \u96c6\u6210\u6784\u5efa Service Mesh\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u72ec\u7acb\u7684\u56db\u3001\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u3001API Gateway\u3001\u4e91\u539f\u751f Ingress \u7b49\u4f7f\u7528\u3002<\/p>\n<h2>Part.2&#8211;MOSN\u00a0\u7684\u53cd\u5411\u901a\u9053\u5b9e\u73b0<\/h2>\n<p>\u5728\u4e91\u8fb9\u534f\u540c\u7684\u7f51\u7edc\u573a\u666f\uff0c\u901a\u5e38\u90fd\u662f\u5355\u5411\u7f51\u7edc\uff0c\u4e91\u4fa7\u8282\u70b9\u65e0\u6cd5\u4e3b\u52a8\u53d1\u8d77\u8fde\u63a5\u4e0e\u8fb9\u7f18\u8282\u70b9\u901a\u8baf\u3002\u8fd9\u79cd\u9650\u5236\u867d\u7136\u5728\u6781\u5927\u7a0b\u5ea6\u4e0a\u4fdd\u8bc1\u4e86\u8fb9\u7f18\u8282\u70b9\u7684\u5b89\u5168\uff0c\u4f46\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u5373\u53ea\u5141\u8bb8\u8fb9\u7f18\u8282\u70b9\u4e3b\u52a8\u53d1\u8d77\u8bbf\u95ee\u4e91\u7aef\u8282\u70b9\u3002<\/p>\n<p>\u4e91\u8fb9\u96a7\u9053\u65e8\u5728\u89e3\u51b3\u4e91\u7aef\u65e0\u6cd5\u4e3b\u52a8\u8bbf\u95ee\u8fb9\u7f18\u8282\u70b9\u7684\u95ee\u9898\uff0c\u5176\u672c\u8d28\u662f\u4e00\u4e2a\u53cd\u5411\u901a\u9053 <em>\uff08\u540e\u6587\u7edf\u79f0\u4e3a\u53cd\u5411\u901a\u9053\uff09<\/em> \u3002\u901a\u8fc7\u5728\u8fb9\u7f18\u4fa7\u4e3b\u52a8\u53d1\u8d77\u5efa\u8fde\u7684\u65b9\u5f0f\u4e0e\u4e91\u7aef\u8282\u70b9\u4e4b\u95f4\u6784\u5efa\u4e00\u6761\u4e13\u7528\u7684\u5168\u53cc\u5de5\u8fde\u63a5\uff0c\u7528\u6765\u4f20\u8f93\u4e91\u7aef\u8282\u70b9\u7684\u8bf7\u6c42\u6570\u636e\u548c\u56de\u4f20\u6700\u7ec8\u7684\u54cd\u5e94\u7ed3\u679c\u3002<\/p>\n<p>\u76ee\u524d\u4f8b\u5982 SuperEdge\u3001Yurttunnel \u7b49\u4e1a\u754c\u77e5\u540d\u4e91\u8fb9\u534f\u540c\u5f00\u6e90\u6846\u67b6\uff0c\u5bf9\u4e8e\u4e91\u8fb9\u901a\u4fe1\u7684\u5b9e\u73b0\u65b9\u6848\u90fd\u662f\u57fa\u4e8e\u53cd\u5411\u901a\u9053\u3002<\/p>\n<p>\u672c\u6587\u5c06\u7740\u91cd\u4ecb\u7ecd MOSN \u4e4b\u4e0a\u7684\u53cd\u5411\u901a\u9053\u8fd0\u4f5c\u6d41\u7a0b\u548c\u539f\u7406\u3002\u603b\u4f53\u67b6\u6784\u5982\u4e0b\u6240\u793a <em>(\u56fe\u4e2d\u7bad\u5934\u8868\u793a TCP \u5efa\u8fde\u53cd\u5411)<\/em> \uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260231\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6574\u4e2a\u8fd0\u4f5c\u6d41\u7a0b\u53ef\u4ee5\u7b80\u5355\u6982\u62ec\u4e3a\uff1a<\/p>\n<p><strong>1.<\/strong> \u8fb9\u7f18\u4fa7\u7684 MOSN \u5b9e\u4f8b <em>\uff08\u540e\u6587\u7edf\u79f0\u4e3a Tunnel Agent\uff09<\/em> \u5728\u542f\u52a8\u65f6 Tunnel Agent \u76f8\u5173\u670d\u52a1\u534f\u7a0b\u3002<\/p>\n<p><strong>2.<\/strong> \u901a\u8fc7\u6307\u5b9a\u7684\u9759\u6001\u914d\u7f6e\u6216\u8005\u52a8\u6001\u670d\u52a1\u53d1\u73b0\u65b9\u5f0f\u62ff\u5230\u9700\u8981\u53cd\u5411\u5efa\u8fde\u7684\u516c\u6709\u4e91\u4fa7\u7684 MOSN Server \u5730\u5740\u5217\u8868 <em>\uff08\u540e\u6587\u7edf\u79f0 Tunnel Server \uff09<\/em> \uff0c\u5e76\u4e14\u5efa\u7acb\u53cd\u5411\u8fde\u63a5\u3002<\/p>\n<p><strong>3.<\/strong> \u4e91\u4fa7\u7684 Frontend \u4e0e Tunnel Server \u4fa7\u7684\u8f6c\u53d1\u7aef\u53e3\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\uff0c\u8fd9\u90e8\u5206\u6570\u636e\u4f1a\u88ab\u6258\u7ba1\u5230\u4e4b\u524d\u5efa\u7acb\u7684\u53cd\u5411\u8fde\u63a5\u8fdb\u884c\u53d1\u9001\u3002<\/p>\n<p><strong>4.<\/strong> \u8fb9\u7f18\u8282\u70b9\u63a5\u53d7\u5230\u8bf7\u6c42\u4e4b\u540e\uff0c\u518d\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u5b9e\u9645\u7684\u540e\u7aef\u76ee\u6807\u8282\u70b9\uff0c\u56de\u5305\u8fc7\u7a0b\u5219\u8fdc\u8def\u8fd4\u56de\u3002<\/p>\n<h2>Part.3&#8211;\u53cd\u5411\u901a\u9053\u542f\u52a8\u8fc7\u7a0b<\/h2>\n<p>MOSN Agent \u901a\u8fc7 ExtendConfig \u7279\u6027\uff0c\u5728 MOSN \u542f\u52a8\u65f6\u52a0\u8f7d\u548c\u5b8c\u6210\u521d\u59cb\u5316 Tunnel Agent \u7684\u5de5\u4f5c\u3002<\/p>\n<p>ExtendConfig \u4e2d\u5b9a\u4e49 AgentBootstrapConfig \u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code>type AgentBootstrapConfig struct {\n    Enable bool `json:&quot;enable&quot;`\n    \/\/ The number of connections established between the agent and each server\n    ConnectionNum int `json:&quot;connection_num&quot;`\n    \/\/ The cluster of remote server\n    Cluster string `json:&quot;cluster&quot;`\n    \/\/ After the connection is established, the data transmission is processed by this listener\n    HostingListener string `json:&quot;hosting_listener&quot;`\n    \/\/ Static remote server list\n    StaticServerList []string `json:&quot;server_list&quot;`\n\n    \/\/ DynamicServerListConfig is used to specify dynamic server configuration\n    DynamicServerListConfig struct {\n        DynamicServerLister string `json:&quot;dynamic_server_lister&quot;`\n    }\n\n    \/\/ ConnectRetryTimes\n    ConnectRetryTimes int `json:&quot;connect_retry_times&quot;`\n    \/\/ ReconnectBaseDuration\n    ReconnectBaseDurationMs int `json:&quot;reconnect_base_duration_ms&quot;`\n\n    \/\/ ConnectTimeoutDurationMs specifies the timeout for establishing a connection and initializing the agent\n    ConnectTimeoutDurationMs int    `json:&quot;connect_timeout_duration_ms&quot;`\n    CredentialPolicy         string `json:&quot;credential_policy&quot;`\n    \/\/ GracefulCloseMaxWaitDurationMs specifies the maximum waiting time to close conn gracefully\n    GracefulCloseMaxWaitDurationMs int `json:&quot;graceful_close_max_wait_duration_ms&quot;`\n\n    TLSContext *v2.TLSConfig `json:&quot;tls_context&quot;`\n}<\/code><\/pre>\n<p><strong>&#8211;<\/strong> <strong>ConnectionNum<\/strong>\uff1aTunnel Agent \u548c\u6bcf\u4e2a Tunnel Server \u5efa\u7acb\u7684\u7269\u7406\u8fde\u63a5\u6570\u91cf\u3002<\/p>\n<p><strong>&#8211;<\/strong> <strong>HostingListener<\/strong>\uff1a\u6307\u5b9a Agent \u5efa\u7acb\u8fde\u63a5\u4e4b\u540e\u6258\u7ba1\u7684 MOSN Listener\uff0c\u5373 Tunnel Server \u53d1\u6765\u7684\u8bf7\u6c42\u4f1a\u7531\u8be5 Listener \u6258\u7ba1\u5904\u7406\u3002<\/p>\n<p><strong>&#8211;<\/strong> <strong>DynamicServerListConfig<\/strong>\uff1a\u52a8\u6001 Tunnel Server \u7684\u670d\u52a1\u53d1\u73b0\u76f8\u5173\u914d\u7f6e\uff0c\u53ef\u901a\u8fc7\u81ea\u5b9a\u4e49\u7684\u670d\u52a1\u53d1\u73b0\u7ec4\u4ef6\u63d0\u4f9b\u52a8\u6001\u7684\u5730\u5740\u670d\u52a1\u3002<\/p>\n<p><strong>&#8211;<\/strong> <strong>CredentialPolicy<\/strong>\uff1a\u81ea\u5b9a\u4e49\u7684\u8fde\u63a5\u7ea7\u522b\u7684\u9274\u6743\u7b56\u7565\u914d\u7f6e\u3002<\/p>\n<p><strong>&#8211;<\/strong> <strong>TLSContext<\/strong>\uff1aMOSN TLS \u914d\u7f6e\uff0c\u63d0\u4f9b TCP \u4e4b\u4e0a\u901a\u4fe1\u7684\u4fdd\u5bc6\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u9488\u5bf9\u6bcf\u4e2a\u8fdc\u7aef\u7684 Tunnel Server \u5b9e\u4f8b\uff0cAgent \u5bf9\u5e94\u4e00\u4e2a AgentPeer \u5bf9\u8c61\uff0c\u542f\u52a8\u65f6\u9664\u4e86\u4e3b\u52a8\u5efa\u7acb ConnectionNum \u4e2a\u53cd\u5411\u901a\u4fe1\u8fde\u63a5\uff0c\u8fd8\u4f1a\u989d\u5916\u5efa\u7acb\u4e00\u6761\u65c1\u8def\u8fde\u63a5\uff0c\u8fd9\u6761\u65c1\u8def\u8fde\u63a5\u4e3b\u8981\u662f\u7528\u6765\u53d1\u9001\u4e00\u4e9b\u7ba1\u63a7\u53c2\u6570\uff0c\u4f8b\u5982\u5e73\u6ed1\u5173\u95ed\u8fde\u63a5\u3001\u8c03\u6574\u8fde\u63a5\u6bd4\u91cd\u3002<\/p>\n<pre><code>func (a *AgentPeer) Start() {\n    connList := make([]*AgentClientConnection, 0, a.conf.ConnectionNumPerAddress)\n    for i := 0; i &lt; a.conf.ConnectionNumPerAddress; i++ {\n      \/\/ \u521d\u59cb\u5316\u548c\u5efa\u7acb\u53cd\u5411\u8fde\u63a5\n        conn := NewAgentCoreConnection(*a.conf, a.listener)\n        err := conn.initConnection()\n        if err == nil {\n            connList = append(connList, conn)\n        }\n    }\n    a.connections = connList\n    \/\/ \u5efa\u7acb\u4e00\u4e2a\u65c1\u8def\u63a7\u5236\u8fde\u63a5\n    a.initAside()\n}<\/code><\/pre>\n<p>initConnection\u00a0\u65b9\u6cd5\u8fdb\u884c\u5177\u4f53\u7684\u521d\u59cb\u5316\u5b8c\u6574\u7684\u53cd\u5411\u8fde\u63a5\uff0c\u91c7\u53d6\u6307\u6570\u9000\u907f\u7684\u65b9\u5f0f\u4fdd\u8bc1\u5728\u6700\u5927\u91cd\u8bd5\u6b21\u6570\u4e4b\u5185\u5efa\u8fde\u6210\u529f\u3002<\/p>\n<pre><code>func (a *connection) initConnection() error {\n    var err error\n    backoffConnectDuration := a.reconnectBaseDuration\n\n    for i := 0; i &lt; a.connectRetryTimes || a.connectRetryTimes == -1; i++ {\n        if a.close.Load() {\n            return fmt.Errorf(&quot;connection closed, don't attempt to connect, address: %v&quot;, a.address)\n        }\n        \/\/ 1. \u521d\u59cb\u5316\u7269\u7406\u8fde\u63a5\u548c\u4f20\u8f93\u53cd\u5411\u8fde\u63a5\u5143\u6570\u636e\n        err = a.init()\n        if err == nil {\n            break\n        }\n        log.DefaultLogger.Errorf(&quot;[agent] failed to connect remote server, try again after %v seconds, address: %v, err: %+v&quot;, backoffConnectDuration, a.address, err)\n        time.Sleep(backoffConnectDuration)\n        backoffConnectDuration *= 2\n    }\n    if err != nil {\n        return err\n    }\n    \/\/ 2. \u6258\u7ba1listener\n    utils.GoWithRecover(func() {\n        ch := make(chan api.Connection, 1)\n        a.listener.GetListenerCallbacks().OnAccept(a.rawc, a.listener.UseOriginalDst(), nil, ch, a.readBuffer.Bytes(), []api.ConnectionEventListener{a})\n    }, nil)\n    return nil\n}<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1.<\/strong> a.init( )\u00a0\u65b9\u6cd5\u4f1a\u8c03\u7528 initAgentCoreConnection \u65b9\u6cd5\u521d\u59cb\u5316\u7269\u7406\u8fde\u63a5\u5e76\u5b8c\u6210\u5efa\u8fde\u4ea4\u4e92\u8fc7\u7a0b\u3002Tunnel Server \u901a\u8fc7 Agent \u4f20\u8f93\u7684\u5143\u6570\u636e\u4fe1\u606f\uff0c\u8fdb\u884c\u7ba1\u7406\u53cd\u5411\u8fde\u63a5\u3002\u5177\u4f53\u7684\u4ea4\u4e92\u8fc7\u7a0b\u548c\u534f\u8bae\u540e\u6587\u4f1a\u7ec6\u8bb2\u3002<\/p>\n<p><strong>2.<\/strong> \u5efa\u8fde\u6210\u529f\u4e4b\u540e\uff0cTunnel Agent \u6258\u7ba1 raw conn \u7ed9\u6307\u5b9a\u7684 Listener\u3002\u4e4b\u540e\u8be5 raw conn \u7684\u751f\u547d\u5468\u671f\u7531\u8be5 Listener \u5168\u6743\u7ba1\u7406\uff0c\u5e76\u4e14\u5b8c\u5168\u590d\u7528\u8be5 Listener \u7684\u80fd\u529b\u3002<\/p>\n<p>\u5176\u5b9a\u4e49\u4e86\u521d\u59cb\u5316\u53cd\u5411\u8fde\u63a5\u7684\u4ea4\u4e92\u6d41\u7a0b\uff0c\u5177\u4f53\u4ee3\u7801\u7ec6\u8282\u53ef\u4ee5\u770b\uff1a<\/p>\n<p><em>pkg\/filter\/network\/tunnel\/connection.go:250<\/em>\uff0c\u672c\u6587\u4e0d\u5c55\u5f00\u6280\u672f\u7ec6\u8282\u3002<\/p>\n<h2>Part.4&#8211;\u4ea4\u4e92\u8fc7\u7a0b<\/h2>\n<p>\u76ee\u524d MOSN \u7684\u53cd\u5411\u901a\u9053\u53ea\u652f\u6301\u4e86 raw conn \u7684\u5b9e\u73b0\uff0c\u56e0\u6b64\u5b9a\u4e49\u4e86\u4e00\u5957\u7b80\u5355\u660e\u4e86\u7684\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260232\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<p><strong>&#8211;<\/strong> \u534f\u8bae\u9b54\u6570\uff1a2 byte\uff1b<\/p>\n<p><strong>&#8211;<\/strong> \u534f\u8bae\u7248\u672c\uff1a1 byte\uff1b<\/p>\n<p><strong>&#8211;<\/strong> \u4e3b\u4f53\u7ed3\u6784\u7c7b\u578b\uff1a1 byte\uff0c\u5305\u62ec\u521d\u59cb\u5316\u3001\u5e73\u6ed1\u5173\u95ed\u7b49\uff1b<\/p>\n<p><strong>&#8211;<\/strong> \u4e3b\u4f53\u6570\u636e\u957f\u5ea6\uff1a2 byte\uff1b<\/p>\n<p><strong>&#8211;<\/strong> JSON \u5e8f\u5217\u5316\u7684\u4e3b\u4f53\u6570\u636e\u3002<\/p>\n<p>MOSN \u53cd\u5411\u901a\u9053\u5b8c\u6574\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\u8fc7\u7a0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260233\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5efa\u8fde\u8fc7\u7a0b\u4e2d\u7531 Tunnel Agent \u4e3b\u52a8\u53d1\u8d77\uff0c\u5e76\u4e14\u5728 TCP \u8fde\u63a5\u5efa\u7acb\u6210\u529f <em>\uff08TLS \u63e1\u624b\u6210\u529f\uff09<\/em> \u4e4b\u540e\uff0c\u5c06\u53cd\u5411\u5efa\u8fde\u7684\u5173\u952e\u4fe1\u606f ConnectionInitInfo \u5e8f\u5217\u5316\u5e76\u4f20\u8f93\u7ed9\u5bf9\u7aef Tunnel Server\uff0c\u8be5\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e86\u53cd\u5411\u901a\u9053\u7684\u5143\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<pre><code>\/\/ ConnectionInitInfo is the basic information of agent host,\n\/\/ it is sent immediately after the physical connection is established\ntype ConnectionInitInfo struct {\n    ClusterName      string                 `json:&quot;cluster_name&quot;`\n    Weight           int64                  `json:&quot;weight&quot;`\n    HostName         string                 `json:&quot;host_name&quot;`\n    CredentialPolicy string                 `json:&quot;credential_policy&quot;`\n    Credential       string                 `json:&quot;credential&quot;`\n    Extra            map[string]interface{} `json:&quot;extra&quot;`\n}<\/code><\/pre>\n<p>Tunnel Server \u63a5\u53d7\u8be5\u5143\u6570\u636e\u4fe1\u606f\u4e4b\u540e\uff0c\u4e3b\u8981\u5de5\u4f5c\u5305\u62ec\uff1a<\/p>\n<p><strong>1.<\/strong> \u5982\u679c\u6709\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9274\u6743\u65b9\u5f0f\uff0c\u5219\u8fdb\u884c\u8fde\u63a5\u9274\u6743\uff1b<\/p>\n<p><strong>2.<\/strong> clusterManager \u5c06\u8be5\u8fde\u63a5\u52a0\u5165\u5230\u6307\u5b9a\u7684 ClusterSnapshot \u5e76\u56de\u5199\u5efa\u8fde\u7ed3\u679c\u3002<\/p>\n<p>\u6b64\u65f6\u5efa\u8fde\u8fc7\u7a0b\u624d\u7b97\u5b8c\u6210\u3002<\/p>\n<pre><code>func (t *tunnelFilter) handleConnectionInit(info *ConnectionInitInfo) api.FilterStatus {\n    \/\/ Auth the connection\n    conn := t.readCallbacks.Connection()\n    if info.CredentialPolicy != &quot;&quot; {\n        \/\/ 1. \u81ea\u5b9a\u4e49\u9274\u6743\u64cd\u4f5c\uff0c\u7bc7\u5e45\u539f\u56e0\u7701\u7565\n    }\n    if !t.clusterManager.ClusterExist(info.ClusterName) {\n        writeConnectResponse(ConnectClusterNotExist, conn)\n        return api.Stop\n    }\n    \/\/ Set the flag that has been initialized, subsequent data processing skips this filter\n    err := writeConnectResponse(ConnectSuccess, conn)\n    if err != nil {\n        return api.Stop\n    }\n    conn.AddConnectionEventListener(NewHostRemover(conn.RemoteAddr().String(), info.ClusterName))\n    tunnelHostMutex.Lock()\n    defer tunnelHostMutex.Unlock()\n    snapshot := t.clusterManager.GetClusterSnapshot(context.Background(), info.ClusterName)\n    \/\/ 2. host\u52a0\u5165\u5230\u6307\u5b9a\u7684cluster\n    _ = t.clusterManager.AppendClusterTypesHosts(info.ClusterName, []types.Host{NewHost(v2.Host{\n        HostConfig: v2.HostConfig{\n            Address:    conn.RemoteAddr().String(),\n            Hostname:   info.HostName,\n            Weight:     uint32(info.Weight),\n            TLSDisable: false,\n        }}, snapshot.ClusterInfo(), CreateAgentBackendConnection(conn))})\n    t.connInitialized = true\n    return api.Stop\n}<\/code><\/pre>\n<p>\u7136\u540e\u662f\u901a\u4fe1\u8fc7\u7a0b\uff0c\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\uff0c\u4ee5\u4e0b\u56fe\u8bf7\u6c42\u5355\u5411\u6d41\u8f6c\u793a\u610f\u56fe\u4e3e\u4f8b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260234\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5728\u4f20\u7edf\u7684 MOSN Sidecar \u5e94\u7528\u573a\u666f\u4e2d\uff0cFrontend \u53d1\u9001\u7684\u8bf7\u6c42\u9996\u5148\u7ecf\u8fc7 Client-MOSN\uff0c\u7136\u540e\u901a\u8fc7\u8def\u7531\u6a21\u5757\uff0c\u4e3b\u52a8\u521b\u5efa\u8fde\u63a5 <em>(\u865a\u7ebf\u90e8\u5206)<\/em> \u5e76\u6d41\u8f6c\u5230\u5bf9\u7aef\uff0c\u7ecf\u7531 Server-MOSN biz-listener \u5904\u7406\u8f6c\u4ea4\u7ed9 Backend\u3002<\/p>\n<p>\u800c\u5728\u4e91\u8fb9\u573a\u666f\u7684\u53cd\u5411\u901a\u9053\u5b9e\u73b0\u4e2d\uff0cClient MOSN <em>(Tunnel Server)<\/em> \u5728\u63a5\u53d7\u5230\u5bf9\u7aef Tunnel Agent \u53d1\u8d77\u521b\u5efa\u53cd\u5411\u901a\u9053\u7684\u8bf7\u6c42\u540e\uff0c\u5373\u5c06\u8be5\u7269\u7406\u8fde\u63a5\u52a0\u5165\u8def\u7531\u5230\u5bf9\u7aef MOSN \u7684 cluster snapshot \u4e2d\u3002\u4ece\u800c Frontend \u7684\u8bf7\u6c42\u6d41\u91cf\u80fd\u7531\u8be5\u53cd\u5411\u901a\u9053\u6d41\u8f6c\u5230\u5bf9\u7aef MOSN\uff0c\u800c\u56e0\u4e3a Tunnel Agent \u4fa7\u628a\u8be5\u8fde\u63a5\u6258\u7ba1\u7ed9\u4e86 biz-listener\uff0c\u5219\u8bfb\u5199\u5904\u7406\u90fd\u7531 biz-listener \u8fdb\u884c\u5904\u7406\uff0cbiz-listener \u5c06\u5904\u7406\u5b8c\u7684\u8bf7\u6c42\u518d\u8f6c\u53d1\u7ed9\u771f\u6b63\u7684 Backend \u670d\u52a1\u3002<\/p>\n<h2>Part.5&#8211;\u603b\u7ed3\u548c\u89c4\u5212<\/h2>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86 MOSN \u53cd\u5411\u901a\u9053\u7684\u5b9e\u73b0\u539f\u7406\u548c\u8bbe\u8ba1\u601d\u8def\u3002MOSN \u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684\u4e91\u539f\u751f\u7f51\u7edc\u4ee3\u7406\uff0c\u5e0c\u671b\u53cd\u5411\u901a\u9053\u7684\u80fd\u529b\u80fd\u66f4\u52a0\u6709\u6548\u5730\u652f\u6301\u5176\u4f5c\u4e3a\u4e91\u8fb9\u534f\u540c\u573a\u666f\u4e2d\u627f\u63a5\u4e1c\u897f\u5411\u6d41\u91cf\u7684\u804c\u8d23\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u540e\u7eed\u6211\u4eec\u4e5f\u4f1a\u7ee7\u7eed\u505a\u4e00\u7cfb\u5217\u7684\u62d3\u5c55\u652f\u6301\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<p><strong>1.<\/strong> \u53cd\u5411\u901a\u9053\u652f\u6301 gRPC \u5b9e\u73b0\uff0cgRPC \u4f5c\u4e3a\u4e91\u539f\u751f\u65f6\u4ee3\u6700\u901a\u7528\u7684\u670d\u52a1\u901a\u8baf\u6846\u67b6\uff0c\u672c\u8eab\u5185\u7f6e\u4e86\u5404\u79cd\u5f3a\u5927\u7684\u6cbb\u7406\u80fd\u529b\uff1b<\/p>\n<p><strong>2.<\/strong> \u7ed3\u5408\u66f4\u591a\u4e91\u539f\u751f\u573a\u666f\uff0c\u5185\u7f6e\u66f4\u52a0\u901a\u7528\u7684 Tunnel Server \u52a8\u6001\u670d\u52a1\u53d1\u73b0\u80fd\u529b\u7ec4\u4ef6\uff1b<\/p>\n<p><strong>3.<\/strong> \u66f4\u591a\u7684\u914d\u5957\u81ea\u52a8\u5316\u8fd0\u7ef4\u548c\u90e8\u7f72\u5de5\u5177\u3002<\/p>\n<h3>\u4e86\u89e3\u66f4\u591a\u2026<\/h3>\n<p><strong>MOSN\u00a0Star \u4e00\u4e0b\u2728\uff1a<\/strong><br \/><em><a href=\"https:\/\/link.segmentfault.com\/?enc=kgTk%2FByWapphivkEk%2B50fQ%3D%3D.90S80c%2BYCQo1rHilVukMiRws5UYN03JtcopEXA5VT%2BE%3D\" rel=\"nofollow\">https:\/\/github.com\/mosn\/mosn<\/a><\/em><\/p>\n<p>\u5feb\u6765\u548c\u6211\u4eec\u4e00\u8d77\u5171\u5efa\u5427\ud83e\uddf8<\/p>\n<h3>\u672c\u5468\u63a8\u8350\u9605\u8bfb<\/h3>\n<p>Go \u539f\u751f\u63d2\u4ef6\u4f7f\u7528\u95ee\u9898\u5168\u89e3\u6790<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=PT%2BsLhCG8FRSaIrMC3VKGw%3D%3D.TtrA8iFhuzSQYHxXZP65OTnWwxEPTAtT3QMwjFPMd17PVM1dpzrqFCO0aH5BNa54Qlbsm57yzjNFvc2U1CH%2B%2FRVpj7QGta7SpKXnZuSMhDHqlN%2BdVQHAHTjjuuq0hRqc3Fv1WdRFMkt%2BIaqITQh%2FxORLFM7KlrJGchHD3Cs%2BMKdvJAXWnLV5rkAzqUXooqn3B%2BY6t9lr%2FwXxqm4BpPv%2FRzElsFANKo8gDEHUBbw6cjBIB%2FmDxQ5LxelZFG9pHD24UanTiSk57WKYgNtivI%2Bs6Q%3D%3D\" rel=\"nofollow\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260235\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/a><\/p>\n<p>MOSN \u6784\u5efa Subset \u4f18\u5316\u601d\u8def\u5206\u4eab<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=ilae33XMO5Lx9qGvVN4koQ%3D%3D.u6cgUytwoR17RbSjApHaCO3%2BuGBnEvMc6aqXTDKi0I%2F100t8iQvF5L1lf28NC%2FD6fHXfgwtdE1KkkkqJWnEeZE71fDoA97ofKZILKa7KrmMik4IzKxwgzALp1jfBlriDPkdVlmV3%2FmMEszuiT1%2FvQZX0kuqdHVH2meW%2B7IrgH1aII4QaiST287XwRoKyGrpWVDWVkJkw1cx3bnEAH%2FV4Y%2BLAfWEY%2FF1X687MWHWCNB2ivbmqfTDzfbUFEehsfeTEePmb5N53GvlpfFBOurU0Sg%3D%3D\" rel=\"nofollow\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260236\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/a><\/p>\n<p>MOSN \u6587\u6863\u4f7f\u7528\u6307\u5357<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=u1olr32rwM%2F78RfJWU7SRQ%3D%3D.926pgZb9kp%2FgH7AVAlCG8fn%2F9dVNqfiZ3kVwCrMeT%2BoZMVJtg7yS%2FKz0TFwv7RNyTb1KvmWwFILEh58jEldVRVeBXJNbX7OP%2BfDLpMONsH4xvMuOK03IIA5%2Bs%2F6GQHOKSxNmihkjX2J2NlP9DVBGMW%2BLtGodPnUYiJLJz8d0YmB1umeFdzftgE5jDYnhWdU5oD5qDE5eqivafr9GIYhaSfkqxahaDriNr5JGJcXFM1B38cdkWt%2FpeN2JD4gxGTnF0HC4EdAkrSVtDt9NIKfk3g%3D%3D\" rel=\"nofollow\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260237\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/a><\/p>\n<p>MOSN 1.0 \u53d1\u5e03\uff0c\u5f00\u542f\u65b0\u67b6\u6784\u6f14\u8fdb<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=n%2By1wpFx12jifpr6p7Y2fw%3D%3D.M2IaL0zCYFDWYBSghOu7T8sFs%2FlO3kzVI3h3sUVkB1ymK6ItZi6K76egkBzDInzCvq7%2BjvewX%2BghSDkUV8x1VYPesFxXG85cAMEdLtnnc6lOzcEsnyNDPmDYdrAw%2FrxkcalZO5GlAoiJfOOYZcjxrCORdjOAXwm4N2kST4YF0A7GTe4MKoMODRE0TbZfaHR7AqOUT85jxi%2FVUEq4e4Y2sqZmLuDh3T16H7c%2FkVk473BtouN4K2kbPI4by9G7XZC0WeBo6MWncAJbDiZUClxnNA%3D%3D\" rel=\"nofollow\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260238\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/a><\/p>\n<p>\u6b22\u8fce\u626b\u7801\u5173\u6ce8\u6211\u4eec\u7684\u516c\u4f17\u53f7\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042260239\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042260228\">https:\/\/segmentfault.com\/a\/1190000042260228<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\uff5c\u90d1\u6cfd\u8d85\uff08GitHub ID\uff1aCodingSinger ) \u5b57\u8282\u8df3\u52a8\u9ad8\u7ea7\u5de5\u7a0b\u5e08 \u70ed\u8877\u4e8e\u5fae\u670d\u52a1\u548c Servic [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006093","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006093"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006093\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}