@@ -72,16 +72,15 @@ public class ServiceAddressRepository {
7272 private final String protocol ;
7373
7474
75-
7675 public ServiceAddressRepository (List <String > addresses , String clientId , Extensions extensions ,
77- ServiceKey remoteCluster ) {
76+ ServiceKey remoteCluster ) {
7877 this (addresses , clientId , extensions , remoteCluster , null , null , null );
7978 this .routers .add (ServiceRouterConfig .DEFAULT_ROUTER_METADATA );
8079 this .routers .add (ServiceRouterConfig .DEFAULT_ROUTER_NEARBY );
8180 }
8281
8382 public ServiceAddressRepository (List <String > addresses , String clientId , Extensions extensions ,
84- ServiceKey remoteCluster , List <String > routers , String lbPolicy , String protocol ) {
83+ ServiceKey remoteCluster , List <String > routers , String lbPolicy , String protocol ) {
8584 // to ip addresses.
8685 this .nodes = new ArrayList <>();
8786 if (CollectionUtils .isNotEmpty (addresses )) {
@@ -138,21 +137,30 @@ public Node getServiceAddressNode() throws PolarisException {
138137 .getPlugin (PluginTypes .LOAD_BALANCER .getBaseType (), lbPolicy );
139138 if (loadBalancer == null ) {
140139 // 降级为轮训
141- if (curIndex >= nodes .size ()) {
142- curIndex = 0 ;
140+ Node node = nodes .get (Math .abs (curIndex % nodes .size ()));
141+ curIndex = (curIndex + 1 ) % Integer .MAX_VALUE ;
142+ if (LOG .isDebugEnabled ()) {
143+ LOG .warn ("Can't find load balancer {}, use round robin instead" , lbPolicy );
144+ LOG .debug ("success to get instance for service {}, instance is {}" , remoteCluster ,
145+ node .getHostPort ());
143146 }
144- return nodes . get ( curIndex ++) ;
147+ return node ;
145148 }
146149 Criteria criteria = new Criteria ();
147150 criteria .setHashKey (this .clientId );
148151 Instance instance = BaseFlow .processLoadBalance (loadBalancer , criteria , remoteAddresses ,
149152 extensions .getWeightAdjusters ());
150- return new Node (IPAddressUtils .getIpCompatible (instance .getHost ()), instance .getPort ());
153+ Node node = new Node (IPAddressUtils .getIpCompatible (instance .getHost ()), instance .getPort ());
154+ if (LOG .isDebugEnabled ()) {
155+ LOG .debug ("success to get instance for service {}, instance is {}" , remoteCluster , node .getHostPort ());
156+ }
157+ return node ;
151158 }
152159 Instance instance = getDiscoverInstance ();
153160 String host = IPAddressUtils .getIpCompatible (instance .getHost ());
154161 if (LOG .isDebugEnabled ()) {
155- LOG .debug ("success to get instance for service {}, instance is {}:{}" , remoteCluster , host , instance .getPort ());
162+ LOG .debug ("success to get instance for service {}, instance is {}:{}" , remoteCluster , host ,
163+ instance .getPort ());
156164 }
157165 return new Node (IPAddressUtils .getIpCompatible (host ), instance .getPort ());
158166 }
@@ -178,13 +186,15 @@ public ServiceInstances getRemoteAddresses() {
178186 return remoteAddresses ;
179187 }
180188
181- public int size (){
189+ public int size () {
182190 return nodes .size ();
183191 }
184192
185193 private Instance getDiscoverInstance () throws PolarisException {
186- Instance instance = BaseFlow .commonGetOneInstance (extensions , remoteCluster , routers , lbPolicy , protocol , clientId );
187- LOG .info ("success to get instance for service {}, instance is {}:{}" , remoteCluster , instance .getHost (), instance .getPort ());
194+ Instance instance = BaseFlow .commonGetOneInstance (extensions , remoteCluster , routers , lbPolicy , protocol ,
195+ clientId );
196+ LOG .info ("success to get instance for service {}, instance is {}:{}" , remoteCluster , instance .getHost (),
197+ instance .getPort ());
188198 return instance ;
189199 }
190200
0 commit comments