Spring Cloud Feign

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

添加依赖

修改 spring-cloud-consul-consumer 的 pom 文件,添加 feign 依赖。

pom.xml

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

修改启动器

修改启动器类,添加 @EnableFeignClients 注解开启扫描Spring Cloud Feign客户端的功能:

ConsuleConsumerApplication.java

package com.louis.spring.cloud.consul.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@EnableFeignClients
@SpringBootApplication
public class ConsuleConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsuleConsumerApplication.class, args);}@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}
}

添加Feign接口

添加 FeignHelloService 接口, 在类头添加注解 @FeignClient("service-producer") ,service-producer是要调用的服务名。

添加跟调用目标方法一样的方法声明,只需要方法声明,不需要具体实现,注意跟目标方法定义保持一致。

FeignHelloService.java

package com.louis.spring.cloud.consul.consumer.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;@FeignClient("service-producer")
public interface FeignHelloService {@RequestMapping("/hello")public String hello();
}

添加控制器

添加 FeignHelloController 控制器,注入 FeignHelloService,就可以像使用本地方法一样进行调用了。

FeignHelloController.java

package com.louis.spring.cloud.consul.consumer.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.louis.spring.cloud.consul.consumer.service.FeignHelloService;@RestController
public class FeignHelloController {@Autowiredprivate FeignHelloService feignHelloService;@RequestMapping("/feign/call")public String call() {// 像调用本地服务一样return feignHelloService.hello();}
}

测试效果

启动成功之后,访问 http://localhost:8521/feign/call,发现调用成功,且 hello consul 和  hello consul two 结果随机出现。

这是因为 Feign 是基于 Ribbon 实现负载均衡的,而我们在上一节中配置了 Ribbon 的负载策略为随机策略。

源码下载

码云:https://gitee.com/liuge1988/spring-cloud-demo.git


作者:朝雨忆轻尘
出处:https://www.cnblogs.com/xifengxiaoma/ 
版权所有,欢迎转载,转载请注明原文作者及出处。

转载于:https://www.cnblogs.com/xifengxiaoma/p/9806291.html

Spring Boot + Spring Cloud 构建微服务系统(三):服务消费和负载(Feign)相关推荐

  1. 使用Spring Boot和Kubernetes构建微服务架构

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 在本教程 ...

  2. Spring Cloud构建微服务架构:分布式服务跟踪(整合zipkin)【Dalston版】

    通过上一篇<分布式服务跟踪(整合logstash)>,我们虽然已经能够利用ELK平台提供的收集.存储.搜索等强大功能,对跟踪信息的管理和使用已经变得非常便利.但是,在ELK平台中的数据分析 ...

  3. Spring Cloud构建微服务架构:分布式服务跟踪(整合logstash)【Dalston版】

    通过之前的<入门示例>,我们已经为两个由SpringCloud构建的微服务项目 trace-1和 trace-2引入了Spring Cloud Sleuth的基础模块 spring-clo ...

  4. Spring Cloud构建微服务架构(七)消息总线(续:Kafka)

    Spring Cloud Bus除了支持RabbitMQ的自动化配置之外,还支持现在被广泛应用的Kafka.在本文中,我们将搭建一个Kafka的本地环境,并通过它来尝试使用Spring Cloud B ...

  5. Spring Cloud构建微服务

    2019独角兽企业重金招聘Python工程师标准>>> Spring Cloud构建微服务 博客分类: 微服务 spring boot 架构 首先了解下项目结构 请忽略config- ...

  6. Spring Cloud构建微服务架构:Hystrix监控面板【Dalston版】

    在上一篇<服务容错保护(hystrix断路器)>的介绍中,我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixComm ...

  7. 1.Spring Cloud 构建微服务应用程序之概览

    1.Spring Cloud 构建微服务应用程序之概览 1.1 微服务发展史 1.2 为什么要学习微服务应用开发? 1.3 微服务和分布式之间的关系 1.4 微服务架构下构建分布式系统带来了哪些问题? ...

  8. Spring Cloud构建微服务架构:分布式服务跟踪(跟踪原理)

    通过上一篇<分布式服务跟踪(入门)>的例子,我们已经通过Spring Cloud Sleuth往微服务应用中添加了实现分布式跟踪具备的基本要素.下面通过本文来详细说说实现分布式服务跟踪的一 ...

  9. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: alt 我们使用Spring Cloud Netflix中的Eureka实 ...

  10. Spring Cloud构建微服务架构:分布式配置中心【Dalston版】

    Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端也称为 ...

最新文章

  1. python基础-牛逼的三层循环,实现想在那里退出,就在那里退出。
  2. 软中断amp;taskletamp;工作队列
  3. 图论--最短路--SPFA
  4. html设置照片模糊效果,CSS如何实现照片模糊?
  5. FastDFS下的storage服务启动卡住
  6. 阿里云天池赛题解析——深度学习篇重磅发布!
  7. 三层交换机VLAN间路由
  8. 【CF1333F】Kate and imperfection(埃氏筛+思维)
  9. wps怎么做时间线_wps中的word文档如何制作时间轴
  10. excel拆分单元格内容_Excel办公软件教程
  11. 花了我一个晚上时间整理的Python魂斗罗小游戏源代码
  12. echarts省级地图显示(入门)
  13. 用计算机好还是自己算好作文,计算机考试后感优秀作文
  14. OpenFOAM 雾化模型头文件信息摘录
  15. Windows11 Docker-Compose 因为挂载问题报错
  16. 形式化方法(Formal Methods)
  17. 计算机应用小数的转换,电脑计算器进位制换算怎么不能输入小数
  18. 大数据入门学习之环境搭建
  19. 计算机考研考线代嘛,计算机考研大纲
  20. 【空间分析之二】点数据集加权平均中心统计(weighed Mean Center)

热门文章

  1. openssh升级后root_又一root神器停止营业!时至今日你还需要root吗
  2. android 获取monkey日志_安卓app测试之Monkey日志分析
  3. 什么是传感器? 传感器由哪几部分组成? 传感器分类?
  4. 计算机应用基础 辅助教学系统,计算机应用基础课程辅助教学及智能测评系统使用手册——网络版.docx...
  5. HTML+CSS+JS实现 ❤️svg图片透明层文本显示❤️
  6. 串行口实验 编写程序利用PC机控制单片机实验板上的数码管设备工作
  7. red flag linux指定域名,Red Flag Server 4.1 系统管理手册(适用桌面linux4.1) 6
  8. 堆排序java实例_堆排序(示例代码)
  9. 6000毫安以上智能手机_展望2021年智能手机市场:这5大技术要爆发
  10. 【蓝桥杯每日一练】 斐波那契数列