微服务学习笔记六 Spring Cloud Ribbon负载均衡

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 微服务学习笔记六 Spring Cloud Ribbon负载均衡

## Ribbon

Spring Cloud Ribbon是一个负载均衡的解决方案,是netflix发布的负载均衡器,

基于netflix Ribbon实现的,是一个用于对HTTP请求进行控制的负载均衡客户端。


在注册中心对Ribbon进行注册之后,Ribbon就可以基于某种负载均衡算法,如

轮询、随机、加权轮询、加权随机等自动帮助服务消费者调用接口,开发者也

可以自定义Ribbon负载均衡算法。

Spring Cloud Ribbon需要结合Spring Cloud Eureka来使用,Eureka server

提供所有可以调用的服务提供者列表,Ribbon基于特定的负载均衡算法从这些服务

提供者中选择要调用的具体实例。

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200711114536192.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

**它没有服务,只是提供一个负载均衡的功能**

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200711114603596.png)

创建模块,pom.xml依赖


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```

创建配置文件 application.yml


```yaml

server:

 port: 8084

spring:

 application:

   name: ribbon

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

 instance:

   prefer-ip-address: true

```

创建启动类


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;


@SpringBootApplication

public class RibbonApplication {

   public static void main(String[] args) {

       SpringApplication.run(RibbonApplication.class,args);

   }


   @Bean

   @LoadBalanced

   public RestTemplate restTemplate(){

       return new RestTemplate();

   }

}

```

@LoadBalanced:声明一个基于Ribbon的负载均衡。

Handler


```java

package com.shuang.controller;


import com.shuang.entity.Student;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;


import java.util.Collection;


@RestController

@RequestMapping("/ribbon")

public class RibbonHandler {


   @Autowired

   private RestTemplate restTemplate;


   @GetMapping("/findAll")

   public Collection<Student> findAll(){

       return restTemplate.getForObject("http://provider/student/findAll",Collection.class);

   }


   @GetMapping("/index")

   public String index(){

       return restTemplate.getForObject("http://provider/student/index",String.class);

   }



}

```

**依次启动:注册中心、服务提供者一、服务提供者二、ribbon负载均衡。**

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200711114805373.png)

8080与8082两个服务提供者交替执行。

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200711114825532.png)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/2020071111484129.png)

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
5天前
|
缓存 负载均衡 监控
探索分布式系统演进之路:从负载均衡到微服务架构
小米分享了分布式系统的发展,从早期的负载均衡(入口级、网关和客户端)到微服务架构的演进。微服务实现服务解耦,增强系统弹性,但带来了新的挑战。为优化数据库性能,实施了主备读写分离、全文搜索引擎、缓存集群等措施。通过微服务治理,如服务注册、动态配置、灰度发布等,提升了系统稳定性和可靠性。未来将继续优化分布式系统,提供更好的服务体验。关注公众号“软件求生”了解更多。
26 6
|
6天前
|
缓存 负载均衡 算法
【微服务 SpringCloud】实用篇 · Ribbon负载均衡
【微服务 SpringCloud】实用篇 · Ribbon负载均衡
24 0
|
6天前
|
负载均衡 算法 Java
Ribbon自定义负载均衡算法
Ribbon自定义负载均衡算法
16 1
|
6天前
|
负载均衡 算法 应用服务中间件
【微服务系列笔记】负载均衡
本文介绍了负载均衡的概念和重要性,指出随着流量增长,通过垂直扩展和水平扩展来提升系统性能,其中水平扩展引入了负载均衡的需求。负载均衡的目标是将流量分布到多台服务器以提高响应速度和可用性,常见的硬件和软件负载均衡器包括F5、A10、Nginx、HAProxy和LVS等。 文章接着提到了Ribbon,这是一个客户端实现的负载均衡器,用于Spring Cloud中。Ribbon在发起REST请求时进行拦截,根据预设的负载均衡算法(如随机算法)选择服务器,并重构请求URI。文中还介绍了如何通过代码和配置文件两种方式自定义Ribbon的负载均衡策略。
45 3
|
6天前
|
负载均衡
Ribbon负载均衡策略
Ribbon负载均衡策略
|
6天前
|
负载均衡 应用服务中间件 nginx
服务器架构、分布式系统、负载均衡、微服务、高可用性
**分布式系统取代单体架构,以微服务实现高扩展性和灵活性。通过负载均衡技术增强性能,防止单点故障,结合冗余备份与故障切换保障高可用性,这种架构是支撑大规模在线业务的关键。**
48 3
|
6天前
|
Java Docker 微服务
|
6天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
30 1
|
6天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
22 2
|
6天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流