Ribbon
基础Ribbon
引入项目改造
一般来说,提到负载均衡,大家一般很容易想到浏览器 -> NGINX
-> 反向代理多个 Tomcat
这样的架构图——业界管这种负载均衡模式叫”服务器端负载均衡”
,因为此种模式下,负载均衡算法是 NGINX
提供的,而 NGINX
部署在服务器端。
Ribbon
则是一个客户端侧
负载均衡组件——通俗地说,就是集成在客户端(服务消费者一侧),并提供负载均衡算法的一个组件。
Ribbon
是 Netflix
发布的负载均衡器,它可以帮我们控制 HTTP
和 TCP
客户端的行为。只需为 Ribbon
配置服务提供者地址列表,Ribbon
就可基于负载均衡算法计算出要请求的目标服务地址。
Ribbon
默认为我们提供了很多的负载均衡算法,例如轮询、随机、响应时间加权等——当然,为 Ribbon
自定义负载均衡算法也非常容易,只需实现IRule
接口即可。
TIPS Ribbon
的仓库地址(Github): https://github.com/Netflix/ribbon
在 Spring Cloud
中,当 Ribbon
与 Eureka
配合使用时,Ribbon
可自动从 Eureka Server
获取服务提供者地址列表,并基于负载均衡算法,选择其中一个服务提供者实例。下图展示了 Ribbon
与 Eureka
配合使用时的大致架构。
创建ribon服务,microservice-consumer-movie-ribbon
pom.xml
Maven
引入依赖:由于 spring-cloud-starter-netflix-eureka-client
已经包含 spring-cloud-starter-netfilx-ribbon
,故而无需额外添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | <?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" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.lzj1234</groupId> <artifactId>microservice-consumer-movie-ribbon</artifactId> <version> 1.0 -SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.0 . 7 .RELEASE</version> <relativePath/> </parent> <properties> <java.version> 1.8 </java.version> <!-- <maven.compiler.source> 8 </maven.compiler.source>--> <!-- <maven.compiler.target> 8 </maven.compiler.target>--> </properties> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version> 2.4 . 2 </version>--> </dependency> <!-- 引入H2数据库,一种内嵌的数据库,语法类似MySQL --> <!-- https: //mvnrepository.com/artifact/com.h2database/h2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version> 1.4 . 200 </version> <!-- <scope>test</scope>--> </dependency> <!-- https: //mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 18 </version> <scope>provided</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <version> 2.4 . 2 </version>--> <scope>test</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-clean-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version> 2.5 </version> </dependency> <!-- 新增依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 2.5 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> </build> </project> |
org.springframework.boot:spring-boot-maven-plugin:2.0.7.RELEASE:repackage failed: Unable to find mai
1 2 3 4 | <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> |
这是因为springboot默认没有启动类,需要手动添加启动类:springboot
创建启动类,App.java
package com.lzj1234;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
// 只需在RestTemplate
上添加LoadBalanced
注解,即可让RestTemplate
整合Ribbon
!
}
再次执行mvn install 命令,安装依赖包,就不会出错了
MovieController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.lzj1234; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RequestMapping ( "/movies" ) @RestController public class MovieController { @Autowired RestTemplate restTemplate; @GetMapping ( "/users/{id}" ) public User findById( @PathVariable Long id){ // 这里用到了RestTemplate的占位符能力 microservice-provider-user User user= this .restTemplate.getForObject( "http://microservice-provider-user/users/{id}" ,User. class ,id); return user; } } |
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | server: port: 8010 spring: application: name: microservice-consumer-movie-ribbon logging: level: root: INFO # 配置日志级别,让hibernate打印出执行的SQL参数 org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE # 新增配置 eureka: client: serviceUrl: defaultZone: http: //localhost:8080/eureka/ instance: prefer-ip-address: true |
mvn spring-boot:run
在项目中引用 Ribbon
来实现了客户端的负载均衡。Ribbon
是一个客户端侧
负载均衡组件——通俗地说,就是集成在客户端(服务消费者一侧),并提供负载均衡算法的一个组件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现