在前面的文章 Eureka详解与实战、Eureka Client应用、RestTemplate详解及其负载均衡几篇文章中,已经介绍了Spring Cloud基本应用,本文将从讲解在进程层面的负载均衡,在Spring Cloud中如何使用Ribbon做系统应用层面的负载均衡使用。
一、什么是Ribbon
Ribbon是netflix 公司开源的基于客户端的负载均衡组件,是Spring Cloud大家庭中非常重要的一个模块;Ribbon应该也是整个大家庭中相对而言比较复杂的模块,直接影响到服务调度的质量和性能。全面掌握Ribbon可以帮助我们了解在分布式微服务集群工作模式下,服务调度应该考虑到的每个环节。
Ribbon内部提供了一个接口叫做ILoadBalance的接口代表负载均衡器的操作,这个接口包含添加服务器操作、选择服务器操作、获取所有的服务器列表、获取可用服务器列表等功能。
下图展示Ribbon的架构图:
二、Spring Cloud中Ribbon应用
Ribbon使用步骤如下
- 在Eureka Client中添加bom.xml 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
2.RestTemplate配置类添加@LoadBalanced注解
@Configuration public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
3.启动类添加注解@EnableEurekaClient和@RibbonClient(name = “Eureka中spring.application.name”)
@SpringBootApplication @EnableEurekaClient @RibbonClient(name="GoyeerRibbonDemo") public class GoyeerCloudRibbonAppliction { public static void main(String[] args){ SpringApplication.run(GoyeerCloudRibbonAppliction.class); } }
4.application.yml配置内容
server: port: 8081 eureka: client: service-url: defaultZone: http://server30000:30000/eureka/,http://server30001:30001/eureka/,http://server30002:30002/eureka/ register-with-eureka: false spring: application: name: GoyeerRibbon profiles: active: ribbonClient
5.编写控制器
@Autowired private RestTemplateConfig restTemplateConfig; @RequestMapping(value = "/") public String index(){ String url="http://GOYEERBOM"; //注册Eureka中的服务名 RestTemplate restTemplate=restTemplateConfig.getRestTemplate(); String str=restTemplate.getForObject(url,String.class); return str; }
6.服务端效果
- Eureka服务器端
- Ribbon调用后结构
三、Ribbon负载均衡策略设置
3.1 全局策略设置
/** * @Author:Goyeer * @Description:Ribbon 全局的负载均衡策略配置类 * @CreateDate:2023-07-11 */ @Configuration public class RibbonGlobalLoadBalancingConfiguration { /** * 随机规则 */ @Bean public IRule ribbonRule() { return new RandomRule(); } } • 16
3.2 基于注解的针对单个服务的 Ribbon 负载均衡策略
3.2.1 注解方式
/** * @Author:Goyeer * @Description:Ribbon 随机负载均衡策略配置类哪个服务引用就作用在那个服务上面在启动类上方使用 @RibbonClient 引用 * @CreateDate:2023-07-11 */ @Configuration @AvoidScan public class RibbonRandomLoadBalancingConfiguration { @Resource IClientConfig clientConfig; @Bean public IRule ribbonRule(IClientConfig clientConfig) { return new RandomRule(); }
3.2.2 IClientConfig针对客户端的配置管理器
/** 配置针对单个服务的 Ribbon 负载均衡策略 **/ @RibbonClient( name = "goyeer-balance-ribbon", configuration = RibbonRandomLoadBalancingConfiguration.class ) /** 此处配置根据标识 @AvoidScan 过滤掉需要单独配置的 Ribbon 负载均衡策略,不然就会作用于全局,启动就会报错 */ @ComponentScan( excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = AvoidScan.class)
3.3 配置文件方式
### 针对单个服务的 Ribbon 配置 goyeer-balance-ribbon: ribbon: # 基于配置文件形式的 针对单个服务的 Ribbon 负载均衡策略 NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule