Feign是spring cloud中服务消费端的调用框架,一般与ribbon,hystrix等组合使用。web
可是在某些项目中,因为遗留缘由,整个系统并非spring cloud项目,甚至不是spring项目,而使用者关注的重点仅仅是简化http调用代码的编写。spring
若是采用httpclient或者okhttp这样相对较重的框架,对初学者来讲编码量与学习曲线都会是一个挑战,而使用spring中RestTemplate,又没有配置化的解决方案,由此想到是否能够脱离spring cloud,独立使用Feign。json
<dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-core</artifactId> <version>8.18.0</version> </dependency>
import feign.Param; import feign.RequestLine; public interface RemoteService { @RequestLine("GET /users/list?name={name}") String getOwner(@Param(value = "name") String name); }
经过@RequestLine
指定HTTP协议及URL地址app
RemoteService service = Feign.builder() .options(new Options(1000, 3500)) .retryer(new Retryer.Default(5000, 5000, 3)) .target(RemoteService.class, "http://127.0.0.1:8085");
options方法指定链接超时时长及响应超时时长,retryer方法指定重试策略,target方法绑定接口与服务端地址。返回类型为绑定的接口类型。框架
String result = service.getOwner("scott");
与调用本地方法相同的方式调用feign包装的接口,直接获取远程服务提供的返回值。maven
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value="users") public class UserController { @RequestMapping(value="/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT}) @ResponseBody public String list(@RequestParam String name) throws InterruptedException{ return name.toUpperCase(); } }
在项目中,服务消费端与生产端之间交换的数据每每是一或多个对象,feign一样提供基于json的对象转换工具,方便咱们直接以对象形式交互。工具
public interface RemoteService { @Headers({"Content-Type: application/json","Accept: application/json"}) @RequestLine("POST /users/list") User getOwner(User user); }
加入@Headers注解,指定Content-Type为json学习
RemoteService service = Feign.builder() .encoder(new JacksonEncoder()) .decoder(new JacksonDecoder()) .options(new Options(1000, 3500)) .retryer(new Retryer.Default(5000, 5000, 3)) .target(RemoteService.class, "http://127.0.0.1:8085");
encoder指定对象编码方式,decoder指定对象解码方式。这里用的是基于Jackson的编、解码方式,须要在pom.xml中添加Jackson的依赖ui
<dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-jackson</artifactId> <version>8.18.0</version> </dependency>
User result = service.getOwner(u);
@Controller @RequestMapping(value="users") public class UserController { @RequestMapping(value="/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT}) @ResponseBody public User list(@RequestBody User user) throws InterruptedException{ System.out.println(user.getUsername()); user.setId(100L); user.setUsername(user.getUsername().toUpperCase()); return user; } }
惟一的变化就是使用了@RequestBody来接收json格式的数据。编码