第一步:檢查依賴 <dependency> <!--注冊中心客戶端 eureka--> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <!--遠程調用組件 feign--> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 第二步:調用方(客戶端)A: 2.1 啟動類配置注解 @SpringBootApplication @EnableEurekaClient @EnableFeignClients @EnableHystrix @EnableApolloConfig public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } } 2.2 客戶端 FeignClientHystrix: public class FeignClientHystrix implements FeginClientTest { @Override public int noParamGet() { log.error(" TEST模塊 <----------> 遠程調用失敗"); return 0;// 調用失敗時返回 } @Override public int noParamPost() { return 0; } @Override public int oneParamGet(String token) { return 0; } @Override public int oneParamGet01(String token) { return 0; } @Override public int oneParamPost(UserInfo userInfo) { return 0; } @Override public int oneParamPost01(UserInfo userInfo) { return 0; } @Override public int oneParamPost02(UserInfo userInfo) { return 0; } @Override public int oneParamPost03(UserInfo userInfo) { return 0; } @Override public int MultiParamsPost01(UserInfo userInfo, String token) { return 0; } @Override public int MultiParamsPost02(UserInfo userInfo, String token) { return 0; } @Override public int MultiParamsGet01(UserInfo userInfo, String token) { return 0; } @Override public int MultiParamsGet02(UserInfo userInfo, String token) { return 0; } } FeginClientTest 接口: import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.*; @FeignClient(// 前提是啟動類配置 @EnableFeignClients value = "test", // 遠程模塊在注冊中心中的名稱 不區分大小寫 fallback = FeignClientHystrix.class, // 異常處理類 configuration = FeignClientHystrix.class // 掃描這個類 也可以直接在該類上加 @Component ) @Service public interface FeginClientTest { /** 無參*/ // 無參get請求 /** * 不指明請求方式 默認是GET請求: * * @RequestMapping(value = "testController/fun01") */ @RequestMapping(value = "testController/noParamGet", method = RequestMethod.GET) int noParamGet(); // 無參post請求 @RequestMapping(value = "testController/noParamPost", method = RequestMethod.POST) int noParamPost(); /**單個參數*/ // 單個參數get請求 /** * get請求第一種: 直接對路徑的參數進行設置:只有get請求有 * * @PathVariable: 獲取url中的具體的值進行設置(由SpringMVC提供) */ @RequestMapping(value = "testController/oneParamGet/{token}", method = RequestMethod.GET) String oneParamGet(@PathVariable("token") String token); /** * get請求第二種: */ @RequestMapping(value = "testController/oneParamGet01", method = RequestMethod.GET) String oneParamGet01(@RequestParam("token") String token); // 單個參數post請求 @RequestMapping(value = "testController/oneParamPost", method = RequestMethod.POST) int oneParamPost(@RequestBody UserInfo userInfo); /** * 1.參數前面是@RequestBody時,method 不起作用 * 2.沒有注解 沒有 method */ @RequestMapping(value = "testController/oneParamPost01", method = RequestMethod.GET) int oneParamPost01(@RequestBody UserInfo userInfo); @RequestMapping(value = "testController/oneParamPost02") int oneParamPost02(@RequestBody UserInfo userInfo); @RequestMapping(value = "testController/oneParamPost03") int oneParamPost03(UserInfo userInfo); /** * 多個參數: * 注意: * 所有參數中RequestBody使用的參數只能只有有一個 * 使用RequestBody注解 method就會失效 請求方式就是POST * 調用者A使用RequestBody注解傳參,被調用者B必須用RequestBody接參 */ //多個參數POST請求 @RequestMapping(value = "testController/MultiParamsPost01", method = RequestMethod.GET) int MultiParamsPost01(@RequestBody UserInfo userInfo, @RequestParam("token") String token); @RequestMapping(value = "testController/MultiParamsPost02", method = RequestMethod.POST) int MultiParamsPost02(@RequestParam UserInfo userInfo, @RequestParam("token") String token); //多個參數GET請求 @RequestMapping(value = "testController/MultiParamsGet01", method = RequestMethod.GET) int MultiParamsGet01(@RequestParam UserInfo userInfo, @RequestParam("token") String token); @RequestMapping(value = "testController/MultiParamsGet02") int MultiParamsGet02(@RequestParam UserInfo userInfo, @RequestParam("token") String token); /** * 最后: * 如果 請求參數使用 @RequestParam 並調用和被調用的參數名字一致時, * 被調用方@RequestParam可以省略不寫(單個參數和多個參數都適用) */ } 第三步:被調用方B TestController @RestController @RequestMapping(value = "testController") public class TestController { @GetMapping(value = "/noParamGet") // 請求方式必須和調用者A中的請求方式一致 public int noParamGet(){ return 111111111; } @GetMapping(value = "/oneParamGet/{token}") // 請求方式必須和調用者A中的請求方式一致 public String noParamGet(@RequstParm("token") String token){ return token; } }