SpringCloud全家桶学习之Feign负载均衡----Feign(四)

1、Feign概述

(1)Feign是什么?  

官网地址:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feignhtml

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

  

   看完官网介绍之后,我整我的是懵逼的,官网老是来一些不像人话的描述,个人理解,简而言之,Feign是一个声明式的Web服务客户端,使用很是方便,只须要建立一个接口,而后添加注解便可完成使用,是否是很简单?github

  为何要使用Feign呢?个人理解:目前你们都习惯面向接口编程,好比WebService接口,dao接口,jdbc等等,已是你们的规范,在目前在项目中服务调用是经过服务名称来调用的,这样并无知足面向接口,因此Feign的出现就是作到面向接口,使用也很是简单(接口+注解)。web

(2)Feign能干什么?

  Feign旨在使编写Java Http客户端变得更容易。前面练习好比:SpringCloud全家桶学习之客户端负载均衡及自定义负载均衡算法----Ribbon(三),是经过Ribbon+RestTemplate,利用RestTemplate对http请求的封装处理,造成了一套模板化的调用方法。可是在实际开发中,因为对微服务的调用可能不止一处,每每一个接口会被多处调用,因此一般都会针对每一个微服务自行封装一些客户端类来包装这些依赖服务的调用。因此,Feign在此基础上作了进一步的封装,由它来帮助咱们定义和实现服务接口的定义。在Feign的实现下,我们只需建立一个接口并使用注解的方式来配置它(之前是Dao接口上面标注Mapper注解,如今是一个微服务接口上边标注一个Feign注解便可),便可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量。算法

  下面,我将我学习的小demo作笔记记录一下。参考地址:https://github.com/OpenFeign/feignspring

  本项目地址:https://github.com/Simple-Coder/microservice-demo-study 编程

2、Feign工程搭建

(1)Maven的工程结构图(本节操做这2个模块)

 (2)microservice-consumer-feign添加pom依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        <dependency>
            <groupId>com.microservice</groupId>
            <artifactId>microservice-inf</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
pom

(3)microservice-inf模块添加接口

 (3)microservice-consumer-feign启动类添加注解(@EnableFeignClients)

 (4)测试:依次启动3个Eureka集群、3个provider、1个consumer-feign

(5)浏览器访问:http://localhost:7001/consumer/get/1001

 

 

   说明Feign工程搭建完成!浏览器

3、总结

  鄙人也在摸索着学习Spring Cloud学习,在使用IDEA搭建Maven聚合工程时,走了不少大坑,好比多模块下的SpringBoot包扫描问题,SpringBoot与Spring Cloud版本之间冲突、等等各类异常,在此模块一个一直很让我头疼的异常,必需要记录一下,异常信息以下:app

com.netflix.client.ClientException: Load balancer does not have available server for client: MICROSERVICE-PROVIDER 负载均衡

 解决方案:

  在application.yml文件中,添加以下配置便可(亲测有效!) 

 

   至此,Feign工程已经搭建完成。