Feign 可以当成是一个http client,灵感来自于 Retrofix,Retrofix 也是一款很好用的工具,目前也一直在使用它。但是Feign可以轻而易举的使用ribbon ,hystrix...技术
下面举个入门列子 代码上传在gitHub上面 如下是简要的代码
package com.xlb.test; import java.util.List; import java.util.Map; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @SpringBootApplication @RestController public class DemofeignApplication { private static Map<String, Contributor> map = Maps.newConcurrentMap(); static { Contributor c1 = new Contributor("张三", 1); Contributor c2 = new Contributor("李四", 2); Contributor c3 = new Contributor("王五", 3); map.put("001", c1); map.put("002", c2); map.put("003", c3); } @RequestMapping(value = "/{owner}", method = RequestMethod.GET) public List<Contributor> contributors(@PathVariable String owner) { List<Contributor> contributors = Lists.newArrayList(); if (map.get(owner) == null) { throw new ServiceException("未找到资源"); } else { contributors.add(map.get(owner)); return contributors; } } public static void main(String[] args) { SpringApplication.run(DemofeignApplication.class, args); } } //在定义一个异常拦截器 @ExceptionHandler(RuntimeException.class) @ResponseBody public ServiceException handleException(RuntimeException e, HttpServletResponse response) { response.setStatus(404); ServiceException se = new ServiceException(e.getMessage()); return se; }
然后定义一个 client接口
package com.xlb.test; import java.util.List; import feign.Param; import feign.RequestLine; public interface IService { @RequestLine("GET /{owner}") List<Contributor> contributors(@Param("owner") String owner); }
最后定义一个异常解码器
static class ServiceErrorDecoder implements ErrorDecoder { final Decoder decoder; final ErrorDecoder defaultDecoder = new ErrorDecoder.Default(); ServiceErrorDecoder(Decoder decoder) { this.decoder = decoder; } @Override public Exception decode(String methodKey, Response response) { try { return (Exception) decoder.decode(response, ServiceException.class); } catch (IOException fallbackToDefault) { return defaultDecoder.decode(methodKey, response); } } }
最后调用
IService service = Feign.builder().decoder(new GsonDecoder()) .errorDecoder(new ServiceErrorDecoder(new GsonDecoder())).logger(new Logger.ErrorLogger()) .logLevel(Logger.Level.BASIC).target(IService.class, "http://localhost:8080"); List<Contributor> contributors = service.contributors("004"); if (contributors != null && contributors.size() > 0) { for (Contributor contributor : contributors) { System.out.println(contributor.getLogin() + " (" + contributor.getContributions() + ")"); } }
控制到打印说明
我查询001这个编号:打印
[IService#contributors] ---> GET http://localhost:8080/001 HTTP/1.1
[IService#contributors] <--- HTTP/1.1 200 OK (190ms)
张三 (1)
当我查询004这个编号 因为我在控制到的时候就抛出serviceException并把响应码设置为了404
[IService#contributors] ---> GET http://localhost:8080/004 HTTP/1.1
[IService#contributors] <--- HTTP/1.1 404 Not Found (52ms
github地址:https://github.com/smartxing/Feigin/