SpringCloud学习笔记(六):Feign+Ribbon负载均衡

简介

SpringCloud学习笔记(六):Feign+Ribbon负载均衡官网解释:
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组合使用以支持负载均衡。
SpringCloud学习笔记(六):Feign+Ribbon负载均衡
Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,

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

参考官网:https://github.com/OpenFeign/feign

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

Feign集成了Ribbon
利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。

而与Ribbon不同的是, 通过feign只需要定义服务绑定接口且以声明式的方法 ,优雅而简单的实现了服务调用

总结:大家目前都习惯面向接口编程,比如webservice接口,比如DAO接口,这个已经是大家的规范
ribbon:微服务的名字获取调用地址
Feign:通过接口+注解,调用服务。
适应社区其他程序员提出的,是统一的面向接口编程的套路。

Feign使用步骤

参考microservicecloud-consumer-dept-80

新建microservicecloud-consumer-dept-feign

修改主启动类名字
DeptConsumer80_Feign_App
主启动类ribbon注解去掉

microservicecloud-consumer-dept-feign工程pom.xml修改,主要添加对feign的支持

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

修改microservicecloud-api工程(主工程)

新加:

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

完整:

< 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" > 
   < modelVersion > 4.0.0 </ modelVersion > 


   < parent > <!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 --> 
    < groupId > com.atguigu.springcloud </ groupId > 
    < artifactId > microservicecloud </ artifactId > 
    < version > 0.0.1-SNAPSHOT </ version > 
   </ parent > 


   < artifactId > microservicecloud - api </ artifactId > <!-- 当前Module我自己叫什么名字 --> 


   < dependencies > <!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 --> 
    < dependency > 
      < groupId > org.projectlombok </ groupId > 
      < artifactId > lombok </ artifactId > 
    </ dependency > 
   <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-starter-feign</artifactId> 
   </dependency> 
   </ dependencies > 
</ project > 

在主启动类下新建DeptClientService接口并新增注解@FeignClient

package  com.atguigu.springcloud.service; 


import  java.util.List; 


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  com.atguigu.springcloud.entities.Dept; 


@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 ); 
} 
 

mvn clean mvn install

说明:为什么在主启动类中加这个接口?
因为不只是一个微服务会调用这个接口,如果只有一个微服务调用这个接口与,放在那个微服务中也可以。

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

package  com.atguigu.springcloud.controller; 


import  java.util.List; 


import  org.springframework.beans.factory.annotation.Autowired; 
import  org.springframework.web.bind.annotation.PathVariable; 
import  org.springframework.web.bind.annotation.RequestMapping; 
import  org.springframework.web.bind.annotation.RestController; 


import  com.atguigu.springcloud.entities.Dept; 
import  com.atguigu.springcloud.service.DeptClientService; 


@RestController 
public   class  DeptController_Feign 
{ 
   @Autowired 
   private  DeptClientService service =  null ; 


   @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 ); 
  } 
} 

说明:主要是把上面的service interface加载进来

@Autowired 
   private  DeptClientService service =  null ; 

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

package  com.atguigu.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; 
  
  
@SpringBootApplication 
@EnableEurekaClient 
@EnableFeignClients(basePackages= {"com.atguigu.springcloud"}) 
@ComponentScan("com.atguigu.springcloud") 
public   class  DeptConsumer80_Feign_App 
{ 
   public   static   void  main(String[]  args ) 
  { 
   SpringApplication. run (DeptConsumer80_Feign_App. class ,  args ); 
  } 
} 
 
 

说明:这里的配置是和上面microservicecloud-api中的

@FeignClient (value =  "MICROSERVICECLOUD-DEPT" ) 
public   interface  DeptClientService 

相对应的

主要是增加如下两个注解:

@EnableFeignClients(basePackages= {"com.atguigu.springcloud"}) 
//@FeignClient所在的包,因为这里所有的服务都是在一个项目中所以microservicecloud-api项目(api项目中的包路径就是这个)
@ComponentScan("com.atguigu.springcloud") 
//扫描的包

测试

启动3个eureka集群
启动3个部门微服务8001/8002/8003
启动Feign自己启动
http://localhost/consumer/dept/list
Feign自带负载均衡配置项

小结


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

Feign并不是一个负载均衡的技术,正如上面讲的是为了通过接口的方式调用Rest服务,因为Feign融合了Ribbon,所以同样支持负载均衡了。不要被文章的标题迷惑了。