第一步:检查依赖 <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; } }