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学习都 ...
随机推荐
- Codeforces 846F - Random Query
原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...
- asp.net开发微信公众平台----目录汇总-持续更新
1.[c#]asp.net微信公众平台开发(1)数据库设计 2.[c#]asp.net微信公众平台开发(2)多层架构框架搭建和入口实现 3.[c#]asp.net微信公众平台开发(3)微信消息封装及反 ...
- 笨办法学Python(learn python the hard way)--练习程序1-10
下面是当初看这本书时按照书中的代码做的练习,一行一行敲下来的,都已经试运行过,没有错误(基于python3),练习1-练习10 #ex1.py 1 #print("Hello world!& ...
- 定时任务cron表达式解析
cron表达式2种: Seconds Minutes Hours DayofMonth Month DayofWeek Year或 Seconds Minutes Hours DayofMonth M ...
- http预请求 options
问题 使用axios请求接口时,出现了发出两次请求的情况:一个是请求OPTIONS,一个是正常的POST请求:查资料发现与跨域请求有关. 概念 MDN中有提到: 出于安全原因,浏览器限制从脚本内发起的 ...
- JMeter 分布式调度压测部署
我们遇到jmeter被假死死了,这里有2个原因,一是jmeter原本是java写的,heap受硬件限制需要调优,二是单机无法分解超大并发比如100万+并发压测,因此,我们分二部走,首先我们需要进行jm ...
- druid spring监控配置
方法一: <bean id="seckillServiceImpl" class="org.seckill.service.impl.SeckillServiceI ...
- Jenkins使用三:管理slave节点(配置SSH公钥和私钥)
添加slave 给节点起个名字 1.远程工作目录:/test/workspace--这个地址是测试机的 jenkins 的 workspace 工作目录,自己随便写个本机的路径2.用法--尽可能的使用 ...
- ag-grid 表格中添加图片
ag-grid是一种非常好用的表格,网上搜索会有各种各样的基本用法,不过对于在ag-grid 表格中添加图片我没有找到方法,看了官方的文档,当然英文的自己也是靠网页翻译,最后发现有这么一个例子,我知道 ...
- vlan trunk配置
vlan trunk配置 Trunk:中继干道 作用:一条链路能承载多个vlan的流量,并对不同vlan的流量进行不同标记 trunk能够使vlan跨越交换机 PT配置 配置vlan:两台交换机同样的 ...