1. 什么是Feign?

  Feign是一种声明式、模板化的HTTP客户端,在SpringCloud中使用Feign。可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到是一个远程方法,更感知不到这是一个HTTP请求

  Feign的灵感来自于Retrofit,JAXRS-2.0和WebSocket,它使得Java HTTP客户端编写更方便,旨在通过最少的资源和代码来实现和HTTP API的连接。

2. 引入依赖

  在需要调用HTTP api的工程加入下面依赖

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3. 代码实现

  1. 在启动类上添加@EnableFeignClients注解开启Feign。

  2. 使用接口实现

package com.wangx.cloud.biz;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("spring-cloud-provider")
public interface IUserBiz { @RequestMapping(value = "/api/user/{id}")
String getUser(@PathVariable(value = "id") Integer id);
}

  3. Controller中直接像调用本地方法一样调用

  

package com.wangx.cloud.controller;

import com.wangx.cloud.biz.IUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping(value = "/user", method = RequestMethod.POST)
public class UserController { @Autowired
private IUserBiz iUserBiz; @RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String get(@PathVariable(value = "id") int id) {
return iUserBiz.getUser(id);
}
}

  本示例是在消费者中调用消费者的服务,将会轮询的去调用消费者服务接口。

  注意:负载均衡的策略与Ribbon中的配置方式一样,因为Feign依赖Ribbon,使用的是Ribbon的负载均衡。

3. 抽取接口

  在实际的开发过程中,为了方便管理,通常是将所有的接口和bean都抽取到一个工程中,在消费者中使用的使用,只需要依赖维护接口的jar即可。跟直接在消费者中使用方式和原理一样,只是将接口和传输的实体类单独维护起来,使得开发更简单,明了。在引入的工程中需要让Spring扫描到@FeignClient注解,只需要@EnableFeignClients(basePackages = "包名")即可。

4. 参数说明

  

SpringCloud学习笔记(9)----Spring Cloud Netflix之声明式 REST客户端 -Feign的使用的更多相关文章

  1. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

  2. 3.【Spring Cloud Alibaba】声明式HTTP客户端-Feign

    使用Feign实现远程HTTP调用 什么是Feign Feign是Netflix开源的声明式HTTP客户端 GitHub地址:https://github.com/openfeign/feign 实现 ...

  3. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

  4. spring cloud 声明式rest客户端feign调用远程http服务

    在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...

  5. springCloud学习-消息总线(Spring Cloud Bus)

    1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...

  6. Spring Cloud(三):声明式调用

    声明式服务调用 前面在使用spring cloud时,通常都会利用它对RestTemplate的请求拦截来实现对依赖服务的接口调用,RestTemplate实现了对http的请求封装处理,形成了一套模 ...

  7. Spring Cloud探路(三)REST 客户端Feign

    Declarative REST Client: Feign Feign is a declarative web service client. It makes writing web servi ...

  8. SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

    简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...

  9. Spring Cloud Netflix项目进入维护模式

    任何项目都有其生命周期,Spring Could Netflix也不例外,官宣已进入维护模式,如果在新项目开始考虑技术选型时要考虑到这点风险,并考虑绕道的可能性. 原创: itmuch  IT牧场 这 ...

随机推荐

  1. BAT三家互联网公司哪家更注重用户体验?

    这几天百度的用户体验又成了设计圈关注的对象,李彦宏好不容易刷出来的好感度一下子被打入了冰点,通过此次事件,不难看出现在的互联网用户对于产品的体验要求越来越高,作为一名美图秀秀级别选手,很难领悟“好设计 ...

  2. swift使用查阅资料备份4

    Swift - RxSwift的使用详解6(观察者2: 自定义可绑定属性) http://www.hangge.com/blog/cache/detail_1946.html extension UI ...

  3. java中静态,抽象,接口,继承总结

    (一).静态: 1.静态方法里只能访问静态变量,静态变量是类所特有的,所有类实例都作用同一个变量 静态随着类的加载而加载 (二). 抽象:抽象相当于接口,没有方法体,只定义方法,让子类实现,抽象类中可 ...

  4. EntityFramework 一

    EntityFramework EF核心库 EntityFramework.SqlServer EF针对sqlsever的库 引用 system.Data.Entity   EF相比SQL语句方便,但 ...

  5. Vim 学习指南

    作者:耀耀 出处:http://www.linuxeden.com/html/news/20130820/142667.html Vim 学习指南 来源:开源中国社区 作者:耀耀 关注我们:   你想 ...

  6. C++函数传递数组的两种方式

    数组与指针. 传首地址过去,然后通过地址输出数组元素. 1.一维数组 #include<iostream> using namespace std; #include <cstrin ...

  7. Unity图集分割

    using System.IO;using UnityEngine;using UnityEditor; public class TestSaveSprite{ [MenuItem("LL ...

  8. Ubuntu下使用crontab部署定时任务

    Ubuntu下使用crontab部署定时任务 安装cron apt-get install cron 开启crontab日志 默认情况下的日志是没有开启的,我们需要找到 /etc/rsyslog.d/ ...

  9. js Math常用方法

    ------------------------ 向上取整,有小数就整数部分加1 Math.ceil(5/2) ------------------------ 四舍五入. Math.round(5/ ...

  10. 洛谷 P2393 yyy loves Maths II

    P2393 yyy loves Maths II 题目背景 上次蒟蒻redbag可把yyy气坏了,yyy说他只是小学生,蒟蒻redbag这次不坑他了. 题目描述 redbag给了yyy很多个数,要yy ...