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实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.