SpringCloud 客户端负载均衡Ribbon Hoxton版本

2023-10-18 02:38

本文主要是介绍SpringCloud 客户端负载均衡Ribbon Hoxton版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring Cloud Ribbon简介:Spring Cloud Ribbon是Spring Cloud Netflix子项目的核心组件之一,是一个基于HTTP和TCP的客户端负载均衡器,它既可以通过在客户端中配置ribbonServerList服务列表以达到均衡负载的作用,也可以结合Eureka或Consul等,从注册中心中获取服务实例列表以完成负载均衡。

本文主要对Spring Cloud Ribbon的基本使用进行简单总结,其中SpringBoot使用的2.2.2.RELEASE版本,SpringCloud使用的Hoxton.SR1版本。这里将沿用SpringCloud 服务注册与发现Eureka Hoxton版本的eureka-server作为注册中心,eureka-client作为服务生产者,并通过Maven新建一个名为spring-cloud-netflix-ribbon的项目。

一、引入依赖

SpringBoot和SpringCloud依赖这里就不列出来了,还需引入以下依赖:

<!-- Spring Cloud Eureka Client 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Spring Cloud Ribbon 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- SpringRetry 重试框架依赖 -->
<dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

二、主启动类和RestTemplate配置类

package com.rtxtitanv;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.RibbonApplication* @description 主启动类* @date 2020/2/18 15:28*/
@EnableEurekaClient
@SpringBootApplication
public class RibbonApplication {public static void main(String[] args) {SpringApplication.run(RibbonApplication.class, args);}
}
package com.rtxtitanv.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.config.RestTemplateConfig* @description RestTemplate配置类* @date 2020/2/18 15:35*/
@Configuration
public class RestTemplateConfig {/*** 配置RestTemplate** @return RestTemplate*/@LoadBalanced@Bean(name = "restTemplate")public RestTemplate getRestTemplate() {HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();// 可以解决ribbon超时时间设置不生效问题httpRequestFactory.setReadTimeout(5000);httpRequestFactory.setConnectTimeout(5000);return new RestTemplate(httpRequestFactory);}
}

三、.编写配置文件

application.yml中进行如下配置:

server:port: ${PORT:9100}spring:application:name: ribboncloud:loadbalancer:retry:# 开启重试机制enabled: trueeureka:client:# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册register-with-eureka: true# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取fetch-registry: true# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开service-url:defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/instance:# 将ip地址注册到Eureka注册中心prefer-ip-address: true# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ipinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒lease-renewal-interval-in-seconds: 20# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒lease-expiration-duration-in-seconds: 60# ribbon指定客户端配置
eureka-client:ribbon:# 指定Ribbon负载均衡策略,ribbon自带七种负载均衡策略,RoundRobinRule:轮询,RandomRule:随机# RetryRule:重试,WeightedResponseTimeRule:响应时间加权,BestAvailableRule:最小并发请求# AvailabilityFilteringRule:可用过滤,ZoneAvoidanceRule:区域权重NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule# ribbon全局配置
ribbon:# 处理请求的超时时间,单位ms,默认1000ReadTimeout: 5000# 连接建立的超时时间,单位ms,默认1000ConnectTimeout: 5000# 切换实例的最大重试次数,不包括首次调用,默认0次MaxAutoRetriesNextServer: 3# 对当前实例的最大重试次数,不包括首次调用,默认1次MaxAutoRetries: 1# 是否对所有操作请求都进行重试,true:是,false:否,只针对get请求进行重试# 设置为true时,如果是put或post等写操作,如果服务器接口不能保证幂等性,会产生不好的结果,所以OkToRetryOnAllOperations设置为true需慎用# 默认情况下,get请求无论是连接异常还是读取异常,都会进行重试,非get请求,只有连接异常时,才会进行重试OkToRetryOnAllOperations: true# 对指定Http响应码进行重试retryableStatusCodes: 404,500,502eager-load:# 是否开启ribbon立即加载,true:开启,false:关闭,默认falseenabled: true# 指定需要立即加载的服务名,也就是你需要调用的服务,有多个则用逗号隔开clients: eureka-client

Ribbon参数配置通常有以下两种方式:

  • 全局配置:格式为ribbon.<key>=<value>
  • 客户端配置:格式为<client>.ribbon.<key>=<value>

其中<key>表示参数名称,<value>表示参数值,<client>表示客户端名称。全局配置可以作为默认值设置,当指定客户端配置了相应key时将会覆盖全局配置内容。

禁用Eureka配置

这里使用了Eureka注册中心,如果想要禁用Eureka注册中心,需配置:

ribbon: eureka:# 是否使用Eureka,true:使用,false:禁用,默认为true,禁用后需手动配置服务列表 enabled: false
eureka-client:ribbon:# 禁用Eureka后手动配置服务列表listOfServers: localhost:9001,localhost:9002,localhost:9003

使用参数配置自定义Ribbon客户端

从版本1.2.0开始,Spring Cloud Netflix支持使用参数与Ribbon文档兼容来自定义Ribbon客户端,对应的参数如下,应以<client>.ribbon.为前缀:

<client>: ribbon:# 配置负载均衡器NFLoadBalancerClassName: ILoadBalancer(负载均衡器接口)实现类# 配置Ribbon负载均衡策略NFLoadBalancerRuleClassName: IRule(负载均衡策略接口)实现类# 配置Ribbon实例检查策略NFLoadBalancerPingClassName: IPing(实例检查策略接口)实现类# 配置服务列表维护机制NIWSServerListClassName: ServerList(服务列表维护接口)实现类# 配置服务列表过滤机制NIWSServerListFilterClassName: ServerListFilter(服务列表过滤接口)实现类

四、Ribbon使用测试

Controller层:

package com.rtxtitanv.controller;import com.rtxtitanv.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.controller.RibbonController* @description RibbonController* @date 2020/2/18 15:35*/
@RestController
public class RibbonController {@Autowiredprivate RibbonService ribbonService;@GetMapping("/ribbon")public String ribbon() {return ribbonService.ribbon();}
}

Service层:

package com.rtxtitanv.service;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.service.RibbonService* @description RibbonService* @date 2020/2/18 15:48*/
public interface RibbonService {/*** ribbon负载均衡测试** @return 调用eureka-client返回的结果*/String ribbon();
}
package com.rtxtitanv.service.impl;import com.rtxtitanv.service.RibbonService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.service.impl.RibbonServiceImpl* @description RibbonService实现类* @date 2020/2/18 15:48*/
@Service
public class RibbonServiceImpl implements RibbonService {@Resource(name = "restTemplate")private RestTemplate restTemplate;@Overridepublic String ribbon() {return restTemplate.getForObject("http://eureka-client/home", String.class);}
}

IDEA启动eureka-server集群,eureka-client集群和ribbon,其中eureka-client共3个节点,访问eureka-server中的一个节点,注册信息见下图:
注册中心中的服务注册列表
不断访问http://localhost:9100/ribbon,这里负载均衡策略设置的随机策略,根据下面动图中的测试过程和结果,说明Ribbon成功实现负载均衡。随机负载均衡策略结果
测试Ribbon的超时重试,根据下面动图中的测试过程和结果,说明Ribbon成功实现超时重试。
超时重试测试结果

代码示例

  • Github:https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-ribbon
  • Gitee:https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-ribbon

这篇关于SpringCloud 客户端负载均衡Ribbon Hoxton版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/229630

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2