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 ...
随机推荐
- ALIZE初涉
ALIZE初涉 在做GMM-UBM和i-vector时都用到了ALIZE,不得不说十分良心,在linux下很方便,但同时也有一些问题,流程总结如下 安装 在http://alize.univ-avig ...
- 磁盘配额(Quota)的应用与实践
1>什么是Quota 在Linux中,由于是多用户,多任务的环境,所以会有多用户共同使用一个硬盘空间的情况发生,如果其中有少数几个用户大量占用掉了硬盘空间的话,那肯定影响其他用户的使 ...
- Mina Session
Chapter 4 - Session The Session is at the heart of MINA : every time a client connects to the server ...
- .NET Core MemoryCache缓存获取全部缓存键
在Core中不能使用原HttpRuntime.Cache缓存,改为MemoryCache(Microsoft.Extensions.Caching.Memory). 现MemoryCache新版为2. ...
- 学习sqlserve的一些笔记
创建表: create table 表名 { //定义列名 id ,) primary key,//自动编号:从1开始每次增长1,约束:主键约束 name ) not null //非空约束 } 表数 ...
- django Form的回顾--手动档和自动挡
from django.shortcuts import renderfrom django.forms import Formfrom django.forms import fieldsfro ...
- include与file_get_contents区别
参考:http://www.cnblogs.com/bgwan/archive/2013/03/13/2957215.html 一,先来说file_get_contents 这个函数就 ...
- mybatis源码追踪2——将结果集映射为map
org.apache.ibatis.binding.MapperMethod中execute方法 ...} else if (method.returnsMap()) { result = execu ...
- SharedFile System Master Slave(共享文件系统)做ActiveMQ集群
WINDOWS环境下:http://www.apache.org/dyn/closer.cgi?path=/activemq/apache-activemq/5.9.0/apache-activemq ...
- apt错误
安装软件遇到错误,不管安装什么软件都有错误. apt-get install softname 报错如下:E: Sub-process /usr/bin/dpkg returned an error ...