SpringCloudAlibabaNacos负载均衡策略Ribbon配置
ribbon有7种负载均衡策略可供选择:
1、随机策略——RandomRule
2、轮询策略——RoundRobinRule
注:Ribbon默认策略
3、重试策略——RetryRule
4、最低并发策略——BestAvailableRule
5、可⽤过滤策略——AvailabilityFilteringRule
过滤掉那些因为⼀直连接失败的被标记为circuit tripped的后端rver,并过滤掉那些⾼并发的的后端rver(active connections 超过配置的阈值)
性能仅次于最低并发策略。
6、响应时间加权策略——WeightedResponTimeRule
每隔30秒计算⼀次服务器响应时间,以响应时间作为权重,响应时间越短的服务器被选中的概率越⼤。
7、区域权衡策略——ZoneAvoidanceRule
Ribbon的负载均衡策略使⽤建议
⼀般情况下,推荐使⽤最低并发策略,这个性能⽐默认的轮询策略⾼很多。
策略类 命名描述
RandomRule随机策略随机选择rver
郑人买履翻译RoundRobinRule轮询策略按照顺序选择rver(ribbon默认策略)
RetryRule重试策略在⼀个配置时间段内,当选择rver不成功,则⼀直尝试选择⼀个可⽤的rver
BestAvailableRule最低并发策
略
逐个考察rver,如果rver断路器打开,则忽略,再选择其中并发链接最低的rver
AvailabilityFilteringRule可⽤过滤策
粤语翻译
略
过滤掉⼀直失败并被标记为circuit tripped的rver,过滤掉那些⾼并发链接的rver(active connections超过配置的阈值)
ResponTimeWeightedRule响应时间加
权重策略根据rver的响应时间分配权重,响应时间越长,权重越低,被选择到的概率也就越低。响应时间越短,权重越⾼,被选中的概率越⾼,这个策略很贴切,综合了各种因素,⽐如:⽹络,磁盘,io等,都直接影响响应时间
ZoneAvoidanceRule区域权重策死机英文
略综合判断rver所在区域的性能,和rver的可⽤性,轮询选择rver并且判断⼀个AWS Zone的运⾏性能是否可⽤,剔除不可⽤的Zone中的所有rver
配置类配置
package com.wsm.ribbon;
flix.loadbalancer.IRule;
flix.loadbalancer.RandomRule;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
public class RibbonRandomRuleConfig {
//⽅法名⼀定要中 iRule
@Bean
public IRule iRule(){
return new RandomRule();
}
}
package ller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.swing.*;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/add")
public String add(){
System.out.println("aaaaaaaaaaaaa");
/
/ String msg = ForObject("localhost:8011/stock/reduct", String.class); String msg = ForObject("stock-rvice/stock/reduct", String.class);
return "hello world "+msg;
}
}
package der;
import com.wsm.ribbon.RibbonRandomRuleConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.flix.ribbon.RibbonClient;
import org.flix.ribbon.RibbonClients;
import t.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//@EnableDiscoveryClient ⽼版本要加,新版本可以不加
@RibbonClients(value = {
@RibbonClient(name = "stock-rvice",configuration = RibbonRandomRuleConfig.class) })
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
@Bean
@LoadBalanced //启⽤负载均衡
public RestTemplate restTemplate(RestTemplateBuilder builder){
RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
rver:
port: 8030
绩效考核管理系统#应⽤名称(nacos 会将该名称当作服务名称)
spring:
application:
name: order-ribbon-rvice
cloud:
nacos:
# rver-addr: 127.0.0.1:8848
rver-addr: 192.168.133.128:8847 #集群 nginx 负载均衡访问 nacos
discovery:
urname: nacos
password: nacos
namespace: public
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudalibaba</artifactId>
<groupId>com.wsm.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-ribbon</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <version>2.5.5</version>-->
</dependency>
<!-- nacos 服务注册发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
成长的烦恼高清下载</dependency>
</dependencies>
</project>
配置⽂件配置
rver:
port: 8030
#应⽤名称(nacos 会将该名称当作服务名称)
hospitalizedspring:
application:
name: order-ribbon-rvice
cloud:
nacos:
混蛋英语# rver-addr: 127.0.0.1:8848
rver-addr: 192.168.133.128:8847 #集群 nginx 负载均衡访问 nacos
discovery:
urname: nacos
password: nacos
namespace: public
stock-rvice:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #指定使⽤Nacos提供的负载均衡策略(优先调⽤同⼀集群的实例,基于随机&权重)
package der;
import com.wsm.ribbon.RibbonRandomRuleConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.flix.ribbon.RibbonClient;
import org.flix.ribbon.RibbonClients;
import t.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//@EnableDiscoveryClient ⽼版本要加,新版本可以不加
//@RibbonClients(value = {
// @RibbonClient(name = "stock-rvice",configuration = RibbonRandomRuleConfig.class)
//})
public class OrderApplication {
public static void main(String[] args) {employer
SpringApplication.run(OrderApplication.class,args);
}
@Bean
@LoadBalanced //启⽤负载均衡
public RestTemplate restTemplate(RestTemplateBuilder builder){
日语 加油RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
⾃定义负载均衡策略
package com.wsm.ribbon.rule;
fig.IClientConfig;
flix.loadbalancer.AbstractLoadBalancerRule;
flix.loadbalancer.ILoadBalancer;
flix.loadbalancer.Server;
import java.util.List;
import urrent.ThreadLocalRandom;
public class CustomRule extends AbstractLoadBalancerRule {
@Override
public Server choo(Object o) {
ILoadBalancer loadBalancer = LoadBalancer();
//获得当前请求的服务实例
List<Server> reachableServers = ReachableServers();
int random = ThreadLocalRandom.current().nextInt(reachableServers.size());
Server rver = (random);
// if(rver.isAlive()){
// return rver;
// }
return rver;
}
@Override
public void initWithNiwsConfig(IClientConfig iClientConfig) {
}
}
rver:
port: 8030
#应⽤名称(nacos 会将该名称当作服务名称)
spring:
application:
name: order-ribbon-rvice
cloud:
nacos:
# rver-addr: 127.0.0.1:8848
rver-addr: 192.168.133.128:8847 #集群 nginx 负载均衡访问 nacos
discovery:
urname: nacos
password: nacos
namespace: public
stock-rvice:
ribbon:
# NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #指定使⽤Nacos提供的负载均衡策略(优先调⽤同⼀集群的实例,基于随机&权重) NFLoadBalancerRuleClassName: com.wsm.ribbon.rule.CustomRule
rver:
port: 8030
#应⽤名称(nacos 会将该名称当作服务名称)
spring:
application:
name: order-ribbon-rvice
cloud:
nacos:
# rver-addr: 127.0.0.1:8848
rver-addr: 192.168.133.128:8847 #集群 nginx 负载均衡访问 nacos
discovery:
urname: nacos
password: nacos
namespace: public
stock-rvice:
ribbon:
swaying
# NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #指定使⽤Nacos提供的负载均衡策略(优先调⽤同⼀集群的实例,基于随机&权重) NFLoadBalancerRuleClassName: com.wsm.ribbon.rule.CustomRule
ribbon:
eager-load:
enabled: true #开启ribbon饥饿加载
clients: stock-rvice #配置stock-rvice使⽤ribbon饥饿加载,多个使⽤逗号分隔