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学习都 ...
随机推荐
- java实现简单的控制台的计算器
要用面向对象的思维写. //我是代码的搬运工import java.util.Scanner;public class Main { public static void main(String[] ...
- C++指针的指针和指针的引用
https://www.cnblogs.com/li-peng/p/4116349.html
- Web开发中的服务器跳转与客户端跳转
两者比较如下: 跳转类型 客户端请求次数 服务端响应次数 URL变化 站外跳转 作用域 服务器跳转 1 1 无 否 pageContext.request.session.application 客 ...
- atoi()和stoi()函数
C++的字符处理函数,把数字字符串转换成int输出 头文件都是#include<cstring> atoi()的参数是 const char* ,因此对于一个字符串str我们必须调用 c_ ...
- 人脸对比微信小程序
https://cloud.tencent.com/document/product/867/17584 测试数据: 图片1: 图片2:
- CSU 1548 Design road(三分查找)
题目链接:https://cn.vjudge.net/problem/142542/origin Description You need to design road from (0, 0) to ...
- 不间断电源(UPS)
UPS电源一般指不间断电源 UPS(Uninterruptible Power System/Uninterruptible Power Supply),即不间断电源,是将蓄电池(多为铅酸免维护蓄电池 ...
- __I、 __O 、__IO是什么意思?怎么用?
出处:http://www.chuxue123.com/forum.php?mod=viewthread&tid=122&ctid=3 __I. __O .__IO是什么意思?这是ST ...
- STM32 实现内部Flash的读写(HAL库版)
Flash 中文名字叫闪存,是一种长寿命的非易失性(断电数据不丢失)的存储器.可以对称为块的存储器单元块进行擦写和再编程,在进行写入操作之前必须先执行擦除.一个Nand Flash由多个块(Block ...
- 使用 Vagrant 搭建 Kubernetes 本地测试环境
Vagrant 中文资料 参考资料 Kubernetes 需要一个至少包含三个节点的分布式系统.如果想学习 Kubernetes,或只是在本地搭建测试环境,则可以通过 Vagrant 来简单的实现. ...