{"id":3605,"date":"2021-08-01T17:56:30","date_gmt":"2021-08-01T09:56:30","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=3605"},"modified":"2023-12-04T15:01:48","modified_gmt":"2023-12-04T07:01:48","slug":"%e7%86%94%e6%96%ad%e9%99%8d%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=3605","title":{"rendered":"\u7194\u65ad\u964d\u7ea7"},"content":{"rendered":"<h1>\u6982\u5ff5<\/h1>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/wyxxt.org.cn\/archives\/\u63a5\u53e3\u9650\u6d41.html\">\u63a5\u53e3\u9650\u6d41<\/a><\/p>\n<p>\u4e0e\u63a5\u53e3\u9650\u6d41\u4e0d\u540c\u7684\u662f\uff0c\u7194\u65ad\u964d\u7ea7\u5728\u8c03\u7528\u7aef\u4f7f\u7528\u3002\u800c\u63a5\u53e3\u9650\u6d41\u662f\u5728\u88ab\u8c03\u7528\u7aef\u505a\u7684\u9650\u5236\u3002<\/p>\n<p>\u4fdd\u8bc1\u5fae\u670d\u52a1\u7a33\u5b9a\u7684\u4e09\u5927\u5229\u5668\uff0c<code>\u7f13\u5b58<\/code> <code>\u9650\u6d41<\/code> <code>\u7194\u65ad\u964d\u7ea7<\/code><\/p>\n<h2>\u7194\u65ad<\/h2>\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u5fae\u670d\u52a1\u662f\u5b8c\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u4e1a\u52a1\u529f\u80fd\uff0c\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u53ef\u4ee5\u505a\u5230\u89e3\u8026\uff0c\u6bcf\u4e2a\u5fae\u670d\u52a1\u53ef\u4ee5\u72ec\u7acb\u6f14\u8fdb\u3002\u4f46\u662f\uff0c\u4e00\u4e2a\u5e94\u7528\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u5fae\u670d\u52a1\u7ec4\u6210\uff0c\u5fae\u670d\u52a1\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u901a\u8fc7\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u5b8c\u6210\u3002\u8fd9\u5c31\u5e26\u6765\u4e00\u4e2a\u95ee\u9898\uff0c\u5047\u8bbe\u5fae\u670d\u52a1A\u8c03\u7528\u5fae\u670d\u52a1B\u548c\u5fae\u670d\u52a1C\uff0c\u5fae\u670d\u52a1B\u548c\u5fae\u670d\u52a1C\u53c8\u8c03\u7528\u5176\u5b83\u7684\u5fae\u670d\u52a1\u3002\u5982\u679c\u8c03\u7528\u94fe\u8def\u4e0a\u67d0\u4e2a\u5fae\u670d\u52a1\u7684\u8c03\u7528\u54cd\u5e94\u65f6\u95f4\u8fc7\u957f\u6216\u8005\u4e0d\u53ef\u7528\uff0c\u5bf9\u5fae\u670d\u52a1A\u7684\u8c03\u7528\u5c31\u4f1a\u5360\u7528\u8d8a\u6765\u8d8a\u591a\u7684\u7cfb\u7edf\u8d44\u6e90\uff0c\u8fdb\u800c\u5f15\u8d77\u7cfb\u7edf\u5d29\u6e83\uff0c\u6240\u8c13\u7684\u201c\u96ea\u5d29\u6548\u5e94\u201d\u3002<\/p>\n<p>\u7194\u65ad\u673a\u5236\u662f\u5e94\u5bf9\u96ea\u5d29\u6548\u5e94\u7684\u4e00\u79cd\u5fae\u670d\u52a1\u94fe\u8def\u4fdd\u62a4\u673a\u5236\u3002<\/p>\n<p>\u670d\u52a1\u7194\u65ad\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u6211\u4eec\u5bb6\u7528\u7684\u4fdd\u9669\u4e1d\uff0c\u5f53\u67d0\u670d\u52a1\u51fa\u73b0\u4e0d\u53ef\u7528\u6216\u54cd\u5e94\u8d85\u65f6\u7684\u60c5\u51b5\u65f6\uff0c\u4e3a\u4e86\u9632\u6b62\u6574\u4e2a\u7cfb\u7edf\u51fa\u73b0\u96ea\u5d29\uff0c\u6682\u65f6\u505c\u6b62\u5bf9\u8be5\u670d\u52a1\u7684\u8c03\u7528\u3002<\/p>\n<p>\u7194\u6bb5\u89e3\u51b3\u5982\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u5f53\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u4e0d\u7a33\u5b9a\u65f6\uff0c\u80fd\u591f\u8d77\u5230\u5feb\u901f\u5931\u8d25\u7684\u76ee\u7684\uff1b<\/li>\n<li>\u5feb\u901f\u5931\u8d25\u540e\uff0c\u80fd\u591f\u6839\u636e\u4e00\u5b9a\u7684\u7b97\u6cd5\u52a8\u6001\u8bd5\u63a2\u6240\u4f9d\u8d56\u5bf9\u8c61\u662f\u5426\u6062\u590d<\/li>\n<\/ul>\n<h2>\u964d\u7ea7<\/h2>\n<p>\u670d\u52a1\u964d\u7ea7\u662f\u4ece\u6574\u4e2a\u7cfb\u7edf\u7684\u8d1f\u8377\u60c5\u51b5\u51fa\u53d1\u548c\u8003\u8651\u7684\uff0c\u5bf9\u67d0\u4e9b\u8d1f\u8377\u4f1a\u6bd4\u8f83\u9ad8\u7684\u60c5\u51b5\uff0c\u4e3a\u4e86\u9884\u9632\u67d0\u4e9b\u529f\u80fd\uff08\u4e1a\u52a1\u573a\u666f\uff09\u51fa\u73b0\u8d1f\u8377\u8fc7\u8f7d\u6216\u8005\u54cd\u5e94\u6162\u7684\u60c5\u51b5<\/p>\n<p>\u5728\u5176\u5185\u90e8\u6682\u65f6\u820d\u5f03\u5bf9\u4e00\u4e9b\u975e\u6838\u5fc3\u7684\u63a5\u53e3\u548c\u6570\u636e\u7684\u8bf7\u6c42\uff0c\u800c\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u63d0\u524d\u51c6\u5907\u597d\u7684fallback\uff08\u9000\u8def\uff09\u9519\u8bef\u5904\u7406\u4fe1\u606f\u3002\u8fd9\u6837\uff0c\u867d\u7136\u63d0\u4f9b\u7684\u662f\u4e00\u4e2a\u6709\u635f\u7684\u670d\u52a1\uff0c\u4f46\u5374\u4fdd\u8bc1\u4e86\u6574\u4e2a\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<p>\u4f8b\u5982\uff1a\u5f53\u53cc11\u6d3b\u52a8\u65f6\uff0c\u628a\u65e0\u5173\u4ea4\u6613\u7684\u670d\u52a1\u7edf\u7edf\u964d\u7ea7\uff0c\u5982\u67e5\u770b\u8682\u8681\u6df1\u6797\uff0c\u67e5\u770b\u5386\u53f2\u8ba2\u5355\uff0c\u5546\u54c1\u5386\u53f2\u8bc4\u8bba\uff0c\u53ea\u663e\u793a\u6700\u540e100\u6761\u7b49\u7b49\u3002<\/p>\n<h2>\u7194\u65ad\u548c\u964d\u7ea7\u7684\u533a\u522b<\/h2>\n<p>\u76f8\u540c\u70b9\uff1a<\/p>\n<ul>\n<li>\u76ee\u7684\u5f88\u4e00\u81f4\uff0c\u90fd\u662f\u4ece\u53ef\u7528\u6027\u53ef\u9760\u6027\u7740\u60f3\uff0c\u4e3a\u9632\u6b62\u7cfb\u7edf\u7684\u6574\u4f53\u7f13\u6162\u751a\u81f3\u5d29\u6e83\uff0c\u91c7\u7528\u7684\u6280\u672f\u624b\u6bb5\uff1b<\/li>\n<li>\u6700\u7ec8\u8868\u73b0\u7c7b\u4f3c\uff0c\u5bf9\u4e8e\u4e24\u8005\u6765\u8bf4\uff0c\u6700\u7ec8\u8ba9\u7528\u6237\u4f53\u9a8c\u5230\u7684\u662f\u67d0\u4e9b\u529f\u80fd\u6682\u65f6\u4e0d\u53ef\u8fbe\u6216\u4e0d\u53ef\u7528\uff1b<\/li>\n<li>\u7c92\u5ea6\u4e00\u822c\u90fd\u662f\u670d\u52a1\u7ea7\u522b\uff0c\u5f53\u7136\uff0c\u4e1a\u754c\u4e5f\u6709\u4e0d\u5c11\u66f4\u7ec6\u7c92\u5ea6\u7684\u505a\u6cd5\uff0c\u6bd4\u5982\u505a\u5230\u6570\u636e\u6301\u4e45\u5c42\uff08\u5141\u8bb8\u67e5\u8be2\uff0c\u4e0d\u5141\u8bb8\u589e\u5220\u6539\uff09\uff1b<\/li>\n<li>\u81ea\u6cbb\u6027\u8981\u6c42\u5f88\u9ad8\uff0c\u7194\u65ad\u6a21\u5f0f\u4e00\u822c\u90fd\u662f\u670d\u52a1\u57fa\u4e8e\u7b56\u7565\u7684\u81ea\u52a8\u89e6\u53d1\uff0c\u964d\u7ea7\u867d\u8bf4\u53ef\u4eba\u5de5\u5e72\u9884\uff0c\u4f46\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e0b\uff0c\u5b8c\u5168\u9760\u4eba\u663e\u7136\u4e0d\u53ef\u80fd\uff0c\u5f00\u5173\u9884\u7f6e\u3001\u914d\u7f6e\u4e2d\u5fc3\u90fd\u662f\u5fc5\u8981\u624b\u6bb5\uff1b<\/li>\n<\/ul>\n<p>\u533a\u522b\uff1a<\/p>\n<ul>\n<li>\u89e6\u53d1\u539f\u56e0\u4e0d\u592a\u4e00\u6837\uff0c\u670d\u52a1\u7194\u65ad\u4e00\u822c\u662f\u67d0\u4e2a\u670d\u52a1\uff08\u4e0b\u6e38\u670d\u52a1\uff09\u6545\u969c\u5f15\u8d77\uff0c\u800c\u670d\u52a1\u964d\u7ea7\u4e00\u822c\u662f\u4ece\u6574\u4f53\u8d1f\u8377\u8003\u8651\uff1b<\/li>\n<li>\u7ba1\u7406\u76ee\u6807\u7684\u5c42\u6b21\u4e0d\u592a\u4e00\u6837\uff0c\u7194\u65ad\u5176\u5b9e\u662f\u4e00\u4e2a\u6846\u67b6\u7ea7\u7684\u5904\u7406\uff0c\u6bcf\u4e2a\u5fae\u670d\u52a1\u90fd\u9700\u8981\uff08\u65e0\u5c42\u7ea7\u4e4b\u5206\uff09\uff0c\u800c\u964d\u7ea7\u4e00\u822c\u9700\u8981\u5bf9\u4e1a\u52a1\u6709\u5c42\u7ea7\u4e4b\u5206\uff08\u6bd4\u5982\u964d\u7ea7\u4e00\u822c\u662f\u4ece\u6700\u5916\u56f4\u670d\u52a1\u5f00\u59cb\uff09<\/li>\n<li>\u5b9e\u73b0\u65b9\u5f0f\u4e0d\u592a\u4e00\u6837\uff1b\u670d\u52a1\u964d\u7ea7\u5177\u6709\u4ee3\u7801\u4fb5\u5165\u6027(\u7531\u63a7\u5236\u5668\u5b8c\u6210\/\u6216\u81ea\u52a8\u964d\u7ea7)\uff0c\u7194\u65ad\u4e00\u822c\u79f0\u4e3a\u81ea\u6211\u7194\u65ad\u3002<\/li>\n<\/ul>\n<h1>\u4e3b\u6d41\u6846\u67b6<\/h1>\n<ul>\n<li>Hystrix<\/li>\n<li>Sentinel<\/li>\n<\/ul>\n<h2>hystrix<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_fd0ddb2fa2c94e67567b2a28c426208e.jpg\" alt=\"\" \/><\/p>\n<h3>\u5f02\u6b65<\/h3>\n<pre><code class=\"language-go line-numbers\">hystrix.ConfigureCommand(\"get_baidu\", hystrix.CommandConfig{\n        Timeout:               100,\n        MaxConcurrentRequests: 100,\n        ErrorPercentThreshold: 1,\n    })\n    output := make(chan bool, 0)\n    hystrix.Go(\"get_baidu\", func() error {\n        _, err := http.Get(\"https:\/\/www.baidu.com\/\")\n\n        output &lt;- true\n        if err != nil {\n            fmt.Println(\"get error\")\n            return err\n        }\n        return nil\n    }, func(err error) error {\n        fmt.Println(\"get an error, handle it\", err)\n        output &lt;- false\n        return nil\n    })\n\n    select {\n    case out := &lt;-output:\n        fmt.Println(out)\n    }\n<\/code><\/pre>\n<h3>\u540c\u6b65 + gin<\/h3>\n<pre><code class=\"language-go line-numbers\">func main() {\n    r := router()\n    r.Run()\n}\n\nfunc router() *gin.Engine {\n    r := gin.New()\n    r.Use(BreakerWrapper)\n    r.Use(gin.Recovery())\n    v1 := r.Group(\"\/v1\")\n    {\n        v1.GET(\"\/panic\", Panic)\n    }\n    return r\n}\n\nfunc Panic(c *gin.Context) {\n    c.JSON(200, gin.H{\"message\": \"this is panic!\"})\n}\n\nfunc BreakerWrapper(c *gin.Context) {\n    name := c.Request.Method + \"-\" + c.Request.RequestURI\n    hystrix.ConfigureCommand(name, hystrix.CommandConfig{\n        Timeout:               1000,\n        MaxConcurrentRequests: 100,\n        ErrorPercentThreshold: 1,\n    })\n    hystrix.Do(name, func() error {\n        c.Next()\n        statusCode := c.Writer.Status()\n        if statusCode &gt;= http.StatusInternalServerError {\n            str := fmt.Sprintf(\"status code %d\", statusCode)\n            return errors.New(str)\n        }\n        return nil\n    }, func(err error) error {\n        if err == hystrix.ErrCircuitOpen {\n            c.String(http.StatusAccepted, \"\u8bf7\u7a0d\u540e\u91cd\u8bd5\")\n        }\n        return err\n    })\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u5ff5 \u63a5\u53e3\u9650\u6d41 \u4e0e\u63a5\u53e3\u9650\u6d41\u4e0d\u540c\u7684\u662f\uff0c\u7194\u65ad\u964d\u7ea7\u5728\u8c03\u7528\u7aef\u4f7f\u7528\u3002\u800c\u63a5\u53e3\u9650\u6d41\u662f\u5728\u88ab\u8c03\u7528\u7aef\u505a\u7684\u9650\u5236\u3002 \u4fdd\u8bc1\u5fae\u670d\u52a1\u7a33\u5b9a\u7684\u4e09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"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":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","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-opacity":"","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-opacity":"","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-opacity":"","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-opacity":"","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-opacity":"","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-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[396],"class_list":["post-3605","post","type-post","status-publish","format-standard","hentry","category-15","tag-396"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3605"}],"version-history":[{"count":4,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3605\/revisions"}],"predecessor-version":[{"id":3784,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3605\/revisions\/3784"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}