|
| 1 | +package com.codingapi.tx.dubbo.balance; |
| 2 | + |
| 3 | +import com.alibaba.dubbo.rpc.Invoker; |
| 4 | +import com.alibaba.dubbo.rpc.RpcException; |
| 5 | +import com.codingapi.tx.aop.bean.TxTransactionLocal; |
| 6 | +import com.lorne.core.framework.utils.encode.MD5Util; |
| 7 | +import org.apache.commons.lang.StringUtils; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * create by lorne on 2017/11/29 |
| 15 | + */ |
| 16 | + |
| 17 | +public class LCNBalanceProxy { |
| 18 | + |
| 19 | + private Logger logger = LoggerFactory.getLogger(LCNBalanceProxy.class); |
| 20 | + |
| 21 | + public <T> Invoker<T> proxy(List<Invoker<T>> invokers,Invoker<T> invoker) throws RpcException { |
| 22 | + TxTransactionLocal txTransactionLocal = TxTransactionLocal.current(); |
| 23 | + if(txTransactionLocal==null){ |
| 24 | + return invoker; |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + logger.info("LCNBalanceProxy - > start"); |
| 29 | + |
| 30 | + String groupId = txTransactionLocal.getGroupId(); |
| 31 | + |
| 32 | + String uniqueKey = invoker.getUrl().getServiceInterface(); |
| 33 | + |
| 34 | + logger.info("LCNBalanceProxy - > uniqueKey - >" + uniqueKey); |
| 35 | + |
| 36 | + String key = MD5Util.md5((groupId + "_" + uniqueKey).getBytes()); |
| 37 | + |
| 38 | + //请求tm获取模块信息 |
| 39 | + Invoker old = getInvoker(txTransactionLocal, invokers, key); |
| 40 | + |
| 41 | + if (old != null) { |
| 42 | + logger.info("LCNBalanceProxy - > load old invoker "); |
| 43 | + |
| 44 | + return old; |
| 45 | + } |
| 46 | + putInvoker(key, txTransactionLocal, invoker); |
| 47 | + |
| 48 | + logger.info("LCNBalanceProxy - > load new invoker "); |
| 49 | + |
| 50 | + return invoker; |
| 51 | + }finally { |
| 52 | + logger.info("LCNBalanceProxy - > end"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + private void putInvoker(String key,TxTransactionLocal txTransactionLocal,Invoker invoker){ |
| 58 | + String serviceName = invoker.getUrl().getServiceInterface(); |
| 59 | + String address = invoker.getUrl().getAddress(); |
| 60 | + |
| 61 | + String md5 = MD5Util.md5((address+serviceName).getBytes()); |
| 62 | + |
| 63 | + logger.info("putInvoker->address->"+address+",md5-->"+md5); |
| 64 | + |
| 65 | + txTransactionLocal.putLoadBalance(key,md5); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + private <T> Invoker<T> getInvoker(TxTransactionLocal txTransactionLocal,List<Invoker<T>> invokers,String key){ |
| 70 | + String val = txTransactionLocal.getLoadBalance(key); |
| 71 | + if(StringUtils.isEmpty(val)){ |
| 72 | + return null; |
| 73 | + } |
| 74 | + for(Invoker<T> invoker:invokers){ |
| 75 | + String serviceName = invoker.getUrl().getServiceInterface(); |
| 76 | + String address = invoker.getUrl().getAddress(); |
| 77 | + |
| 78 | + String md5 = MD5Util.md5((address+serviceName).getBytes()); |
| 79 | + |
| 80 | + logger.info("getInvoker->address->"+address+",md5-->"+md5); |
| 81 | + |
| 82 | + if(val.equals(md5)){ |
| 83 | + return invoker; |
| 84 | + } |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | +} |
0 commit comments