11package com .codingapi .ribbon .loadbalancer ;
22
3+ import com .alibaba .fastjson .JSONObject ;
34import com .codingapi .tx .aop .bean .TxTransactionLocal ;
45import com .lorne .core .framework .utils .encode .MD5Util ;
56import com .netflix .loadbalancer .Server ;
@@ -16,60 +17,63 @@ public class LcnLoadBalancerRule {
1617
1718 private Logger logger = LoggerFactory .getLogger (LcnLoadBalancerRule .class );
1819
19- public Server proxy (List <Server > qualifiedServers , Server server ){
20- logger . info ( "The selected server info, host:" + server . getHost () + ", port:" + server . getPort ());
20+ public Server proxy (List <Server > servers , Server server ){
21+
2122 TxTransactionLocal txTransactionLocal = TxTransactionLocal .current ();
22- if (txTransactionLocal == null ){
23+ if (txTransactionLocal == null ){
2324 return server ;
2425 }
25-
26- String groupId = txTransactionLocal .getGroupId ();
2726
28- //取出组件的appName
29- String appName = server .getMetaInfo ().getAppName ();
27+ try {
28+ logger .info ("LCNBalanceProxy - > start" );
29+
30+ String groupId = txTransactionLocal .getGroupId ();
31+
32+ //取出组件的appName
33+ String appName = server .getMetaInfo ().getAppName ();
34+
3035
31- logger .info ("The model named " + appName + " is going to be called, groupId:" + groupId );
36+ String key = MD5Util .md5 ((groupId + "_" + appName ).getBytes ());
37+
38+ Server oldServer =getServer (txTransactionLocal ,servers ,key );
39+ if (oldServer != null ){
40+ logger .info ("LCNBalanceProxy - > load old server " );
41+ return server ;
42+ }
3243
33- String key = MD5Util .md5 ((groupId + "_" + appName ).getBytes ());
34-
35- Server cachedServer = getInvoker (txTransactionLocal , qualifiedServers , key );
44+ putServer (key , txTransactionLocal , server );
45+ logger .info ("LCNBalanceProxy - > load new server " );
3646
37- if (cachedServer == null ){
38- logger .info ("The server of key:" + key + " has not been cached yet!" );
39- putInvoker (key , txTransactionLocal , server );
4047 return server ;
41- } else {
42- logger .info ("The cached server info, host:" + cachedServer .getHost () + ", port:" + cachedServer .getPort ());
43- return cachedServer ;
48+ }finally {
49+ logger .info ("LCNBalanceProxy - > end" );
4450 }
45-
4651 }
4752
4853
49- private void putInvoker (String key ,TxTransactionLocal txTransactionLocal ,Server server ){
50- String appName = server .getMetaInfo ().getAppName ();
5154
52- String address = server .getHost () + ":" + server .getPort ();
55+ private void putServer (String key ,TxTransactionLocal txTransactionLocal ,Server server ){
56+ String serviceName = server .getMetaInfo ().getAppName ();
57+ String address = server .getHostPort ();
5358
54- String md5 = MD5Util .md5 ((address +appName ).getBytes ());
59+ String md5 = MD5Util .md5 ((address +serviceName ).getBytes ());
5560
5661 logger .info ("putServer->address->" +address +",md5-->" +md5 );
5762
5863 txTransactionLocal .putLoadBalance (key ,md5 );
5964 }
6065
6166
62- private Server getInvoker (TxTransactionLocal txTransactionLocal , List <Server > servers ,String key ){
67+ private Server getServer (TxTransactionLocal txTransactionLocal , List <Server > servers , String key ){
6368 String val = txTransactionLocal .getLoadBalance (key );
6469 if (StringUtils .isEmpty (val )){
6570 return null ;
6671 }
6772 for (Server server :servers ){
68- String appName = server .getMetaInfo ().getAppName ();
69- //格式统一为host:port
70- String address = server .getHost () + ":" + server .getPort ();
73+ String serviceName = server .getMetaInfo ().getAppName ();
74+ String address = server .getHostPort ();
7175
72- String md5 = MD5Util .md5 ((address +appName ).getBytes ());
76+ String md5 = MD5Util .md5 ((address +serviceName ).getBytes ());
7377
7478 logger .info ("getServer->address->" +address +",md5-->" +md5 );
7579
0 commit comments