Skip to content

Commit 8d9ddde

Browse files
committed
springcloud 负载均衡优化
1 parent 4920a3f commit 8d9ddde

5 files changed

Lines changed: 68 additions & 34 deletions

File tree

transaction-dubbo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<dependency>
7575
<groupId>com.alibaba</groupId>
7676
<artifactId>dubbo</artifactId>
77-
<version>2.5.3</version>
77+
<version>2.5.7</version>
7878
<exclusions>
7979
<exclusion>
8080
<groupId>org.springframework</groupId>
Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.codingapi.ribbon.loadbalancer;
22

3+
import com.codingapi.tx.aop.bean.TxTransactionLocal;
4+
import com.lorne.core.framework.utils.encode.MD5Util;
5+
import com.netflix.loadbalancer.Server;
6+
import org.apache.commons.lang.StringUtils;
37
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
59

6-
import com.codingapi.tx.Constants;
7-
import com.codingapi.tx.aop.bean.TxTransactionLocal;
8-
import com.netflix.loadbalancer.Server;
10+
import java.util.List;
911

1012
/**
1113
* created by foxdd 2017-12-05
@@ -14,31 +16,71 @@ public class LcnLoadBalancerRule {
1416

1517
private Logger logger = LoggerFactory.getLogger(LcnLoadBalancerRule.class);
1618

17-
public Server proxy(Server server){
18-
logger.info("LCNloadBalancer proxy -> map-size -> " + Constants.cacheModelInfo.size());
19-
logger.info("The selected server info, host:" + server.getHost() + ", port:" + server.getPort());
19+
public Server proxy(List<Server> servers,Server server){
20+
2021
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
21-
if(txTransactionLocal == null){
22+
if(txTransactionLocal==null){
2223
return server;
2324
}
24-
25-
String groupId = txTransactionLocal.getGroupId();
26-
27-
//取出组件的appName
28-
String appName = server.getMetaInfo().getAppName();
29-
30-
String key = groupId + "_" + appName;
31-
32-
Server cachedServer = (Server) Constants.cacheModelInfo.get(key);
33-
if(cachedServer == null){
34-
logger.info("The server of key:" + key + " has not been cached yet!");
35-
Constants.cacheModelInfo.put(key, server);
25+
26+
try{
27+
logger.info("LCNBalanceProxy - > start");
28+
29+
String groupId = txTransactionLocal.getGroupId();
30+
31+
//取出组件的appName
32+
String appName = server.getMetaInfo().getAppName();
33+
34+
35+
String key = MD5Util.md5((groupId + "_" + appName).getBytes());
36+
37+
Server oldServer =getServer(txTransactionLocal,servers,key);
38+
if(oldServer == null){
39+
logger.info("The server of key:" + key + " has not been cached yet!");
40+
return server;
41+
}
42+
43+
putServer(key, txTransactionLocal, server);
44+
logger.info("LCNBalanceProxy - > load new server ");
45+
3646
return server;
37-
} else{
38-
logger.info("The cached server info, host:" + cachedServer.getHost() + ", port:" + cachedServer.getPort());
39-
return cachedServer;
47+
}finally {
48+
logger.info("LCNBalanceProxy - > end");
49+
}
50+
}
51+
52+
53+
54+
private void putServer(String key,TxTransactionLocal txTransactionLocal,Server server){
55+
String serviceName = server.getMetaInfo().getAppName();
56+
String address = server.getHostPort();
57+
58+
String md5 = MD5Util.md5((address+serviceName).getBytes());
59+
60+
logger.info("putServer->address->"+address+",md5-->"+md5);
61+
62+
txTransactionLocal.putLoadBalance(key,md5);
63+
}
64+
65+
66+
private Server getServer(TxTransactionLocal txTransactionLocal, List<Server> servers, String key){
67+
String val = txTransactionLocal.getLoadBalance(key);
68+
if(StringUtils.isEmpty(val)){
69+
return null;
70+
}
71+
for(Server server:servers){
72+
String serviceName = server.getMetaInfo().getAppName();
73+
String address = server.getHostPort();
74+
75+
String md5 = MD5Util.md5((address+serviceName).getBytes());
76+
77+
logger.info("getServer->address->"+address+",md5-->"+md5);
78+
79+
if(val.equals(md5)){
80+
return server;
81+
}
4082
}
41-
83+
return null;
4284
}
4385

4486
}

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnNoOpLoadBalancerProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public LcnNoOpLoadBalancerProxy(){
2222
@Override
2323
public Server chooseServer(Object key){
2424
logger.info("enter chooseServer method, key:" + key);
25-
return lcnLoadBalancerRule.proxy(super.chooseServer(key));
25+
return lcnLoadBalancerRule.proxy(getAllServers(),super.chooseServer(key));
2626
}
2727

2828
}

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnZoneAwareLoadBalancerProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public LcnZoneAwareLoadBalancerProxy(IClientConfig clientConfig, IRule rule,
3030
@Override
3131
public Server chooseServer(Object key){
3232
logger.info("enter chooseServer method, key:" + key);
33-
return lcnLoadBalancerRule.proxy(super.chooseServer(key));
33+
return lcnLoadBalancerRule.proxy(getAllServers(),super.chooseServer(key));
3434
}
3535

3636
}

tx-client/src/main/java/com/codingapi/tx/Constants.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.codingapi.tx;
22

3-
import java.util.Map;
4-
import java.util.concurrent.ConcurrentHashMap;
5-
63
import com.codingapi.tx.model.TxServer;
74

85
/**
@@ -28,11 +25,6 @@ public class Constants {
2825
* 模块唯一标示
2926
*/
3027
public static String uniqueKey;
31-
32-
/**
33-
* 用于优化ribbon负载
34-
*/
35-
public static Map<String, Object> cacheModelInfo = new ConcurrentHashMap<>();
3628

3729

3830
}

0 commit comments

Comments
 (0)