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. Failed connect to mirrors.cloud.aliyuncs.com:80

    在yum insatall 安装是报错 Failed connect to mirrors.cloud.aliyuncs.com:80; Connection refused 解决方法: cd /et ...

  2. 信号-linux

    https://www.linuxjournal.com/article/3985 每个信号在 signal.h 头文件中通过宏进行定义,实际是在 signal.h 中定义,对于编号以及信号名的映射关 ...

  3. Flink处理函数实战之一:深入了解ProcessFunction的状态(Flink-1.10)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. rgw实现nfs的首测

    功能介绍 关于rgw实现nfs接口这个,刚接触的人可能并不清楚这个是个什么样的服务架构,rgw是ceph里面的对象存储接口,而nfs则是纯正的网络文件系统接口,这二者如何结合在一起,关于这个,有几个相 ...

  5. no appropriate service handler found,修改数据库的最大连接数,默认150

    no appropriate service handler found,频繁进行数据操作的时候,会出现这种错误.例如,当我读取excel时,一次读取好多数据,这个时候需要修改数据库的最大连接数 se ...

  6. invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"

    解决办法: $ sudo nginx -c /usr/local/etc/nginx/nginx.conf $ sudo nginx -s reload

  7. 宕机了,Redis数据丢了怎么办?

    持续原创输出,点击上方蓝字关注我 目录 前言 什么是AOF? 三种写回策略 日志文件太大怎么办? AOF重写会阻塞主线程吗? AOF的缺点 总结 什么是RDB? 给哪些数据做快照? 快照时能够修改数据 ...

  8. 【老孟Flutter】自定义文本步进组件

    交流 老孟Flutter博客(330个控件用法+实战入门系列文章):http://laomengit.com 欢迎加入Flutter交流群(微信:laomengit).关注公众号[老孟Flutter] ...

  9. 【ubuntu】搭建mysql5.7

    一.安装mysql (一) 安装mysql 注意别安装8,8配置太高了 $: sudo apt-get install mysql-server or $: sudo apt-get install ...

  10. 802.11抓包软件对比之Microsoft Network Monitor

    从事WiFi嵌入式软件开发的同学,802.11协议层抓包分析是一个需要熟练掌握的一个技能,需要通过分析WiFi底层802.11协议层的数据包来定位问题.同时从学习802.11协议的角度而言,最有效的学 ...