feign使用_Feign:介绍与使用
在前面的学习中,使用了Ribbon的负载均衡功能,大大简化了远程调用时的代码:
// 定义服务实例访问URLString url = "http://user-service/user/" + id;return restTemplate.getForObject(url, String.class);
如果就学到这里,你可能以后需要编写类似的大量重复代码,格式基本相同,无非参数不一样。有没有更优雅的方式,来对这些代码再次优化呢?
这就是接下来要学的Feign的功能了。
介绍
Feign也叫伪装:
Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。
项目主页:https://github.com/OpenFeign/feign
2 使用【掌握】
目标
使用:Feign进行远程调用
操作步骤
配置依赖
在consumer中添加如下依赖:
org.springframework.cloudspring-cloud-starter-openfeign
编写Feign的客户端
在consumer中编写如下Feign客户端接口类:
package com.icoding.client;
import com.icoding.pojo.User;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("user-service")public interface UserClient {
@GetMapping("/user/{id}") User findOne(@PathVariable("id") Long id);}
首先这是一个接口,Feign会通过动态代理,帮我们生成实现类。这点跟mybatis的mapper很像。
@FeignClient注解,声明这是一个Feign客户端,同时通过value属性指定服务名称。
接口中的方法,完全采用SpringMVC的注解,Feign会根据注解帮我们生成URL,并访问获取结果
@GetMapping中的/user,请不要忘记;因为Feign需要拼接可访问的地址。
编写控制器
定义新的控制器类ConsumerFeignController,使用UserClient访问:
package com.icoding.controller;
import com.icoding.client.UserClient;import com.icoding.pojo.User;import lombok.extern.slf4j.Slf4j;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.RequestMapping;import org.springframework.web.bind.annotation.RestController;
@RestController@RequestMapping("/consumer")@Slf4jpublic class ConsumerFeignController { @Autowired(required = false) private UserClient userClient;
@GetMapping("/{id}") public User findOne(@PathVariable("id") Long id){ return userClient.findOne(id); }}
开启Feign的支持
在ConsumerApplication启动类上,添加注解,开启Feign功能
package com.icoding;
import org.springframework.boot.SpringApplication;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;
/** @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker */@SpringCloudApplication@EnableFeignClients // 开启Feign的支持public class ConsumerApplication { public static void main(String[] args){ SpringApplication.run(ConsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }}
说明:Feign中已经自动集成了Ribbon负载均衡,因此不需要自己定义RestTemplate进行负载均衡的配置。
启动测试
访问接口:http://localhost:8080/consumer/2
回复关键词
JUC 分布式限流 消息队列 alibaba JVM性能调优
看更多精彩教程
别忘了点个在看哦!转发那就太好了!
feign使用_Feign:介绍与使用相关推荐
- feign扫描_feign原理+源码解析
1 架构图. 2 feign的初始化扫包原理. (1)feign使用,是main上的@EnableFeignClients 和 api的@FeignClient(value = "servi ...
- Spring Cloud Feign 负载均衡
一.Feign负载均衡介绍 Feign本身集成了Ribbon依赖和自动配置,因此不需要额外引入依赖,也不需要再注册RestTemplate对象 Feign内置的ribbon默认设置了请求超时时长,默认 ...
- 再见 Feign!推荐一款微服务间调用神器,跟 SpringCloud 绝配!
在微服务项目中,如果我们想实现服务间调用,一般会选择Feign.之前介绍过一款HTTP客户端工具Retrofit,配合SpringBoot非常好用!其实Retrofit不仅支持普通的HTTP调用,还能 ...
- SpringClude--feign介绍
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Feign支持可 ...
- Spring Cloud的负载均衡Spring Cloud Ribbon和Spring Cloud Feign
一.客户端负载均衡:Spring Cloud Ribbon. Spring Cloud Ribbon是基于HTTP和TCP的客户端负载工具,它是基于Netflix Ribbon实现的.通过Spring ...
- [享学Feign] 一、原生Feign初体验,Netflix Feign or Open Feign?
生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...
- 微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关
微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关 1. 微服务简介 1.1 服务架构演变 1.2 SpringCloud ...
- spring cloud+zookeeper+feign整合 简单实例(一)
一.前言 各位热爱知识的小伙伴们大家好呀!很高兴大家能点开这个博客,这是我个人的第一篇博客,之后也会持续的更新java以及spring项目的相关代码,希望大家持续关注.如果对本篇博客有什么不懂的地方或 ...
- 外行人都能看懂的SpringCloud,错过了血亏!
一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...
最新文章
- 网易是世界最好的公司
- npoi上传xlsx文件,并读取数据
- 王者荣耀服务器响应超时,“团战”打游戏,为什么你的网络信号总是连接超时?...
- tkinter使用MySQL存数据_我无法从tkinter表单向mysql插入数据
- “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 (K L题解)
- ubuntu fstab 示例
- 程序的加载和执行(三)——《x86汇编语言:从实模式到保护模式》读书笔记23
- 为什么下拉框拉不下来_太气人了!《除暴》吴彦祖的浴巾为什么就是掉不下来?...
- mac os touch命令_MacOS系统终端常用命令大全
- 二十.激光、视觉和惯导LVIO-SLAM框架学习之相机内参标定
- 一文详解Attention机制
- 结合thinkphp5与hplus(h+)写的一个带权限的后台管理系统
- MacOS Big Sur 11.2.2 (20D80) With and OC 0.6.7 原版DMG黑苹果镜像
- dcp7080d怎么加墨粉_兄弟7080加粉清零方法(兄弟dcp7080d加粉图解)
- (二十六)Storm常见错误及处理方法
- Redux:优点和缺点
- 2021-04-13
- Android wifi 常见断开问题总结
- 苹果Mac合上屏幕怎样才能不休眠?
- 算法 - n个数字形成的圆圈中循环删除第m个数字(C++)
热门文章
- 实用教程丨官方下载Oracle各版本安装软件及补丁包
- 今晚直播:WLS/WAS故障基本分析介绍
- 如何处理会话等待事件与ORA-21780故障
- 诊断案例:Failed parse elapsed time引发db time过高的案例
- 常见的反爬措施:UA反爬和Cookie反爬
- update 没有索引导致业务崩了,老板骂了一个小时
- 教你如何在Python中读,写和解析CSV文
- 教你用Python自制拼图小游戏,轻松搞定熊孩子
- 技术解读丨分布式缓存数据库Redis大KEY问题定位及优化建议
- canal mysql多节点_数据同步的终极解决方案,阿里巴巴开源的Canal框架当之无愧!!...