• Spring Cloud构建微服务架构服务消费Feign


    Spring Cloud Feign

    Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

    下面,我们通过一个例子来展现Feign如何方便的声明对eureka-client服务的定义和调用。

    下面的例子,我们将利用之前构建的eureka-server作为服务注册中心、eureka-client作为服务提供者作为基础。而基于Spring Cloud Ribbon实现的消费者,我们可以根据eureka-consumer实现的内容进行简单改在就能完成,具体步骤如下:

    • 根据eureka-consumer复制一个服务消费者工程,命名为:eureka-consumer-feign。在pom.xml中增加下面的依赖:
    
    
    <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies>
    ...
    <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency>
    <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>org.springframework.cloud</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>
    <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>spring-cloud-starter-feign</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>
    </<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency>
    </<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies>
    • 修改应用主类。通过@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功能:
    
    
    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class Application {
     
    public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
    }
    • 创建一个Feign的客户端接口定义。使用@FeignClient注解来指定这个接口所要调用的服务名称,接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口,比如下面就是绑定eureka-client服务的/dc接口的例子:
    
    
    @FeignClient("eureka-client")
    public interface DcClient {
     
    @GetMapping("/dc")
    String consumer();
     
    }
    • 修改Controller。通过定义的feign客户端来调用服务提供方的接口:
    
    
    @RestController
    public class DcController {
     
    @Autowired
    DcClient dcClient;
     
    @GetMapping("/consumer")
    public String dc() {
    return dcClient.consumer();
    }
     
    }

    通过Spring Cloud Feign来实现服务调用的方式更加简单了,通过@FeignClient定义的接口来统一的生命我们需要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用即可。由于Feign是基于Ribbon实现的,所以它自带了客户端负载均衡功能,也可以通过Ribbon的IRule进行策略扩展。另外,Feign还整合的Hystrix来实现服务的容错保护,在Dalston版本中,Feign的Hystrix默认是关闭的。待后文介绍Hystrix带领大家入门之后,我们再结合介绍Feign中的Hystrix以及配置方式。

    在完成了上面你的代码编写之后,读者可以将eureka-server、eureka-client、eureka-consumer-feign都启动起来,来跟踪观察eureka-consumer-feign服务是如何消费eureka-client服务的/dc接口的,并且也可以通过启动多个eureka-client服务来观察其负载均衡的效果。

    Spring <wbr>Cloud构建微服务架构服务消费Feign

    从现在开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源

  • 相关阅读:
    pycharm中将文件目录标记为sources root和sys.path.append()效果一样
    简单的股票信息查询系统 1 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环) 2 允许用户通过模糊查询股票名,比如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来 3 允许按股票价格、涨跌幅、换手率这几列来筛选信息, 比如输入“价格>50”则把价格大于50的股票都打印,输入“市盈率<50“,则把市盈率小于50的股票都打印,不用判断等于。
    添加jar到本地maven库
    jquery.qrcode中文乱码的解决终极办法
    easyUI datagrid view扩展
    CANNOT READ PROPERTY ‘opera’ OF UNDEFINED解决方法
    关于 Promise 的一些简单理解
    Java 内功修炼 之 数据结构与算法(一)
    学习一下 JVM (三) -- 了解一下 垃圾回收
    学习一下 JVM (二) -- 学习一下 JVM 中对象、String 相关知识
  • 原文地址:https://www.cnblogs.com/hhh3/p/8021513.html
Copyright © 2020-2023  润新知