diff --git a/transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnLoadBalancerRule.java b/transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnLoadBalancerRule.java index 93a2a4838..85fff4d18 100644 --- a/transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnLoadBalancerRule.java +++ b/transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnLoadBalancerRule.java @@ -1,94 +1,94 @@ -package com.codingapi.ribbon.loadbalancer; - - -import com.codingapi.tx.aop.bean.TxTransactionLocal; -import com.lorne.core.framework.utils.encode.MD5Util; -import com.netflix.loadbalancer.Server; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * created by foxdd 2017-12-05 - */ -public class LcnLoadBalancerRule { - - private Logger logger = LoggerFactory.getLogger(LcnLoadBalancerRule.class); - - public Server proxy(List servers,Server server){ - - TxTransactionLocal txTransactionLocal = TxTransactionLocal.current(); - if(txTransactionLocal==null){ - return server; - } - - try{ - logger.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); - logger.debug("LCNBalanceProxy -> only one server available"); - return server; - } - - Server oldServer =getServer(txTransactionLocal,servers,key); - if(oldServer != null){ - logger.debug("LCNBalanceProxy - > load old server "); - return server; - } - - putServer(key, txTransactionLocal, server); - logger.debug("LCNBalanceProxy - > load new server "); - - return server; - }finally { - logger.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()); - - logger.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()); - - logger.debug("getServer->address->"+address+",md5-->"+md5); - - if(val.equals(md5)){ - return server; - } - } - return null; - } - -} +package com.codingapi.ribbon.loadbalancer; + + +import com.codingapi.tx.aop.bean.TxTransactionLocal; +import com.lorne.core.framework.utils.encode.MD5Util; +import com.netflix.loadbalancer.Server; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * created by foxdd 2017-12-05 + */ +public class LcnLoadBalancerRule { + + private Logger logger = LoggerFactory.getLogger(LcnLoadBalancerRule.class); + + public Server proxy(List servers,Server server){ + + TxTransactionLocal txTransactionLocal = TxTransactionLocal.current(); + if(txTransactionLocal==null){ + return server; + } + + try{ + logger.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); + logger.debug("LCNBalanceProxy -> only one server available"); + return server; + } + + Server oldServer =getServer(txTransactionLocal,servers,key); + if(oldServer != null){ + logger.debug("LCNBalanceProxy - > load old server "); + return oldServer; + } + + putServer(key, txTransactionLocal, server); + logger.debug("LCNBalanceProxy - > load new server "); + + return server; + }finally { + logger.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()); + + logger.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()); + + logger.debug("getServer->address->"+address+",md5-->"+md5); + + if(val.equals(md5)){ + return server; + } + } + return null; + } + +} diff --git a/tx-client/src/main/java/com/codingapi/tx/framework/utils/serializer/ProtostuffSerializer.java b/tx-client/src/main/java/com/codingapi/tx/framework/utils/serializer/ProtostuffSerializer.java index 54c8446b6..e45f46aa3 100644 --- a/tx-client/src/main/java/com/codingapi/tx/framework/utils/serializer/ProtostuffSerializer.java +++ b/tx-client/src/main/java/com/codingapi/tx/framework/utils/serializer/ProtostuffSerializer.java @@ -30,39 +30,37 @@ /** * @author lorne 2017/11/11 */ +@SuppressWarnings("unchecked") public class ProtostuffSerializer implements ISerializer { - private static final SchemaCache cachedSchema = SchemaCache.getInstance(); - private static final Objenesis objenesis = new ObjenesisStd(true); + + private static final SchemaCache SCHEMA_CACHE = SchemaCache.getInstance(); + private static final Objenesis OBJENESIS = new ObjenesisStd(true); private static Schema getSchema(Class cls) { - return (Schema) cachedSchema.get(cls); + return (Schema) SCHEMA_CACHE.get(cls); } - @Override public byte[] serialize(Object obj) throws SerializerException { Class cls = obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - try { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){ Schema schema = getSchema(cls); ProtostuffIOUtil.writeTo(outputStream, obj, schema, buffer); + return outputStream.toByteArray(); } catch (Exception e) { throw new SerializerException(e.getMessage(), e); } finally { buffer.clear(); } - return outputStream.toByteArray(); } @Override - public T deSerialize(byte[] param, Class clazz) throws SerializerException { + public T deSerialize(byte[] param, Class cls) throws SerializerException { T object; - try { - ByteArrayInputStream inputStream = new ByteArrayInputStream(param); - Class cls = clazz; - object = objenesis.newInstance((Class) cls); + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(param)) { + object = OBJENESIS.newInstance(cls); Schema schema = getSchema(cls); ProtostuffIOUtil.mergeFrom(inputStream, object, schema); return object;