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 ...
随机推荐
- SpringMVC绑定到实体数组、list、set、和map时要注意
实体的属性前一定要用.分割,如果是使用jquery的ajax提交的一个js数组对象,则请求数据会被格式化为 var sub = [{name:1,num:2},{name:1,num:2}] $.po ...
- delphi 动态加载dll
引入文件 DLL比较复杂时,可以为它的声明专门创建一个引入单元,这会使该DLL变得更加容易维护和查看.引入单元的格式如下: unit MyDllImport; {Import unit for MyD ...
- L-BFGS
L-BFGS算法比较适合在大规模的数值计算中,具备牛顿法收敛速度快的特点,但不需要牛顿法那样存储Hesse矩阵,因此节省了大量的空间以及计算资源.本文主要通过对于无约束最优化问题的一些常用算法总结,一 ...
- CentOS ASP.NET Core Runtime Jexus跨平台布署
.net core 开源和跨平台,能布署到当前主流的Windows,Linux,macOS 系统上.本篇我们将在 Linux 系统上使用 ASP.NET Core Runtime 和 Jexus 布署 ...
- AJPFX告诉你MT4平台有什么优势?
FX TERMINAL(Meta Trader4)交易平台功能 成功驾驭金融市场的第一步是拥有正确的工具.AJPFX为客户提供二十四小时的在线交易服务,MT4交易软件是目前全世界上最为先进,应用最为广 ...
- HEOI2014 南国满地堆轻絮
题目链接:戳我 就是二分一个数,之后记录一个前缀max,然后和当前数做差再/2即可.(因为我们要使得原来的序列变成不下降序列,所以当然是要控制一个上限,以达到后面较小数能以尽可能小的代价增加) 代码如 ...
- sql语句应考虑哪些安全性?
(1)少使用root账户,应该为不同的动作分配不同的账户: (2)sql执行出错后,不能把数据库中显示的出错信息,直接展示给用户.防止泄露服务器和数据库相关信息: (3)防止sql注入,对特殊字符进行 ...
- java中使用OpenOffice
1. 下载软件/傻瓜安装 OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http://sourceforge.net/proje ...
- Alamofire源码导读五:错误表示
AFError is the error type returned by Alamofire. It encompasses a few different types of errors, eac ...
- OS之进程管理---孤儿进程和僵尸进程
僵尸进程 当一个进程终止时,操作系统会释放其资源,不过它位于进程表中的条目还是在的,直到它的父进程调用wait():这是因为进程表中包含了进程的退出状态.当进程已经终止,但是其父进尚未调用wait() ...