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的使用的更多相关文章

  1. SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer);

    SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer); 第一种方法: 如果你 ...

  2. SpringCloud 在Feign上使用Hystrix(断路由)

    SpringCloud  在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...

  3. SpringCloud(5)---Feign服务调用

    SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...

  4. springcloud 实战 feign使用中遇到的相关问题

    springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...

  5. springcloud 之 feign的重复性调用 优化

    最近有一个springcloud的feign请求,用于获取坐标经纬度的信息,返回结果永远是固定不变的,所以考虑优化一下,不然每次转换几个坐标都要去请求feign,返回的所有坐标信息,数据量太大导致耗时 ...

  6. SpringCloud之Feign负载均衡(四)

    整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...

  7. 解决SpringCloud使用Feign跨服调用时header请求头中的信息丢失

    在使用SpringCloud进行Feign跨服调用时header请求头中的信息会丢失,是因为Feign是不会带上当前请求的Cookie信息和头信息的,这个时候就需要重写请求拦截. 1.需要重写Requ ...

  8. SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]

    目录 前提:本篇是基于 SpringCloud+Eureka+Feign+Ribbon的简化搭建流程和CRUD练习[1] 的修改与拓展 1.修改consumer的CenterFeign.java,把返 ...

  9. SpringCloud系列——Feign 服务调用

    前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...

  10. 31.【微服务架构】SpringCloud之Feign(五)

    Feign简介 Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Fei ...

随机推荐

  1. EBS Archiving and Purging: You Know you need to

    A number of trends in the IT industry have contributed to the increasing size of ERP application dat ...

  2. 【转】【译】【Win10】在你的程序标题栏中显示后退按钮

    原文地址:http://www.sharpgis.net/post/2015/05/21/Displaying-a-backbutton-in-your-app-window 免责声明:这篇文章基于 ...

  3. zookeeper集群崩溃处理

    今天在私有化项目中遇到如下问题: 1.客户反馈用户登录返回303 2.登录服务器查看是大量的log将服务器磁盘空间占用殆尽,导致所有服务进程仍旧存在但是监听端口失败,服务不可用 3.清理日志文件 4. ...

  4. WPF Auto LogOff

    Implementation of Auto Logoff Based on User Inactivity in WPF Application http://www.codeproject.com ...

  5. NERDTree基本使用教程

    Vim插件之属性目录NERDTree   1.下载安装 下载地址: 官网:http://www.vim.org/scripts/script.php?script_id=1658 GitHib:htt ...

  6. c++日志输出库 spdlog 简介(3)多线程控制台输出日志

    spdlog源码分析:https://www.cnblogs.com/eskylin/p/6483199.html spdlog的异步模式使得spdLog可以支持多线程,于是写了一个多线程的小例子: ...

  7. Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...

  8. js中null, undefined 和 typeof

    参考自:http://www.cnblogs.com/wicub/p/3442891.html typeof 是运算符,注意不是函数,是运算符,其作用,是考察变量究竟是什么类型.或曰,是变量是否定义或 ...

  9. 813. Largest Sum of Averages

    We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...

  10. 频繁项集挖掘之Aprior和FPGrowth算法

    频繁项集挖掘的应用多出现于购物篮分析,现介绍两种频繁项集的挖掘算法Aprior和FPGrowth,用以发现购物篮中出现频率较高的购物组合. 基础知识 项:“属性-值”对.比如啤酒2罐.  项集:项的集 ...