在前面的学习中,使用了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:介绍与使用相关推荐

  1. feign扫描_feign原理+源码解析

    1 架构图. 2 feign的初始化扫包原理. (1)feign使用,是main上的@EnableFeignClients 和 api的@FeignClient(value = "servi ...

  2. Spring Cloud Feign 负载均衡

    一.Feign负载均衡介绍 Feign本身集成了Ribbon依赖和自动配置,因此不需要额外引入依赖,也不需要再注册RestTemplate对象 Feign内置的ribbon默认设置了请求超时时长,默认 ...

  3. 再见 Feign!推荐一款微服务间调用神器,跟 SpringCloud 绝配!

    在微服务项目中,如果我们想实现服务间调用,一般会选择Feign.之前介绍过一款HTTP客户端工具Retrofit,配合SpringBoot非常好用!其实Retrofit不仅支持普通的HTTP调用,还能 ...

  4. SpringClude--feign介绍

    Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Feign支持可 ...

  5. Spring Cloud的负载均衡Spring Cloud Ribbon和Spring Cloud Feign

    一.客户端负载均衡:Spring Cloud Ribbon. Spring Cloud Ribbon是基于HTTP和TCP的客户端负载工具,它是基于Netflix Ribbon实现的.通过Spring ...

  6. [享学Feign] 一、原生Feign初体验,Netflix Feign or Open Feign?

    生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...

  7. 微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关

    微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关 1. 微服务简介 1.1 服务架构演变 1.2 SpringCloud ...

  8. spring cloud+zookeeper+feign整合 简单实例(一)

    一.前言 各位热爱知识的小伙伴们大家好呀!很高兴大家能点开这个博客,这是我个人的第一篇博客,之后也会持续的更新java以及spring项目的相关代码,希望大家持续关注.如果对本篇博客有什么不懂的地方或 ...

  9. 外行人都能看懂的SpringCloud,错过了血亏!

    一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...

最新文章

  1. Pandas常见的数据过滤方法、通过列条件筛选行数据
  2. python日期时间
  3. UIVisualEffectView实现毛玻璃效果
  4. oracle获取父级,如何通过sql获取oracle connect中的最终父id列
  5. hdu 3954(线段树区间更新)
  6. 算法心经.数学的应用.微分的应用
  7. 命令执行——系统命令执行(三)
  8. aws lambda使用_使用AWS Lambda,S3和AWS CloudFront进行动态内容缓存
  9. .sh文件是什么语言_FastDFS分布式文件系统的搭建安装
  10. java 注解 方法 参数_java在注解中绑定方法参数的解决方案
  11. 数据库 流量切分_私域流量之社群运营技巧,社群运营技巧解析
  12. Hadoop YARN:调度性能优化实践
  13. mysql中如何将几个没有关系的结果集放在一起
  14. spring+mybatis+log4j 输出SQL
  15. C#面试题(String和StringBuilder区别)
  16. 【java编程规范】阿里巴巴编程考试规范+真题答案+考试分享
  17. spyder指定python环境
  18. 漫步者蓝牙只有一边有声音_为什么我蓝牙耳机只有一边有声音啊.
  19. CSS度量单位rem、em、vw、vh详解
  20. 思维导图 · App的商业模式:如何寻找商业化

热门文章

  1. openGauss 2.1.0 闪回特性
  2. 实战分享:activemq 在灾备双活建设中的研究
  3. 视镜:华为云媒体质量管理最新实践
  4. 换个角度思考勒索攻击事件
  5. 物联网打工人必备:LiteOS Studio图形化调测能力
  6. 【华为云技术分享】数据管理服务DAS 之 数据库自动化运维功能展播4:慢SQL
  7. 一图尽览华为云数据库全套安全解决方案
  8. python网站有中文界面吗_手把手教你用python开发界面程序
  9. 红橙Darren视频笔记 view的绘制流程(上) onMeasure测量代码分析 基于API27
  10. PID控制的输入量与输出量的关系