springcloud-06-feign的使用
在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端。我们可以使用JDK原生的URLConnection
、Apache的Http Client
、Netty的异步HTTP Client, Spring的RestTemplate
。但是,用起来最方便、最优雅的还是要属Feign了。
Feign简介
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求
下面写一个feign的实例:
pom.xml的配置
<!-- 添加feign 的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
启动类添加注解:
package com.wenbronk.consumer.feign; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
//import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /**
* Created by root on 2017/5/20.
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MovieFeignApplication.class, args);
}
}
3, 编写一个feignClient接口, 以实现远程调用
package com.wenbronk.consumer.feign.feignclient; import com.wenbronk.consumer.feign.entity.UserEntity;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* feign的接口
* Created by root on 2017/5/20.
*/
@FeignClient("microservice-provider-user")
public interface UserFeignClient { /**
* 不可以使用 getMapping, 或者postMapping注解
*
* @param id
* @return
* @PathVariable 中 必须有value的值
*/
@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
public UserEntity findById(@PathVariable("id") Long id); @RequestMapping(value = "/user", method = RequestMethod.POST)
public UserEntity postUser(@RequestBody UserEntity userEntity); /**
* 这儿不会管呗调用的用什么方法
@PostMapping("/user")
public User postUser(@RequestBody User user) {
return user;
}
*/
@RequestMapping(value = "/user", method = RequestMethod.GET)
public UserEntity getUser(UserEntity userEntity); /**
* 如果被调用的方法是对象, 默认是post请求, 对方不可以是get请求
// 该请求不会成功
@GetMapping("/get-user")
public User getUser(User user) {
return user;
}
* @param userEntity
* @return
*/
@RequestMapping(value = "/get-user", method = RequestMethod.GET)
public UserEntity getUserGet(UserEntity userEntity); }
4, 在controller中进行实验
package com.wenbronk.consumer.feign.controller; import com.wenbronk.consumer.feign.entity.UserEntity;
import com.wenbronk.consumer.feign.feignclient.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by root on 2017/5/20.
*/
@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/findUserById/{id}")
public UserEntity findUserById(@PathVariable Long id) {
return userFeignClient.findById(id);
} @PostMapping("/getUser")
public UserEntity getUser(UserEntity user) {
// return userFeignClient.postUser(user);
return userFeignClient.getUser(user);
// return userFeignClient.getUserGet(user);
} }
调用的远程服务为: http://www.cnblogs.com/wenbronk/p/6881573.html
中的user服务
使用feign遇到的坑:
1, feign接口中, GetMapping, PostMapping不支持, 必须使用RequestMapping
2, 使用RestFul请求时, @PathVariables("id"), 和变量同名也必须加 "id"
3, 接口中的参数是对象, 默认使用post方法, 不管是否指定 @RequestMapping(method=..)
springcloud-06-feign的使用的更多相关文章
- SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer);
SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer); 第一种方法: 如果你 ...
- SpringCloud 在Feign上使用Hystrix(断路由)
SpringCloud 在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...
- SpringCloud(5)---Feign服务调用
SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...
- springcloud 实战 feign使用中遇到的相关问题
springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...
- springcloud 之 feign的重复性调用 优化
最近有一个springcloud的feign请求,用于获取坐标经纬度的信息,返回结果永远是固定不变的,所以考虑优化一下,不然每次转换几个坐标都要去请求feign,返回的所有坐标信息,数据量太大导致耗时 ...
- SpringCloud之Feign负载均衡(四)
整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...
- 解决SpringCloud使用Feign跨服调用时header请求头中的信息丢失
在使用SpringCloud进行Feign跨服调用时header请求头中的信息会丢失,是因为Feign是不会带上当前请求的Cookie信息和头信息的,这个时候就需要重写请求拦截. 1.需要重写Requ ...
- SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]
目录 前提:本篇是基于 SpringCloud+Eureka+Feign+Ribbon的简化搭建流程和CRUD练习[1] 的修改与拓展 1.修改consumer的CenterFeign.java,把返 ...
- SpringCloud系列——Feign 服务调用
前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...
- 31.【微服务架构】SpringCloud之Feign(五)
Feign简介 Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Fei ...
随机推荐
- Delphi XE6 Android拨号函数
http://blog.sina.com.cn/s/blog_44fa172f0101rpex.html Delphi XE6 Android拨号函数 (2014-05-07 17:48:51) 转载 ...
- matlab的特殊字符(上下标和希腊字母等)
'T=25\circC',(摄氏度) 下标用 _(下划线) 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...
- Spring Boot 应用系列 2 -- Spring Boot 2 整合MyBatis和Druid
本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(MySQL)的配置. 1. pom.xml文件配置 需要在dependencies节点添加: <!-- MySQL --> ...
- 使用redis实现【统计文章阅读量】及【最热文章】功能
1.视图函数 # 不需要登录装饰器,匿名用户也可访问def article_detail(request, id, slug): # print(slug,id) article = get_obje ...
- python3--django for 循环中,获取序号
功能需求:在前端页面中,for循环id会构不成连续的顺序号,所以要找到一种伪列的方式来根据数据量定义序号 因此就用到了在前端页面中的一个字段 forloop.counter,完美解决 <tbod ...
- “System.Runtime.InteropServices.COMException”类型的第一次机会异常在 System.Windows.Forms.dll 中发生
最近做一个winform项目,在里面用了webbrowser控件进行html文档打印,遇到了标题所示问题.根据查到的一些资料,在调试>异常>查找中输入“System.Runtime.Int ...
- C++派生类与基类对象赋值情况
一 .普通赋值 (同名隐藏) 子类对象调用和父类相同的函数名,该父类的函数名会被隐藏,只会调用子类的函数. Class A { public: void show(); void show(int); ...
- leetcode 121. 买卖股票的最佳时机 JAVA
题目: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票 ...
- Bit Manipulation-476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- MySQL(慢日志记录)
day63 参考:https://www.cnblogs.com/wupeiqi/articles/5716963.html