11package 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 ;
37import org .slf4j .Logger ;
48import 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}
0 commit comments