(编写不易,转载请注明: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)添加依赖
<!--rest client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
(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) ProtocolResult<String> now(@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() { ProtocolResult<String> result = 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序列化支持
依赖:
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-gson</artifactId> </dependency>
相关推荐
Laravel开发-restclient 用于PHP的RestClient
RestClient类固醇! 想要将透明的HTTP缓存添加到 gem吗? 就像这样简单: require 'restclient/components' require 'rack/cache' RestClient . enable Rack :: Cache RestClient . get ...
简单的休息测试仪 为实践创建的Odin项目。 使用rest-client gem处理GET请求并输出结果。
Netdot :: RestClient 网络文档工具的RESTful API Ruby客户端 安装 将此行添加到您的应用程序的Gemfile中: gem 'netdot-restclient' 然后执行: $ bundle 或将其自己安装为: $ gem install netdot-restclient ...
资源分类:Python库 所属语言:Python 资源全名:hs_restclient-1.3.4.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
restclient是一个用于测试restful web services的java客户端。
restful http接口测试工具,解压后运行restclient-ui-3.7.1\restclient-ui-3.7.1\bin\restclient-ui.bat即可测试
Wisdom RESTClient https://github.com/Wisdom-Projects/rest-client
C# RestClient组件下载,实现请求接口地址。RestClient请求(form-data)。 RestClient请求(application/json)。字节RestClient请求(form-data)。 支持以下类型 application/json JSON application/xml XML ...
restclient
RESTClient 1.3.5版 下载下来看看是否支持firefox9
RESTClient是用于测试RESTful Web services的Java客户端,这是我用过最好的Restful 测试工具,没有之一! 若没有下载分可以直接点击下面链接进行下载,速度稍慢点! ...
restclient for windows最新版 RESTClient是一款用于测试各种Web服务的工具,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应。使用RESTClient您可以方便的测试各种Web服务,为您的...
RESTClient是一个用于测试RESTful Web services的Java客户端。 标签:RESTClient
添加了用于二进制响应的下载按钮。 修复了一些错误。 版本3.0.5 修复了基本身份验证unicode问题。 添加了OAuth 1.0身份验证。 添加了OAuth 2.0支持。 添加了CURL命令支持,您可以将请求复制为curl命令或粘贴...
restclient工具jar,下载即可使用,建议安装jdk1.7以上版本,可在cmd中打开使用该工具