SpringCloud学习笔记(9)----Spring Cloud Netflix之声明式 REST客户端 -Feign的使用
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的使用的更多相关文章
- SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性
1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...
- 3.【Spring Cloud Alibaba】声明式HTTP客户端-Feign
使用Feign实现远程HTTP调用 什么是Feign Feign是Netflix开源的声明式HTTP客户端 GitHub地址:https://github.com/openfeign/feign 实现 ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
- spring cloud 声明式rest客户端feign调用远程http服务
在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- Spring Cloud(三):声明式调用
声明式服务调用 前面在使用spring cloud时,通常都会利用它对RestTemplate的请求拦截来实现对依赖服务的接口调用,RestTemplate实现了对http的请求封装处理,形成了一套模 ...
- Spring Cloud探路(三)REST 客户端Feign
Declarative REST Client: Feign Feign is a declarative web service client. It makes writing web servi ...
- SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据
简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...
- Spring Cloud Netflix项目进入维护模式
任何项目都有其生命周期,Spring Could Netflix也不例外,官宣已进入维护模式,如果在新项目开始考虑技术选型时要考虑到这点风险,并考虑绕道的可能性. 原创: itmuch IT牧场 这 ...
随机推荐
- string 去除空格
/** * 去除空格 * @param {str} * @param {type} * type: 1-所有空格 2-前后空格 3-前空格 4-后空格 * @return {String} */ ...
- Linux部署之NFS方式安装系统
1. 让客户端从网络启动并且选择第二项 2. 选择语言 3. 选择键盘布局 4. 选择安装方式为NFS 5. ...
- HTML大纲
- 史上最低,低到尘埃,CDR邀你一起嗨购618
盼呀盼,望穿秋水~盼呀盼,何时降价~ 6.4开始,CDR X6全民狂欢618放价活动全面开启 力度之大,范围之广,时间之久,价格之低,都是前所未有的 不负众望,这个618,CDR真的做到一降到底,没有 ...
- spring的JdbcTemplate
一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象. 第一种方式:我们可以在自己定 ...
- Pyhton学习——Day6
# def test(x) : #形参:不占内存空间,调用函数时传入值,程序完成形参释放内存# # 注释内容# # 代码内容# y = x*2# print(y)# # return# # test( ...
- 02操控奴隶——掌握它的语言“Python”
一 编程常识 1编程语言的发展史 程序员是计算机的主人,主人与奴隶沟通的介质是编程语言,编程语言从诞生到现在它经历了那几个阶段呢? 2 语言的特性: 3 初期的编程语言更多的是站在计算机的角度去设计编 ...
- vue 阿里云上传组件
vue 阿里云上传组件 Vue.js上传图片到阿里云OSS存储 测试项目git地址 本测试项目启动方法 示例链接 组件配置项 实践解释 本文主要介绍如何 在vue项目中使用web 直传方式上传阿里云o ...
- 打包成ipa包
http://zengwu3915.blog.163.com/blog/static/27834897201362831449893/?suggestedreading&wumii Xcode ...
- if判断语句
6)if判断语句 if ... then else end if; if ... then elsif ... then elsif ... then else ...