1、快速建立springboot项目集成cloud包
2、pom中加入feign相关jar包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>复制代码
3、项目启动类配置添加注解 @EnableFeignClients 启用feignclient
4、编写接口controller
package com.ang.feign.controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/ang")public class AngController { @PostMapping("/feign") public String angFeign(@RequestBody String ang){ ang += "ang de fan ying"; return ang; }}复制代码
5、编写client
package com.ang.feign.client;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;@FeignClient(value = "AngClient",url = "http://localhost:5214/ang/feign")public interface AngClient { @PostMapping String angReq(@RequestBody String ang);}复制代码
6、编写测试类
@Testvoid contextLoads() { String req = client.angReq("hello ang and "); log.info("ang client response info {}",req);}复制代码
7、源码地址:https://gitee.com/leisure-w/ang-feign.git