Spring Cloud - Spring Cloud 负载均衡(Ribbon 负载均衡概述、Ribbon 使用)

一、Ribbon 负载均衡概述

  • Ribbon 是 Spring Cloud 提供的一种负载均衡解决方案,Ribbon 是 Netflix 发布的负载均衡器,Spring Cloud 对其进行了封装,形成了 Spring Cloud Ribbon

  • Ribbon 的使用同样需要结合 Eureka Server

  • 负载均衡算法:轮询、随机、加权轮询、加权随机


二、Ribbon 使用

1、具体实现
(1)配置工程
  • 创建子工程(Module),在 pom.xml 文件中配置相关依赖
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloudTest</artifactId>
        <groupId>com.my</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Ribbon</artifactId>

    <!-- Eureka Client -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    
</project>
(2)配置文件
  • 在 resources 目录下创建并配置 application.yaml 文件
yaml 复制代码
server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
(3)Config
  • 创建 MyConfig 配置类,将 RestTemplate 对象注入到 IoC 容器中,并实现负载均衡
java 复制代码
package com.my.config;

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;

@Configuration
public class Myconfig {

    @Bean

    // 添加负载均衡注解
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
(4)Controller
  • 创建 RibbonHandler 类
java 复制代码
package com.my.controller;

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;

@RestController
@RequestMapping("/ribbon")
public class RibbonHandler {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public String test() {
        return restTemplate.getForObject("http://ServerConsumer/consumer//zuulLoadBalancingTest", String.class);
    }
}
(5)启动类
  • 创建启动类
java 复制代码
package com.my;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class, args);
    }
}
2、测试
  1. 依次启动注册中心,服务提供者,服务消费者(第一个启动类),Ribbon

  2. 修改服务消费者的配置文件(application.yaml)

    • 将 server.port: 8020 修改为 server.port: 8021
  3. 再次启动一个服务消费者(使用第二个启动类)

  4. 使用 Postman 测试,多次访问,观察端口号的变化

相关推荐
Coder_Boy_12 分钟前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
Victory_orsh15 分钟前
AI雇佣人类,智能奴役肉体
后端
踢足球092920 分钟前
寒假打卡:2026-2-7
java·开发语言·javascript
闻哥23 分钟前
Kafka高吞吐量核心揭秘:四大技术架构深度解析
java·jvm·面试·kafka·rabbitmq·springboot
金牌归来发现妻女流落街头25 分钟前
【Springboot基础开发】
java·spring boot·后端
考琪41 分钟前
Nginx打印变量到log方法
java·运维·nginx
wangjialelele1 小时前
Linux中的进程管理
java·linux·服务器·c语言·c++·个人开发
历程里程碑1 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
callJJ1 小时前
Spring AI ImageModel 完全指南:用 OpenAI DALL-E 生成图像
大数据·人工智能·spring·openai·springai·图像模型
李日灐1 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树