(编写不易,转载请注明:http://shihlei.iteye.com/blog/2399457)
一 概述
feign: 声明式rest 客户端,spring cloud 扩展了feign,提供了springmvc的标签支持,替代RestTemplate简化操作。
git:https://github.com/OpenFeign/feign
二 项目
服务提供者:《 SpringCloud(一): SpringBoot 创建简单的微服务》中的时间微服务——spring-cloud-microservice
服务消费者:《 SpringCloud(一): SpringBoot 创建简单的微服务》改进——spring-cloud-webfront 中 TimeService 实现
三 代码实现
(1)添加依赖
org.springframework.cloud spring-cloud-starter-feign
(2)资源文件添加时间微服务服务器配置:
server: port: 20001 # 自定义配置 timeMisroService: server: http://localhost:10001
(3)定义feign接口
package x.demo.springcloud.webfront.service.impl.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import x.demo.springcloud.webfront.service.impl.ProtocolResult; @FeignClient(name = "microservice-time", url = "${timeMisroService.server}") public interface TimeV1MicroServiceClient { @RequestMapping(method = RequestMethod.GET, value = "/time/v1/now", consumes = MediaType.APPLICATION_JSON_VALUE) ProtocolResultnow(@RequestParam(name = "format", required = false) String format); }
(4)实现TimeService
package x.demo.springcloud.webfront.service; public interface TimeService { /** * 获取当前时间 * @return 当前时间,格式:yyyy-MM-dd HH:mm:ss */ String now(); }
package x.demo.springcloud.webfront.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import x.demo.springcloud.webfront.service.TimeService; import x.demo.springcloud.webfront.service.impl.feign.TimeV1MicroServiceClient; @Service("timeV1FeignImpl") public class TimeV1FeignImpl implements TimeService { @Resource private TimeV1MicroServiceClient timeV1MicroServiceClient; /** * 获取当前时间 * * @return 当前时间,格式:yyyy-MM-dd HH:mm:ss */ @Override public String now() { ProtocolResultresult = timeV1MicroServiceClient.now(null); return result.getBody(); } }
(5)controller 调用服务
package x.demo.springcloud.webfront.web; import javax.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import x.demo.springcloud.webfront.service.TimeService; @RestController @RequestMapping("/time") public class TimeController { @Resource(name = "timeV1FeignImpl") private TimeService timeService; @GetMapping("/now") public String now() { return timeService.now(); } }
(6)开启FeignClient支持:主要是添加@EnableFeignClients,用于Spring Cloud 生成 实现
package x.demo.springcloud.webfront; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class SpringCloudWebfrontApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudWebfrontApplication.class, args); } }
(7)效果:同上篇
四 在普通的项目中使用 feign
区别:因为么有spring cloud 实例FeignClient,所以只能自己实例化FeignClient
package x.demo.springcloud.webfront.conf; import feign.Feign; import feign.Request; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.netflix.feign.support.SpringMvcContract; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import x.demo.springcloud.webfront.service.impl.feign.TimeV1MicroServiceClient; @Configuration public class SpringConfiguration { @Value("${timeMisroService.server}") private String timeMisroServiceServer; @Bean public TimeV1MicroServiceClient timeV1MicroServiceClient() { return Feign.builder() .encoder(new GsonEncoder()) .decoder(new GsonDecoder()) .contract(new SpringMvcContract()) .options(new Request.Options(5000, 5000)) .target(TimeV1MicroServiceClient.class, timeMisroServiceServer); } }
五 其他
(1)feign 目前不支持Mvc注解 @GetMapping @PostMapping 会报错
(2)提供json序列化支持
依赖:
io.github.openfeign feign-gson