{"id":207602,"date":"2025-07-08T12:47:02","date_gmt":"2025-07-08T04:47:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207602\/"},"modified":"2025-07-08T12:47:02","modified_gmt":"2025-07-08T04:47:02","slug":"%e4%b8%8d%e6%8f%90%e4%be%9b-http-%e6%9c%8d%e5%8a%a1%e6%97%b6-golang-%e5%ae%a2%e6%88%b7%e7%ab%af%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207602\/","title":{"rendered":"\u4e0d\u63d0\u4f9b http \u670d\u52a1\u65f6 golang \u5ba2\u6237\u7aef\u8d1f\u8f7d\u5747\u8861\u5668"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e0d\u63d0\u4f9b http \u670d\u52a1\u65f6 golang \u5ba2\u6237\u7aef\u8d1f\u8f7d\u5747\u8861\u5668<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 21:27:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e0d\u63d0\u4f9b http \u670d\u52a1\u65f6 golang \u5ba2\u6237\u7aef\u8d1f\u8f7d\u5747\u8861\u5668\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4f5c\u4e3a\u4e00\u4e2a<strong>golang n00b<\/strong>\uff0c\u6211\u6709\u4e00\u4e2a go \u7a0b\u5e8f\uff0c\u5b83\u5c06\u6d88\u606f\u8bfb\u53d6\u5230 kafka \u4e2d\uff0c\u4fee\u6539\u5b83\u4eec\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u53d1\u5e03\u5230\u5217\u8868\u4e2d\u7684 http \u7aef\u70b9\u4e4b\u4e00\u3002 <\/p>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u505a\u4e86\u4e00\u4e9b\u975e\u5e38\u57fa\u672c\u7684\u968f\u673a\u5faa\u73af<\/p>\n<pre>cur := rand.Int() % len(httpEndpointList)<\/pre>\n<p>\u6211\u60f3\u6539\u8fdb\u8fd9\u4e00\u70b9\uff0c\u5e76\u6839\u636e\u7aef\u70b9\u7684\u54cd\u5e94\u65f6\u95f4\u6216\u7c7b\u4f3c\u5185\u5bb9\u589e\u52a0\u7aef\u70b9\u7684\u6743\u91cd\u3002<\/p>\n<p>\u6211\u7814\u7a76\u8fc7\u5e93\uff0c\u4f46\u6211\u53d1\u73b0\u7684\u6240\u6709\u5e93\u90fd\u662f\u4f7f\u7528 http.handle \u7f16\u5199\u7684\uff0c\u53ef\u7528\u4f5c\u4e2d\u95f4\u4ef6\u3002\u4f8b\u5982\uff0c\u8bf7\u53c2\u9605 oxy lib roundrobin<\/p>\n<p>\u5c31\u6211\u800c\u8a00\uff0c\u6211\u4e0d\u63d0\u4f9b http \u8bf7\u6c42\u3002 <\/p>\n<p><strong>\u6709\u4ec0\u4e48\u60f3\u6cd5\u53ef\u4ee5\u5728\u6211\u7684 golang \u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u8fd9\u79cd\u66f4\u9ad8\u7ea7\u7684\u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861\u5417\uff1f<\/strong><\/p>\n<p>\u6211\u60f3\u907f\u514d\u5728\u6211\u7684\u73af\u5883\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a haproxy \u6216\u7c7b\u4f3c\u7684\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6709\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u52a0\u6743\u968f\u673a\u9009\u62e9\u7b97\u6cd5\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"math\/rand\"\n)\n\ntype endpoint struct {\n    url    string\n    weight int\n}\n\nfunc randomweightedselector(endpoints []endpoint) endpoint {\n    \/\/ this first loop should be optimised so it only gets computed once\n    max := 0\n    for _, endpoint := range endpoints {\n        max = max + endpoint.weight\n    }\n\n    r := rand.intn(max)\n    for _, endpoint := range endpoints {\n        if r &lt; endpoint.weight {\n            return endpoint\n        } else {\n            r = r - endpoint.weight\n        }\n    }\n    \/\/ should never get to this point because r is smaller than max\n    return endpoint{}\n}\n\nfunc main() {\n    endpoints := []endpoint{\n        {weight: 1, url: \"https:\/\/web1.example.com\"},\n        {weight: 2, url: \"https:\/\/web2.example.com\"},\n    }\n\n    count1 := 0\n    count2 := 0\n\n    for i := 0; i &lt; 100; i++ {\n        switch randomweightedselector(endpoints).url {\n        case \"https:\/\/web1.example.com\":\n            count1++\n        case \"https:\/\/web2.example.com\":\n            count2++\n        }\n    }\n    fmt.println(\"times web1: \", count1)\n    fmt.println(\"times web2: \", count2)\n}<\/pre>\n<p>\u5728\u53ef\u4ee5\u4f18\u5316\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u6700\u5e7c\u7a1a\u7684\u3002\u5f53\u7136\uff0c\u5bf9\u4e8e\u751f\u4ea7\u6765\u8bf4\uff0c\u60a8\u4e0d\u5e94\u8be5\u6bcf\u6b21\u90fd\u8ba1\u7b97\u6700\u5927\u503c\uff0c\u4f46\u9664\u6b64\u4e4b\u5916\uff0c\u8fd9\u57fa\u672c\u4e0a\u5c31\u662f\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u66f4\u4e13\u4e1a\u548c\u9762\u5411\u5bf9\u8c61\u7684\u7248\u672c\uff0c\u4e0d\u4f1a\u6bcf\u6b21\u90fd\u91cd\u65b0\u8ba1\u7b97\u6700\u5927\u503c\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"math\/rand\"\n)\n\ntype Endpoint struct {\n    URL    string\n    Weight int\n}\n\ntype RandomWeightedSelector struct {\n    max       int\n    endpoints []Endpoint\n}\n\nfunc (rws *RandomWeightedSelector) AddEndpoint(endpoint Endpoint) {\n    rws.endpoints = append(rws.endpoints, endpoint)\n    rws.max += endpoint.Weight\n}\n\nfunc (rws *RandomWeightedSelector) Select() Endpoint {\n    r := rand.Intn(rws.max)\n    for _, endpoint := range rws.endpoints {\n        if r &lt; endpoint.Weight {\n            return endpoint\n        } else {\n            r = r - endpoint.Weight\n        }\n    }\n    \/\/ should never get to this point because r is smaller than max\n    return Endpoint{}\n}\n\nfunc main() {\n    var rws RandomWeightedSelector\n    rws.AddEndpoint(Endpoint{Weight: 1, URL: \"https:\/\/web1.example.com\"})\n    rws.AddEndpoint(Endpoint{Weight: 2, URL: \"https:\/\/web2.example.com\"})\n\n    count1 := 0\n    count2 := 0\n\n    for i := 0; i &lt; 100; i++ {\n        switch rws.Select().URL {\n        case \"https:\/\/web1.example.com\":\n            count1++\n        case \"https:\/\/web2.example.com\":\n            count2++\n        }\n    }\n    fmt.Println(\"Times web1: \", count1)\n    fmt.Println(\"Times web2: \", count2)\n}<\/pre>\n<p>\u5bf9\u4e8e\u57fa\u4e8e\u7aef\u70b9\u5ef6\u8fdf\u7b49\u6307\u6807\u66f4\u65b0\u6743\u91cd\u7684\u90e8\u5206\uff0c\u6211\u5c06\u521b\u5efa\u4e00\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528\u6b64\u6307\u6807\u6765\u66f4\u65b0 randomweightedselector \u5bf9\u8c61\u4e2d\u7684\u6743\u91cd\u3002\u6211\u8ba4\u4e3a\u4e00\u8d77\u5b9e\u65bd\u5b83\u4f1a\u8fdd\u80cc\u5355\u4e00\u8d23\u4efb\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207602","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207602"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207602\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}