一、Feign的简介

  Feign是一个声明式 WebService 客户端,使用Feign能够让编写Web Service 客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可插拔式的编码器和解码器。

  Spring Cloud 对 Fiegn 进行了封装,使其支持了Spring MVC 标准注解和HttpMessageConverts。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

  前面使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法,但是在实际的开发中,由于对服务依赖的调用可能不止一处。往往一个接口会被多处调用,所以通常会对每个微服务自行封装一些客户端类来包装依赖服务的调用,所以Feign在此基础上做了进一步封装,有他来帮助我们自定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置他(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成服务提供放的接口绑定,简化了使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量。

二、工程配置

1、在 microservicecloud-api 工程增加pom依赖和代码:

(1)pom.xml文件中添加feign的jar包:spring-cloud-starter-feign

<!-- feign相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

(2)新建DeptClientService接口并且添加@FeignClient注解

@FeignClient(value = "microservicecloud-dept")
public interface DeptClientService
{
@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(@RequestBody Dept dept); @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id); @RequestMapping(value = "/dept/get/list", method = RequestMethod.GET)
public List<Dept> list();
}

说明:

@FeignClient的value值指明对哪个微服务进行负载均衡;

@RequestMapping 是Spring提供的注解,这里可以直接使用以前使用SpringMVC时用过的各种注解,唯一不同的是,这里只是把注解用在了接口上。

2、新建一个消费者项目(Feign):microservicecloud-consumer-dept-feign

(1)添加pom.xml中的依赖

    <dependencies>
<dependency><!-- 自己定义的api -->
<groupId>com.yufeng.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- Ribbon相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

(2)在主启动类中增加 @EnableFeignClients 注解

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class DeptConsumer80_Feign_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}

在主启动类中增加 @EnableFeignClients 注解之后,在Controller中使用 @Autowired 注解导入 DeptClientService 时候才不会报错;

(3)在controller包下增加 DeptController ,使用@Autowired直接注入上面定义的 FeignConsumerService接口对应的实例

@RestController
public class DeptController
{
@Autowired
private DeptClientService deptClientService; @RequestMapping(value = "/consumer/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id)
{
return deptClientService.get(id);
} @RequestMapping(value = "/consumer/dept/get/list", method = RequestMethod.GET)
public List<Dept> list()
{
return deptClientService.list();
}
}

(4)结果验证:启动eureka集群,启动3个生产者服务,启动本服务;

在浏览器中打开:http://localhost/consumer/dept/get/1,可以看到轮询调用了3个生产者服务;

importcom.yufeng.springcloud.entities.Dept;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; /**
* Created by Administrator on 2019/6/11.
*/
@FeignClient(value = "microservicecloud-dept")
public interface FeignConsumerService
{
@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(@RequestBody Dept dept); @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id); @RequestMapping(value = "/dept/get/list", method = RequestMethod.GET)
public List<Dept> list();
}

Feign的介绍和使用的更多相关文章

  1. Feign的介绍与使用(五)

    一.Feign的介绍 Feign是一个声明式 WebService 客户端,使用Feign能够让编写Web Service 客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持J ...

  2. 客户端负载均衡Feign之二:Feign 功能介绍

    一.Ribboon配置 在Spring cloud Feign中客户端负载均衡是通过Spring cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个服务客户端 ...

  3. Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  4. springcloud-知识点总结(二):Ribbon&Feign

    1.Ribbon简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netflix发 ...

  5. 微服务架构之spring cloud feign

    在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...

  6. 打造适用于c#的feign

    之前因为工作原因使用spring cloud全家桶开发过若干项目,发现其中的feign非常好用,以前开发接口客户端的时候都是重复使用HttpClient实现业务,每次新增接口都十分繁琐,故萌生了自定义 ...

  7. Ribbon负载均衡及Feign消费者调用服务

    微服务调用Ribbon 简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netf ...

  8. Feign Ribbon Hystrix 三者关系 | 史上最全, 深度解析

    史上最全: Feign Ribbon Hystrix 三者关系 | 深度解析 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -25[ 博客园 总入口 ] 前言 疯狂创客圈(笔者尼恩创建的 ...

  9. spring cloud学习笔记三 Feign与Ribbon负载均衡的区别

    一.Feign的介绍 Feign一般比较书面的解释是:Feign是一个声明式的WebService客户端,使用Feign编写的WebService客户端更加简单,他的使用方法是定义一个接口,然后在上线 ...

随机推荐

  1. Java常用类Date相关知识

    Date:类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串. Dat ...

  2. docker操作命令大全和后台参数

    一.命令行 可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 CentOS 为操作环境为例): [root@iz2ze2bn5x2wqxdeq65wlpz ...

  3. jquery源码问题

    最近公司升级jquery版本后,原来项目中复选框的attr的使用失效,在查看了jquery的内容的时候发现版本更新,复选框的attr的使用替换成了prop,所以使用的时候出现了问题,但是涉及到的文件太 ...

  4. 入职一个月后 对.net的感想

    我本来应该找Java工程师的岗位的,因种种原因进入了.net开发工程师.然后,我进入了一扇新世界的大门. 1.语法不同,思想相同. 刚入职那几天,每天都好蒙,.net代码语法啥的都和Java不一样,a ...

  5. 绕过基于签名的XSS筛选器:修改HTML

    绕过基于签名的XSS筛选器:修改HTML 在很多情况下,您可能会发现基于签名的过滤器只需切换到一个不太熟悉的执行脚本的方法即可.如果失败了,您需要查看混淆攻击的方法. 本文提供了HTML语法可以被混淆 ...

  6. LeeCode——Second Highest Salary

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  7. Phoenix 无法启动报错: java.net.BindException: Address already in use

    一.问题描述 i. 登录Ambari发现有一个节点的 Phoenix 无法启动 ii. 在Ambari上点击“Start”,监控 Phoenix 日志文件 iii. Phoenix 日志如下: [ro ...

  8. supervisor 管理应用程序

    supervisor 进程管理 主要包含后台进程 supervisord 和控制台 supervisorctl 两个程序 supervisor # 官方文档 http://www.supervisor ...

  9. zz2019年主动学习有哪些进展?答案在这三篇论文里

    2019年主动学习有哪些进展?答案在这三篇论文里 目前推广应用的机器学习方法或模型主要解决分类问题,即给定一组数据(文本.图像.视频等),判断数据类别或将同类数据归类等,训练过程依赖于已标注类别的训练 ...

  10. vue系列--- 认识Flow(一)

    1. 什么是Flow? Flow 是javascript代码的静态类型检查工具.它是Facebook的开源项目(https://github.com/facebook/flow),Vue.js(v2. ...