使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
2017-08-25 10:07
1476 查看
如果你的项目使用了SpringCloud微服务技术,那么你就可以使用Feign来作为http客户端来调用远程的http服务。当然,如果你不想使用Feign作为http客户端,也可以使用比如JDK原生的URLConnection、Apache的Http Client、Netty的异步HTTP Client或者Spring的RestTemplate。
那么,为什么我们要使用Feign呢?
首先我们的项目使用了SpringCloud技术,而Feign可以和SpringCloud技术无缝整合。并且,你一旦使用了Feign作为http客户端,调用远程的http接口就会变得像调用本地方法一样简单。
下面就看看Feign是怎么调用远程的http服务的吧。
(1)首先你得引入Feign依赖的jar包:
gradle依赖:
Maven依赖:
(2)在properties配置文件中配置要调用的接口的URL路径(域名部分)
(2)声明要调用的远程接口
说明:
下面就开始调用吧:
Service部分:
Controller部分:
如果觉得本文对您有帮助,不妨扫描下方微信二维码打赏点,您的鼓励是我前进最大的动力:
那么,为什么我们要使用Feign呢?
首先我们的项目使用了SpringCloud技术,而Feign可以和SpringCloud技术无缝整合。并且,你一旦使用了Feign作为http客户端,调用远程的http接口就会变得像调用本地方法一样简单。
下面就看看Feign是怎么调用远程的http服务的吧。
(1)首先你得引入Feign依赖的jar包:
gradle依赖:
compile "org.springframework.cloud:spring-cloud-netflix-core:1.3.2.RELEASE"
Maven依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-core</artifactId> <version>1.3.2.RELEASE</version> </dependency>
(2)在properties配置文件中配置要调用的接口的URL路径(域名部分)
url.xapi=http://xapi.xuebusi.com
(2)声明要调用的远程接口
/** * Created by SYJ on 2017/8/11. */ @FeignClient(name = "xapi", url = "${url.xapi}") @RequestMapping(value = "/Resume", produces = {"application/json;charset=UTF-8"}) public interface ResumeClient { @RequestMapping(value = "/OperateResume", method = RequestMethod.POST) ResultModel sendInterviewRD(@RequestBody FeedBackDto feedBackDto); }
说明:
@FeignClient 是Feign提供的注解,用于通知Feign组件对该接口进行代理(不需要编写接口实现),使用者可直接通过@Autowired注入。
@RequestMapping 是Spring提供的注解,这里可以直接使用以前使用SpringMVC时用过的各种注解,唯一不同的是,这里只是把注解用在了接口上。 如果将Feign与Eureka组合使用,@FeignClient(name = "xapi")意为通知Feign在调用该接口方法时要向Eureka中查询名为 xapi 的服务,从而得到服务URL, 但是远程的http接口并不是我们自己的,我们无法把它注册到Eureka中,所以这里我们就使用 url = "${url.xapi}" 把要调用的接口的url域名部分直接写死到配置文件中。
下面就开始调用吧:
Service部分:
/** * Created by SYJ on 2017/4/26. */ @Service public class InterviewServiceImpl implements InterviewService { @Autowired private ResumeClient resumeClient; @Override public ResultModel sendInterviewRD(FeedBackDto feedBackDto) { return resumeClient.sendInterviewRD(feedBackDto); } }
Controller部分:
/** * Created by SYJ on 2017/4/25. */ @Controller @RequestMapping(value = "/interview", produces = {"application/json;charset=UTF-8"}) public class InterviewController extends BaseController { @Autowired private InterviewService interviewService; /** * Created by SYJ on 2017/4/25. * @param request * @param invitationVo * @return */ @RequestMapping(method = RequestMethod.POST, value = "/sendinterview", produces = {"application/json;charset=UTF-8"}) @ResponseBody public ResultModel sendInterview(HttpServletRequest request, @RequestBody InvitationVo invitationVo) { return interviewService.sendInterviewRD(feedBackDto); } }
如果觉得本文对您有帮助,不妨扫描下方微信二维码打赏点,您的鼓励是我前进最大的动力:
相关文章推荐
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud Feign作为HTTP客户端调用远程HTTP服务,feign熔断器
- Spring Boot 中使用Feign作为HTTP客户端调用远程HTTP服务
- 使用Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
- Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决
- spring cloud 之 Feign 使用HTTP请求远程服务
- Spring Cloud(十一)声名式服务调用:Feign的使用 (上)
- SpringCloud(第 012 篇)电影微服务接入 Feign 进行客户端负载均衡,通过 FeignClient 调用远程 Http 微服务
- spring cloud eureka服务调用出现feign.codec.EncodeException: Could not write request: no suitable HttpMessa
- SpringCloud使用Feign进行服务调用