一、ribbon的作用和含义

Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP客⼾端的⾏为。为Ribbon,配置服务提供者的地址列表后,Ribbon就可基于某种负载均衡算法,⾃动地帮助服务消费者去请求。Ribbon默认为我们提供了很多的负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现⾃定义的负载均衡算法。在SpringCloud中,当Ribbon与Eureka配 合使⽤时,Ribbon可⾃动从EurekaServer获取服务提供者的地址列表,并基于负载均衡算法,请求其中⼀个服务提供者的实例 (为了服务的可靠性,⼀个微服务可能部署多个实例)。

二、使用步骤

1.引入jar包

  1. <!-- 引入eureka客户端,eureka内部集成了ribbon -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>

2.完整pom文件内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>com.springcloud</artifactId>
  7. <groupId>com.sc2020</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.example</groupId>
  11. <artifactId>consumer-eureka-client81</artifactId>
  12. <version>1.0.0-SNAPSHOT</version>
  13. <name>consumer-client81</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>com.sc2020</groupId>
  21. <artifactId>cloud-payment8001</artifactId>
  22. <version>1.0-SNAPSHOT</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.sc2020</groupId>
  26. <artifactId>cloud-api-commons</artifactId>
  27. <version>1.0-SNAPSHOT</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-actuator</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.mybatis.spring.boot</groupId>
  39. <artifactId>mybatis-spring-boot-starter</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>com.alibaba</groupId>
  43. <artifactId>druid-spring-boot-starter</artifactId>
  44. <version>1.1.10</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>mysql</groupId>
  48. <artifactId>mysql-connector-java</artifactId>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-jdbc</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-devtools</artifactId>
  62. <scope>runtime</scope>
  63. <optional>true</optional>
  64. </dependency>
  65. <!-- 引入eureka客户端 -->
  66. <dependency>
  67. <groupId>org.springframework.cloud</groupId>
  68. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  69. </dependency>
  70. </dependencies>
  71. </project>

3. application配置文件

  1. server:
  2. port: 81
  3. eureka:
  4. client:
  5. register-with-eureka: true #是否将自己注册到注册中心,集群必须设置为true配合ribbon
  6. fetch-registry: true #是否从服务端抓取已有的注册信息
  7. service-url:
  8. defaultZone: http://eureka7001.com:7001/eureka #服务注册中心路径,一般为localhost
  9. instance:
  10. instance-id: consumer-client81
  11. prefer-ip-address: true
  12. spring:
  13. datasource:
  14. type: com.alibaba.druid.pool.DruidDataSource
  15. driver-class-name: org.gjt.mm.mysql.Driver
  16. url: jdbc:mysql://101.34.49.127:3306/cloud?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false
  17. username: 数据库名称
  18. password: 数据库密码
  19. application:
  20. name: consumer-client
  21. mybatis:
  22. mapper-locations: classpath:mapper/*.xml

4.main启动类

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. //name表示使用负载均衡的服务名称,configuration是指定负载均衡策略类
  4. @RibbonClient(name = "cloud-eureka-service",configuration = MyselfRule.class)
  5. public class ConsumerEurekaClient81Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ConsumerEurekaClient81Application.class, args);
  8. }
  9. }

5.负载均衡配置类MyselfRule

  1. package com.example.consumerclient81.springcloud.config;
  2. import com.netflix.loadbalancer.*;
  3. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. * @description:
  9. * @author: xiaYZ
  10. * @createDate: 2021/11/16
  11. * @version: 1.0
  12. */
  13. @Configuration
  14. public class MyselfRule {
  15. //设置RestTemplate
  16. @Bean
  17. //调用微服务项目,并负载均衡
  18. @LoadBalanced
  19. public RestTemplate getRestTemplate(){
  20. return new RestTemplate();
  21. }
  22. /**
  23. * description: 随机轮训策略
  24. * version: 1.0
  25. * date: 2021/11/30 13:17
  26. * author: xiaYZ
  27. * iteration: 迭代说明
  28. * @param
  29. * @return
  30. */
  31. @Bean
  32. public IRule myRule(){
  33. return new RandomRule();
  34. }
  35. /**
  36. * description: 轮询策略,Ribbon默认策略
  37. * version: 1.0
  38. * date: 2021/11/30 13:23
  39. * author: xiaYZ
  40. * iteration: 迭代说明
  41. * @param
  42. * @return
  43. */
  44. //@Bean
  45. public IRule myRule2(){
  46. return new RoundRobinRule();
  47. }
  48. /**
  49. * description: 重试策略
  50. * version: 1.0
  51. * date: 2021/11/30 13:25
  52. * author: xiaYZ
  53. * iteration: 迭代说明
  54. * @param
  55. * @return
  56. */
  57. //@Bean
  58. public IRule myRule3(){
  59. return new RetryRule();
  60. }
  61. /**
  62. * description: 最低并发策略
  63. * version: 1.0
  64. * date: 2021/11/30 13:26
  65. * author: xiaYZ
  66. * iteration: 迭代说明
  67. * @param
  68. * @return
  69. */
  70. //@Bean
  71. public IRule myRule4(){
  72. return new BestAvailableRule();
  73. }
  74. /**
  75. * description: 可用过滤策略
  76. * version: 1.0
  77. * date: 2021/11/30 13:26
  78. * author: xiaYZ
  79. * iteration: 迭代说明
  80. * @param
  81. * @return
  82. */
  83. //@Bean
  84. public IRule myRule5(){
  85. return new AvailabilityFilteringRule();
  86. }
  87. /**
  88. * description: 响应时间加权策略
  89. * version: 1.0
  90. * date: 2021/11/30 13:27
  91. * author: xiaYZ
  92. * iteration: 迭代说明
  93. * @param
  94. * @return
  95. */
  96. //@Bean
  97. public IRule myRule6(){
  98. return new WeightedResponseTimeRule();
  99. }
  100. /**
  101. * description: 区域权衡策略
  102. * version: 1.0
  103. * date: 2021/11/30 13:27
  104. * author: xiaYZ
  105. * iteration: 迭代说明
  106. * @param
  107. * @return
  108. */
  109. //@Bean
  110. public IRule myRule7(){
  111. return new ZoneAvoidanceRule();
  112. }
  113. }

6.Controller控制层代码

  1. @Resource
  2. RestTemplate restTemplate;
  3. public static final String PAYMENT_URL = "http://cloud-eureka-client";
  4. @GetMapping(value = "getMessage")
  5. public String getMessage(){
  6. //调用cloud-eureka-client服务中方法
  7. String forObject = restTemplate.getForObject( PAYMENT_URL +"/paymentController/getPortMessage", String.class);
  8. return forObject;
  9. }

7.效果展示

4.ribbon负载均衡策略 - 图1

4.ribbon负载均衡策略 - 图2

4.ribbon负载均衡策略 - 图3

项目地址: https://gitee.com/xyz1041221997/springcloud.git