mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 04:39:00 +08:00
♻️ 重构代码。调整包结构
This commit is contained in:
1 parent
7bd84378b2
commit
55d98ad2e7
4 files changed
+273
No files matched your search
+111
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.transaction.ribbon;
|
||||
|
||||
|
||||
import com.codingapi.tx.aop.bean.TxTransactionLocal;
|
||||
import com.lorne.core.framework.utils.encode.MD5Util;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* created by foxdd 2017-12-05
|
||||
*
|
||||
* @author LCN
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class LcnLoadBalancerRule {
|
||||
|
||||
public Server proxy(List<Server> servers, Server server) {
|
||||
|
||||
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
|
||||
if (txTransactionLocal == null) {
|
||||
return server;
|
||||
}
|
||||
|
||||
try {
|
||||
log.debug("LCNBalanceProxy - > start");
|
||||
|
||||
String groupId = txTransactionLocal.getGroupId();
|
||||
|
||||
//取出组件的appName
|
||||
String appName = server.getMetaInfo().getAppName();
|
||||
|
||||
|
||||
String key = MD5Util.md5((groupId + "_" + appName).getBytes());
|
||||
|
||||
//如果只有一个可调用模块,则用当前的,且需要将数据记录到redis中
|
||||
if (servers.size() == 1) {
|
||||
putServer(key, txTransactionLocal, server);
|
||||
log.debug("LCNBalanceProxy -> only one server available");
|
||||
return server;
|
||||
}
|
||||
|
||||
Server oldServer = getServer(txTransactionLocal, servers, key);
|
||||
if (oldServer != null) {
|
||||
log.debug("LCNBalanceProxy - > load old server ");
|
||||
return server;
|
||||
}
|
||||
|
||||
putServer(key, txTransactionLocal, server);
|
||||
log.debug("LCNBalanceProxy - > load new server ");
|
||||
|
||||
return server;
|
||||
} finally {
|
||||
log.debug("LCNBalanceProxy - > end");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void putServer(String key, TxTransactionLocal txTransactionLocal, Server server) {
|
||||
String serviceName = server.getMetaInfo().getAppName();
|
||||
String address = server.getHostPort();
|
||||
|
||||
String md5 = MD5Util.md5((address + serviceName).getBytes());
|
||||
|
||||
log.debug("putServer->address->" + address + ",md5-->" + md5);
|
||||
|
||||
txTransactionLocal.putLoadBalance(key, md5);
|
||||
}
|
||||
|
||||
|
||||
private Server getServer(TxTransactionLocal txTransactionLocal, List<Server> servers, String key) {
|
||||
String val = txTransactionLocal.getLoadBalance(key);
|
||||
if (StringUtils.isEmpty(val)) {
|
||||
return null;
|
||||
}
|
||||
for (Server server : servers) {
|
||||
String serviceName = server.getMetaInfo().getAppName();
|
||||
String address = server.getHostPort();
|
||||
|
||||
String md5 = MD5Util.md5((address + serviceName).getBytes());
|
||||
|
||||
log.debug("getServer->address->" + address + ",md5-->" + md5);
|
||||
|
||||
if (val.equals(md5)) {
|
||||
return server;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.transaction.ribbon;
|
||||
|
||||
import com.netflix.loadbalancer.NoOpLoadBalancer;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* created by foxdd 2017-12-05
|
||||
*
|
||||
* @author LCN
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class LcnNoOpLoadBalancerProxy extends NoOpLoadBalancer {
|
||||
|
||||
private LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule();
|
||||
|
||||
public LcnNoOpLoadBalancerProxy() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server chooseServer(Object key) {
|
||||
log.debug("enter chooseServer method, key:" + key);
|
||||
|
||||
List<Server> serverList = new ArrayList<Server>();
|
||||
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.transaction.ribbon;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author LCN
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class LcnRibbonConfiguration {
|
||||
|
||||
/**
|
||||
* 为ribbon的loadbalancer做代理,相比于重写IRULE,重写loadbalancer更有利于用户自选LB算法,而且有默认LB算法可用
|
||||
*
|
||||
* @param config
|
||||
* @param serverList
|
||||
* @param serverListFilter
|
||||
* @param rule
|
||||
* @param ping
|
||||
* @param serverListUpdater
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
|
||||
ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
|
||||
IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
|
||||
return new LcnZoneAwareLoadBalancerProxy(config, rule, ping, serverList,
|
||||
serverListFilter, serverListUpdater);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.transaction.ribbon;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* created by foxdd 2017-12-05
|
||||
*
|
||||
* @author LCN
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class LcnZoneAwareLoadBalancerProxy extends ZoneAwareLoadBalancer<Server> {
|
||||
|
||||
LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule();
|
||||
|
||||
public LcnZoneAwareLoadBalancerProxy(IClientConfig clientConfig, IRule rule,
|
||||
IPing ping, ServerList<Server> serverList, ServerListFilter<Server> filter,
|
||||
ServerListUpdater serverListUpdater) {
|
||||
super(clientConfig, rule, ping, serverList, filter, serverListUpdater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server chooseServer(Object key) {
|
||||
log.info("enter chooseServer method, key:" + key);
|
||||
//利用Dynamic负载类中的方法,可以在运行时更新服务实例清单
|
||||
List<Server> serverList;
|
||||
//获取处理之后的serverlist
|
||||
serverList = super.getServerListImpl().getUpdatedListOfServers();
|
||||
//获取过滤之后的serverlist
|
||||
serverList = super.getFilter().getFilteredListOfServers(serverList);
|
||||
if (null == serverList || serverList.isEmpty()) {
|
||||
return super.chooseServer(key);
|
||||
}
|
||||
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user