优化自定义负载均衡
This commit is contained in:
+20
-6
@@ -1,18 +1,32 @@
|
||||
package com.central.common.ribbon;
|
||||
|
||||
import com.alibaba.cloud.nacos.ribbon.NacosServer;
|
||||
import com.central.common.constant.ConfigConstants;
|
||||
import com.central.common.ribbon.config.RuleConfigure;
|
||||
import com.central.common.ribbon.rule.VersionIsolationRule;
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Ribbon扩展配置类
|
||||
* 自定义负载均衡配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/11/17 9:24
|
||||
* @date 2019/9/3
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@ConditionalOnProperty(value = ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED, havingValue = "true")
|
||||
@RibbonClients(defaultConfiguration = {RuleConfigure.class})
|
||||
@AutoConfigureBefore(RibbonClientConfiguration.class)
|
||||
public class LbIsolationAutoConfigure {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(NacosServer.class)
|
||||
@ConditionalOnMissingBean
|
||||
public IRule versionIsolationRule() {
|
||||
return new VersionIsolationRule();
|
||||
}
|
||||
}
|
||||
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package com.central.common.ribbon.config;
|
||||
|
||||
import com.central.common.ribbon.rule.CustomIsolationRule;
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/9/3
|
||||
*/
|
||||
public class RuleConfigure {
|
||||
@Bean
|
||||
public IRule isolationRule() {
|
||||
return new CustomIsolationRule();
|
||||
}
|
||||
}
|
||||
+11
-2
@@ -12,12 +12,21 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 自定义隔离随机规则
|
||||
* 根据实例版本号隔离规则
|
||||
* 详细用法:https://mp.weixin.qq.com/s/9XQ-SIbYsov3KBx9TGFN0g
|
||||
*
|
||||
* 实例获取规则顺序如下(不满足则走下一个规则):
|
||||
* 1. 相同版本号的实例
|
||||
* 2. 无版本号的实例
|
||||
* 3. 所有实例中轮询
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/9/3
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class CustomIsolationRule extends RoundRobinRule {
|
||||
public class VersionIsolationRule extends RoundRobinRule {
|
||||
private final static String KEY_DEFAULT = "default";
|
||||
/**
|
||||
* 优先根据版本号取实例
|
||||
Reference in New Issue
Block a user