﻿{"id":693,"date":"2020-08-18T15:40:11","date_gmt":"2020-08-18T07:40:11","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=693"},"modified":"2020-08-18T15:45:47","modified_gmt":"2020-08-18T07:45:47","slug":"springboot2-1-1%e4%bd%bf%e7%94%a8springcloud%e7%9a%84feign%e8%b0%83%e7%94%a8eureka%e5%be%ae%e6%9c%8d%e5%8a%a1%e5%b9%b6%e5%bc%80%e5%90%afhystrix%e7%86%94%e6%96%ad%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=693","title":{"rendered":"SpringBoot2.1.1\u4f7f\u7528SpringCloud\u7684Feign\u8c03\u7528Eureka\u5fae\u670d\u52a1\u5e76\u5f00\u542fHystrix\u7194\u65ad\u673a\u5236"},"content":{"rendered":"<p>1\u3001\u5148\u5728pom.xml\u4e2d\u5f15\u5165\u5982\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\t&lt;!--Netflix Eureka\u4f9d\u8d56--&gt;\r\n\t&lt;dependency&gt;\r\n\t\t&lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n\t\t&lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;\/artifactId&gt;\r\n\t&lt;\/dependency&gt;\r\n\t&lt;dependency&gt;\r\n\t\t&lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n\t\t&lt;artifactId&gt;spring-cloud-starter-netflix-ribbon&lt;\/artifactId&gt;\r\n\t&lt;\/dependency&gt;\r\n\t&lt;!--springcloud\u6574\u5408\u7684openFeign--&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-cloud-starter-openfeign&lt;\/artifactId&gt;\r\n        &lt;\/dependency&gt;<\/pre>\n<p>2\u3001\u5728pom.xml\u4e2d\u7684&lt;project&gt;&lt;\/project&gt;\u4e4b\u95f4\u589e\u52a0\u4ee5\u4e0b\u5173\u4e8espringcloud\u7684\u7248\u672c\u7ba1\u7406\uff1a<\/p>\n<pre class=\"lang:default decode:true\">\t&lt;dependencyManagement&gt;\r\n\t\t&lt;dependencies&gt;\r\n\t\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;spring-cloud-dependencies&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;${spring-cloud.version}&lt;\/version&gt;\r\n\t\t\t&lt;type&gt;pom&lt;\/type&gt;\r\n\t\t\t&lt;scope&gt;import&lt;\/scope&gt;\r\n\t\t\t&lt;\/dependency&gt;\r\n\t\t&lt;\/dependencies&gt;\r\n\t&lt;\/dependencyManagement&gt;<\/pre>\n<p>3\u3001\u5728application.propertites\u4e2d\u589e\u52a0\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#feign\u7684\u914d\u7f6e\uff0c\u8fde\u63a5\u8d85\u65f6\u53ca\u8bfb\u53d6\u8d85\u65f6\u914d\u7f6e\r\nfeign.client.config.default.connectTimeout=5000\r\nfeign.client.config.default.readTimeout=5000\r\nfeign.client.config.default.loggerLevel=basic\r\n#\u5f00\u542f\u7194\u65ad\u529f\u80fd\r\nfeign.hystrix.enabled=true<\/pre>\n<p>4\u3001\u5728application\u542f\u52a8\u5165\u53e3\u589e\u52a0@EnableFeignClients\u6ce8\u89e3\uff1a<\/p>\n<pre class=\"lang:default decode:true \">@EnableFeignClients\r\n@ImportResource(\"classpath:hibernate.xml\")\r\n@SpringBootApplication\r\npublic class Zero4jApplication extends SpringBootServletInitializer{\r\n\t\r\n\t@Override\r\n\tprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {\r\n\t\treturn builder.sources(Zero4jApplication.class);\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tSpringApplication.run(Zero4jApplication.class, args);\r\n\t}\r\n\r\n}<\/pre>\n<p>5\u3001\u521b\u5efaFeignClient\u5ba2\u6237\u7aef\uff1a<\/p>\n<pre class=\"lang:default decode:true\">package com.zero4j.controller.api.v1;\r\n\r\nimport org.springframework.cloud.openfeign.FeignClient;\r\nimport org.springframework.web.bind.annotation.DeleteMapping;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.PatchMapping;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.PostMapping;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\n\r\n@FeignClient(value=\"zero4j-provider-area\", fallback=AreasApiHystrixV1.class)\r\npublic interface AreasApiFeignClientV1 {\r\n\r\n\t@PatchMapping(value=\"api\/v1\/areas\/{id}\")\r\n\tString update(\r\n\t\t\t@PathVariable(value=\"id\") String id,\r\n\t\t\t@RequestParam(value=\"token\",required=false) String token,\r\n\t\t\t@RequestParam(value=\"name\",required=false) String name,\r\n\t\t\t@RequestParam(value=\"parentId\",required=false) String parentId,\r\n\t\t\t@RequestParam(value=\"hasChildren\",required=false) Boolean hasChildren\r\n\t\t);\r\n\r\n}\r\n<\/pre>\n<p>6\u3001\u7f16\u5199\u8c03\u7528\u5fae\u670d\u52a1\u7684Controller\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.zero4j.controller.api.v1;\r\n\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\n\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\nimport net.sf.json.JSONObject;\r\n\r\nimport org.hibernate.SessionFactory;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.http.HttpEntity;\r\nimport org.springframework.http.HttpHeaders;\r\nimport org.springframework.http.HttpMethod;\r\nimport org.springframework.http.MediaType;\r\nimport org.springframework.http.ResponseEntity;\r\nimport org.springframework.web.bind.annotation.DeleteMapping;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.PatchMapping;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.PostMapping;\r\nimport org.springframework.web.bind.annotation.RequestAttribute;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.bind.annotation.RestController;\r\nimport org.springframework.web.client.RestTemplate;\r\n\r\nimport com.zero4j.model.area.service.AreaService;\r\n\r\n\r\n@RestController\r\n@RequestMapping(\"\/api\/v1\/areas\")\r\npublic class AreasApiControllerV1 {\r\n\r\n\t@Autowired\r\n\tprivate AreaService areaService;\r\n\t\r\n\t@Autowired\r\n\tprivate SessionFactory sessionFactory;\r\n\t\r\n\t@Autowired\r\n\tprivate RestTemplate restTemplate;\r\n\t\r\n\t@PatchMapping(value=\"\/{id}\")\r\n\tpublic ResponseEntity update(HttpServletRequest request,HttpServletResponse response,\r\n\t\t\t@PathVariable(value=\"id\") String id,\r\n\t\t\t@RequestParam(required=false) String token,\r\n\t\t\t@RequestParam(required=false) String name,\r\n\t\t\t@RequestParam(required=false) String parentId,\r\n\t\t\t@RequestParam(required=false) Boolean hasChildren\r\n\t\t){\r\n\t\t\r\n\t\treturn ResponseEntity.ok((String)areaApiFeignClient.update(id,token,name,parentId,hasChildren));\r\n\t}\r\n\t\r\n\r\n}\r\n<\/pre>\n<p>7\u3001\u7f16\u5199\u7194\u65ad\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.zero4j.controller.api.v1;\r\n\r\nimport net.sf.json.JSONObject;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class AreasApiHystrixV1 implements AreasApiFeignClientV1 {\r\n\r\n\t@Override\r\n\tpublic String update(String id, String token, String name, String parentId, Boolean hasChildren) {\r\n\t\tJSONObject out = new JSONObject();\r\n\t\tout.put(\"status\", 400);\r\n\t\tout.put(\"message\", \"update\u670d\u52a1\u5f02\u5e38\");\r\n\t\tout.put(\"debug\", \"update\u670d\u52a1\u5f02\u5e38\");\r\n\t\treturn out.toString();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>8\u3001\u542f\u52a8\u670d\u52a1\uff0c\u8bbf\u95ee\u5bf9\u5e94\u7684api\uff0c\u5206\u522b\u5728\u5fae\u670d\u52a1\u5f00\u542f\u3001\u5173\u95ed\u7684\u65f6\u5019\u8c03\u7528API\uff0c\u67e5\u770b\u7ed3\u679c\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u5728pom.xml\u4e2d\u5f15\u5165\u5982\u4e0b\u4f9d\u8d56\uff1a &lt;!&#8211;Netflix Eureka\u4f9d\u8d56&#8211;&gt; &lt;d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[167],"tags":[209,208,168,178],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-springboot","tag-feign","tag-hystrix","tag-springboot","tag-springcloud"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=693"}],"version-history":[{"count":2,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":695,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/693\/revisions\/695"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}