Feign快速入门
一、Feign简介
1、Feign是一个声明式的web服务客户端,使用Feign编写web服务客户端更加容易
2、具有可插拔注解支持,包括Feign注解和JAX-RS注解,还支持可插拔的编码器与解码器
3、Spring Cloud 增加了对 Spring MVC的注解的支持,Spring Web 默认使用了HttpMessageConverters
4、Spring Cloud 集成了 Ribbon 和 Eureka,在使用Feign时提供负载均衡的HTTP客户端
二、导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
三、开启注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启Feign支持
public class ConsumerApplication { }
四、Feign入门示例
1、ProducerController——服务提供者,在mima-cloud-producer项目中
@RestController
public class ProducerController { @GetMapping("/get/{id}")
public String get(@PathVariable String id) {
return "hi,"+id;
} @GetMapping("/getuser/{id}")
public User getUser(@PathVariable String id) {
System.out.println("getUser.....");
User user = new User();
user.setId(id);
user.setName("wangwu" + id);
return user;
} @PostMapping("/postuser")
public User postUser(@RequestBody User user) {
System.out.println("postUser.....");
return user;
} @GetMapping("/getuser2")
public User getUser2(User user) {
System.out.println("getUser2.....");
return user;
} @GetMapping("/listAll")
public List<User> listAll(){
List<User> users = new ArrayList<User>();
users.add(new User("1","kevin1"));
users.add(new User("2","kevin2"));
users.add(new User("3","kevin3"));
return users;
}
}
以下代码在cloud-consumer-feign项目中
2、FeignTestClient——定义Feign客户端,声明式接口与ProducerController服务提供的方法一一对应
//定义Feign客户端,value参数为provider的serviceName。name参数实际是value的别名
//@FeignClient("mima-cloud-producer")与@FeignClient(name="mima-cloud-producer")本质相同
//@FeignClient(url="")参数已经作废,必须使用name属性
//如果设置url属性, 则name属性则只代表Feign客户端的别名,而不代表服务端的serviceName
@FeignClient(name="mima-cloud-producer")
public interface FeignTestClient { // 可以使用GetMapping组合注解,以前是不能使用的
@GetMapping(value = "/get/{id}")
// @PathVariable必须指定value,否则异常:PathVariable annotation was empty on param 0.
public String get(@PathVariable("id") String id); @RequestMapping(value = "/getuser/{id}")
public User getUser(@PathVariable("id") String id); // 调用远程的post方法,如果参数为复杂对象,就算指定了method=RequestMethod.GET,依然会使用post方式请求
// 远程的方法是get的时候就会失败,错误消息: status 405 reading FeignTestClient#getUser2(User); content:{"timestamp":1511326531240,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/getuser2"}
@RequestMapping(value = "/getuser2", method = RequestMethod.GET)
public User getUser2(User user); // 调用远程的post方法,可以使用@RequestBody
@RequestMapping(value = "/postuser")
public User postUser(@RequestBody User user); // 调用远程的post方法,可以不使用@RequestBody
@RequestMapping(value = "/postuser")
public User postUser2(User user); // 调用远程的post方法,如果参数为复杂对象,就算指定了method=RequestMethod.GET,依然会使用post方式请求
// 远程的方法也是post的,所以可以调用成功
@RequestMapping(value = "/postuser", method = RequestMethod.GET)
public User postUser3(User user); @GetMapping(value = "/listAll")
List<User> listAll();
}
3、FeignTestController——调用Feign客户端
@RestController
public class FeignTestController { @Autowired
private FeignTestClient feignTestClient; @GetMapping("/feign/get/{id}")
public String get(@PathVariable String id) {
String result = feignTestClient.get(id);
return result;
} @GetMapping("/feign/getuser/{id}")
public User getUser(@PathVariable String id) {
User result = feignTestClient.getUser(id);
return result;
} @GetMapping("/feign/getuser2")
public User getUser2(User user) {
User result = feignTestClient.getUser2(new User());
return result;
} @GetMapping("/feign/listAll")
public List<User> listAll() {
return feignTestClient.listAll();
} @PostMapping("/feign/postuser")
public User postUser(@RequestBody User user) {
User result = feignTestClient.postUser(user);
return result;
} @PostMapping("/feign/postuser2")
public User postUser2(@RequestBody User user) {
User result = feignTestClient.postUser2(user);
return result;
} @PostMapping("/feign/postuser3")
public User postUser3(@RequestBody User user) {
User result = feignTestClient.postUser3(user);
return result;
} }
4、开启Feign注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启Feign支持
public class ConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
} }
Feign快速入门的更多相关文章
- Spring Cloud 快速入门
Spring Cloud快速入门 代码地址: https://gitee.com/gloryxu/spring-cloud-test EureKa:服务注册中心 添加依赖 <dependenc ...
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)
[原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
- 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Mybatis框架 的快速入门
MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...
随机推荐
- freemaker超详细 讲解 配置
一.FreeMarker简介 二.第一个FreeMark示例 2.1.新建一个Maven项目 2.2.添加依赖 2.3.添加存放模板的文件夹 2.4.添加模板 2.5.解析模板 2.6.运行结果 三. ...
- Django之Form、ModelForm 组件
Django之Form.ModelForm 组件 一.Form组件: django框架提供了一个form类,来处理web开发中的表单相关事项.众所周知,form最常做的是对用户输入的内容进行验证,为此 ...
- Python开发——数据类型【字符串】
字符串定义 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 在Python中加了引号的字符,都被认为是字符串! 单引号.双引号.多引号之间的区别? 答案:单双引号没有区别 多引号的作用? ...
- Android开发中同时存在多个ListView的处理
在Android开发过程中,有的时候我们需要在一个页面中通过多个ListView展示不同的数据,让用户直观上感觉是一个ListView在变换着数据. 假设有两个ListView,listView1和L ...
- mysql自动删除90天前数据
#coding:utf-8import MySQLdb #方法1直接在Navicat中添加计划任务#DELETE FROM message2 where SEND_TIME < UNIX_TIM ...
- 【Mybatis】MyBatis对表执行CRUD操作(三)
本例在[Mybatis]MyBatis配置文件的使用(二)基础上继续学习对表执行CRUD操作 使用MyBatis对表执行CRUD操作 1.定义sql映射xml文件(EmployeeMapper.xml ...
- 【Selenium】【BugList4】执行pip报错:Fatal error in launcher: Unable to create process using '""D:\Program Files\Python36\python.exe"" "D:\Program Files\Python36\Scripts\pip.exe" '
环境信息: python版本:V3.6.4 安装路径:D:\Program Files\python36 环境变量PATH:D:\Program Files\Python36;D:\Program F ...
- ABP框架系列之五十:(Swagger-UI-集成)
Introduction From it's web site: "....with a Swagger-enabled API, you get interactive documenta ...
- js后加版本号
页面引入两个js文件 <script charset="utf-8" src="mjs/randomimage.js?v=01291"></s ...
- 【转】Closeable, Readable, Flushable, Appendable
Closeable: package java.io; import java.io.IOException; public interface Closeable { /** * Closes th ...