spring cloud学习笔记三 Feign与Ribbon负载均衡的区别
一、Feign的介绍
Feign一般比较书面的解释是:Feign是一个声明式的WebService客户端,使用Feign编写的WebService客户端更加简单,他的使用方法是定义一个接口,然后在上线添加注解,,同事也支持JAX-RX标准的注解,Feign也支持可拔插式的编码器和解码器,Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准直接和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用一支持负载均衡。
Feign和Ribbon的区别是:
简单的说,ribbon是直接通过微服务的地址调用服务,Feign是通过调用接口来进行调用服务。下面我就来根据二者的代码来分析两者的区别:
二、Feign的服务调用与Ribbon的服务调用
1.Ribbon的调用模式
a.导入maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
b.创建一个RestBean注入RestTemplate,不直接注入到controller层的原因是:我们还会为RestTemplate注入其他的属性
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; //这个注解表示将这个类注入到spring boot的容器中
@Configuration
public class RestBean { @Bean
@LoadBalanced
//这个注解是Ribbon使用负载均衡的注解
public RestTemplate getRestTemplate() {
return new RestTemplate();
} }
c.创建controller层,调用生产者服务
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import java.util.List; @RestController
public class RibbonController {
//此处的请求前缀是微服务提供者的服务名称,相当于localhost:8080
private static final String producter_url = "http://productor"; /**
* 使用 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap,
* ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
*/
@Autowired
private RestTemplate restTemplate; @RequestMapping(value = "/consumer/dept/list")
public List<String> list()
{
return restTemplate.getForObject(producter_url + "/show", List.class);
} }
我们可以看见,Ribbon的调用模式是直接通过响应为服务的地址进行访问
2.Feign的调用模式
a.导入maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
b.创建一个controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class FeignController { @Autowired
private FeignService feignShow; @RequestMapping(value = "/show")
public List<String> feignMain() { return this.feignShow.show();
}
}
c.创建service
//这个注解表示要调用的微服务名称
@FeignClient(value = "productor")
public interface FeignService { //使用Feign方式调用服务,直接就能调用生产者接口的地址
@RequestMapping(value = "/show", method = RequestMethod.GET)
List<String> show();
}
从上面两个调用方式的对比我们可以看见:Ribbon中没有使用service,而是通过RestTemplate直接通过地址访问生产者的服务;Feign则是和我们平时使用的架构一样,只是service的接口没有自己去实现,而是直接去调用生产者的接口地址。
spring cloud学习笔记三 Feign与Ribbon负载均衡的区别的更多相关文章
- SpringCloud学习笔记(2):使用Ribbon负载均衡
简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...
- Spring Cloud系列文,Feign整合Ribbon和Hysrix
在本博客之前的Spring Cloud系列里,我们讲述了Feign的基本用法,这里我们将讲述下Feign整合Ribbon实现负载均衡以及整合Hystrix实现断路保护效果的方式. 1 准备Eureka ...
- Spring Cloud 学习笔记(一)——入门、特征、配置
[TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...
- spring cloud学习笔记二 ribbon负载均衡
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求.Ribb ...
- SpringCloud的入门学习之概念理解、Ribbon负载均衡入门
1.Ribbon负载均衡,Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端.负载均衡的工具. 答:简单的说,Ribbon是Netflix发布的开源项目,主要功能 ...
- Spring Cloud学习笔记【三】服务消费者Feign
Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单.它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件.F ...
- Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...
- Spring Cloud学习笔记-007
声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
随机推荐
- ECS主动运维事件--让你HOLD住全场 (二)
背景 数月前,我们推出了新的功能:ECS主动运维事件--让你HOLD住全场 https://yq.aliyun.com/articles/573782?spm=a2c4e.11155435.0.0.7 ...
- dede标签大全
想必很多人对后台不熟悉,并且觉得很难.其实不难,只是你们没有找到合适的方法学习而已!只有找到一个合适的学习方法,不管做什么事情,我想都很容易.学习讲究的是效率,而效率又是由思路决定的.就拿网页制作来说 ...
- [luogu]P1168 中位数[堆]
[luogu]P1168 中位数 题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.即前1 ...
- 对拍 & 随机数生成
用 Windows 批处理对拍: 1. 新建一个批处理(.bat),代码如下: :loop@echo off data_creator.exe force_solution.exe correct_s ...
- 初步学习jQuery之事件
事件 页面加载 在DOM中提供了load事件用于页面加载完毕之后执行机制,jQuery提供了ready()方法实现相似的功能,但是存在以下的区别.1.DOM中的load事件没有任何的简写形式,但是在j ...
- 牛客 Rabbit的数列 (线段树维护值为x的个数+区间覆盖)
https://ac.nowcoder.com/acm/contest/907/C 链接:https://ac.nowcoder.com/acm/contest/907/C来源:牛客网 题目描述 Ra ...
- ACM2014-04训练计划
这是我写的第一篇博文,先简单说说今天的状态吧,毕竟我的第一篇博文是今天诞生的.这学期开学以来各种乱忙,开学初准备高数竞赛决赛,而后有一段时间疯狂学习英语,一直到前几天国创项目中的任务,准备数模竞赛,上 ...
- Linux学习篇(四)-Linux 文件管理命令详解
rootfs:根文件系统,Root FileSystem 的简称. Linux 文件命名规则 长度不超过255个字符. 不能使用/当文件名. 严格区分大小写. Linux 目录简介 / 根目录 /bo ...
- day12—jQuery ui引入及初体验
转行学开发,代码100天——2018-03-28 按照所下载教学视频,今天已进行到jQuery UI的学习中.注:本人所用教学视频不是太完整,介绍的内容相对简单,有些只是带过.其他时间中,仍需继续针对 ...
- RichEdit 学习
procedure TForm1.AddText(RichEdit: TRichEdit; Str: string; TextColor: TColor = clBlack; FontName: st ...