zoukankan      html  css  js  c++  java
  • SpringCloud学习笔记(4)负载均衡——Feign

    负载均衡——Feign

    1、Ribbon和Feign的区别

    Ribbon和Feign都是用于调用其他服务的,不过方式不同。

    1.1 启动类使用的注解不同,Ribbon用的是@RibbonClient,Feign用的是@EnableFeignClients。

    1.2 服务的指定位置不同,Ribbon是在@RibbonClient注解上声明,Feign则是在定义抽象方法的接口中使用@FeignClient声明。

    1.3 调用方式不同,Ribbon需要自己构建http请求,模拟http请求然后使用RestTemplate发送给其他服务,步骤相当繁琐。Feign则是在Ribbon的基础上进行了一次改进,采用接口的方式,将需要调用的其他服务的方法定义成抽象方法即可,不需要自己构建http请求。不过要注意的是抽象方法的注解、方法签名要和提供服务的方法完全一致。

     

    Ribbon——面向服务

    Feign——面向接口

    2、Feign使用

    引入jar包

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

    添加一个接口:

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

    注:接口对应服务提供者对外暴露的访问路径

    启动类添加注解:

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

     

  • 相关阅读:
    实例讲解Springboot以Repository方式整合Redis
    Spark join 源码跟读记录
    数理统计与参数估计杂记
    常见的距离算法和相似度(相关系数)计算方法
    生成模型(Generative Model)与判别模型(Discriminative Model)
    Java 积累复习用
    RangePartitioner 实现简记
    Spark常见问题汇总
    2016年终总结
    学习资料库
  • 原文地址:https://www.cnblogs.com/qzt666/p/11364064.html
Copyright © 2011-2022 走看看