1.Spring Cloud Feign简介

(1).Fegin简介

官方文档:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,

只需要创建一个接口,然后在上面添加注解即可。

官方文档:https://github.com/OpenFeign/feign

(2).Feign作用

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

(3).Feign集成Ribbon

利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。

2.Feign实现步骤

(1).创建工程

[1].新建工程feign

新建microservicecloud-consumer-dept-feign

将consumer里的com包和application.yml配置文件复制到新创建的feign里

修改主启动类名

[2].pom文件

配置microservicecloud-consumer-dept-feign的pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>

<artifactId>microservicecloud</artifactId>

<groupId>com.hosystem</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>microservicecloud-consumer-dept-feign</artifactId>

<dependencies>

<dependency><!-- 自己定义的api -->

<!--注意:groupId我们需要由com.hosystem.springcloud 变成com.hosystem就不会出现'Dependency 'com.hosystem.springcloud:microservicecloud-api:1.0-SNAPSHOT' not found'错误-->

<groupId>com.hosystem</groupId>

<artifactId>microservicecloud-api</artifactId>

<version>${project.version}</version>

</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>

<!-- 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>

<!-- feign相关 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

</dependencies>

</project>

(2).修改microservicecloud-api工程

[1].修改pom文件

新增部分:

<!--feign相关-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

</dependencies>

完整部分:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 -->

<parent>

<artifactId>microservicecloud</artifactId>

<groupId>com.hosystem</groupId>

<version>1.0-SNAPSHOT</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<!--当前Module叫什么名字 -->

<artifactId>microservicecloud-api</artifactId>

<!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->

<dependencies>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</dependency>

<!--feign相关-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

</dependencies>

</project>

[2].创建接口

新建DeptClientService接口并新增注解@FeignClient。

package com.hosystem.springcloud.service;

import com.hosystem.springcloud.entities.Dept;

import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@FeignClient(value = "MICROSERVICECLOUD-DEPT")

public interface DeptClientService

{

@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)

public Dept get(@PathVariable("id") long id);

@RequestMapping(value = "/dept/list",method = RequestMethod.GET)

public List<Dept> list();

@RequestMapping(value = "/dept/add",method = RequestMethod.POST)

public boolean add(Dept dept);

}

[3].mvn clean和mvn install

(3).修改microservicecloud-consumer-dept-feign

microservicecloud-consumer-dept-feign工程修改Controller,添加上一步新建的DeptClientService接口

package com.hosystem.springcloud.controller;

import com.hosystem.springcloud.entities.Dept;

import com.hosystem.springcloud.service.DeptClientService;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;

@RestController //注:这里一定不能忘记注解@RestController 否则出现404;

public class DeptController_Consumer {

//注:如果出现could not autowired no beans of 'DeptClientService' type found. 我们只需要将@autowired改成@Resource即可

@Resource

private DeptClientService service;

@RequestMapping(value = "/consumer/dept/get/{id}")

public Dept get(@PathVariable("id") Long id)

{

return this.service.get(id);

}

@RequestMapping(value = "/consumer/dept/list")

public List<Dept> list()

{

return this.service.list();

}

@RequestMapping(value = "/consumer/dept/add")

public Object add(Dept dept)

{

return this.service.add(dept);

}

}

(4).修改主启动类

修改microservicecloud-consumer-dept-feign主启动类

package com.hosystem.springcloud;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.netflix.feign.EnableFeignClients;

import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients(basePackages= {"com.hosystem.springcloud"})

@ComponentScan("com.hosystem.springcloud")

public class DeptConsumer80_Feign_App

{

public static void main(String[] args)

{

SpringApplication.run(DeptConsumer80_Feign_App.class, args);

}

}

(5).测试

[1].启动eureka集群

[2].启动provider8001、provider8002、provider8003

[3].启动Feign

[4].访问页面

Feign自带负载均衡配置项

http://localhost/consumer/dept/list

(6).总结

Feign通过接口的方法调用Rest服务(之前是Ribbon+RestTemplate),该请求发送给Eureka服务器(http://MICROSERVICECLOUD-DEPT/dept/list),通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。

参考文档:

http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

https://github.com/OpenFeign/feign

6、Sping Cloud Feign的更多相关文章

  1. Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?

    导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...

  2. Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 实践

    Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 目录 Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 引子 FeignClient的 ...

  3. 2、Spring Cloud和dubbo简介

    1.Spring Cloud简介 (1).Spring Cloud简介 SpringCloud,基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负 ...

  4. 一:Spring Boot、Spring Cloud

    上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...

  5. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  6. 笔记:Spring Cloud Feign 其他配置

    请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...

  7. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

  8. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  9. Spring Cloud feign

    Spring Cloud feign使用 前言 环境准备 应用模块 应用程序 应用启动 feign特性 综上 1. 前言 我们在前一篇文章中讲了一些我使用过的一些http的框架 服务间通信之Http框 ...

随机推荐

  1. CSS动画animation

    transition: 过渡动画transition-property: 属性transition-duration: 间隔transition-timing-function: 曲线transiti ...

  2. linux 信号 ctrl + d z c fg bg 作用

    ctrl+c:前台进程终止 后台进程的终止: 方法一:通过jobs命令查看job号(假设为num),然后执行kill %num   $ kill %1 方法二:通过ps命令查看job的进程号(PID, ...

  3. linux 调试&各种知识收集2(持续更新)

    1.查看netlink socket 丢包 cat /proc/net/netlink sk Eth Pid Groups Rmem Wmem Dump Locks Drops Inode c91ed ...

  4. kafka数据一致性(HW只能保证副本之间的数据一致性,并不能保证数据不丢失ack或者不重复。)

    数据一致性问题:消费一致性和存储一致性 例如:一个leader 写入 10条数据,2个follower(都在ISR中),F1.F2都有可能被选为Leader,例如选F2 .后面Leader又活了.可能 ...

  5. Linux (操作二)

    1.U盘的装载与卸载(设备都保存在/dev中  /dev存放设备的文件) 1.卸载u盘 umount /media/xxx/xxx  (xxx为具体路径) 2.查看设备 sudo fdisk -l ( ...

  6. CephFS cache tier实践

    这是一篇分享文,作者因为最近想深入研究下ceph的cache pool,作者写的文章非常的好,这里先直接翻译这篇文章,然后再加入我自己的相关数据 blog原文 作者想启动blog写下自己的Openst ...

  7. Oracle 集合类型

    集合类型 1. 使用条件: a. 单行单列的数据,使用标量变量 .    b. 单行多列数据,使用记录 [ 详细讲解请见: 点击打开链接 ]   c. 单列多行数据,使用集合 *集合:类似于编程语言中 ...

  8. 《Spring Boot 实战纪实》之需求管理

    目录 前言 (思维篇)人人都是产品经理 1.需求文档 1.1 需求管理 1.2 如何攥写需求文档 1.3 需求关键点文档 2 原型设计 2.1 缺失的逻辑 2.2 让想法跃然纸上 3 开发设计文档 3 ...

  9. 精尽 MyBatis 源码分析 - 整体架构

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  10. java开发两年,连Spring的依赖注入的方式都搞不清楚,你工作可能有点悬!

    Spring依赖注入 常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的 ...