From 55d98ad2e7e3d69edf0bb6b386cf982a79aeb036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Sun, 19 Aug 2018 00:23:15 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=82=E8=B0=83=E6=95=B4=E5=8C=85=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ribbon/LcnLoadBalancerRule.java | 111 ++++++++++++++++++ .../ribbon/LcnNoOpLoadBalancerProxy.java | 51 ++++++++ .../ribbon/LcnRibbonConfiguration.java | 52 ++++++++ .../ribbon/LcnZoneAwareLoadBalancerProxy.java | 59 ++++++++++ 4 files changed, 273 insertions(+) create mode 100755 pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnLoadBalancerRule.java create mode 100755 pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnNoOpLoadBalancerProxy.java create mode 100755 pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnRibbonConfiguration.java create mode 100755 pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnZoneAwareLoadBalancerProxy.java diff --git a/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnLoadBalancerRule.java b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnLoadBalancerRule.java new file mode 100755 index 000000000..ca3b329fc --- /dev/null +++ b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnLoadBalancerRule.java @@ -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 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 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; + } + +} diff --git a/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnNoOpLoadBalancerProxy.java b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnNoOpLoadBalancerProxy.java new file mode 100755 index 000000000..549a8e202 --- /dev/null +++ b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnNoOpLoadBalancerProxy.java @@ -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 serverList = new ArrayList(); + return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key)); + + } + +} diff --git a/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnRibbonConfiguration.java b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnRibbonConfiguration.java new file mode 100755 index 000000000..1445f8b73 --- /dev/null +++ b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnRibbonConfiguration.java @@ -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 serverList, ServerListFilter serverListFilter, + IRule rule, IPing ping, ServerListUpdater serverListUpdater) { + return new LcnZoneAwareLoadBalancerProxy(config, rule, ping, serverList, + serverListFilter, serverListUpdater); + } + + +} diff --git a/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnZoneAwareLoadBalancerProxy.java b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnZoneAwareLoadBalancerProxy.java new file mode 100755 index 000000000..2563d5270 --- /dev/null +++ b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/ribbon/LcnZoneAwareLoadBalancerProxy.java @@ -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 { + + LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule(); + + public LcnZoneAwareLoadBalancerProxy(IClientConfig clientConfig, IRule rule, + IPing ping, ServerList serverList, ServerListFilter 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 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)); + + } + +}