Int nextServerIndex=incrementAndGetModulo (serverCount);
I feel the source of this statement should be obtain corresponding index of cluster service, and then to get the corresponding cluster index to provide port, but he said here is to provide the number of times the return
,,,
/*
*
* Copyright 2013 Netflix, Inc.,
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in the compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS is" BASIS,
* WITHOUT WARRANTIES OR the CONDITIONS OF ANY KIND, either express OR implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Package com.net flix. Loadbalancer;
The import com.net flix. Client. Config. IClientConfig;
The import org. Slf4j. Logger;
The import org. Slf4j. LoggerFactory;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/* *
* The most well known and basic load balancing strategy, i.e. for Round Robin Rule.
*
* @ author stonse
* @ author Nikos Michalakis & lt; [email protected] & gt;
*
*/
Public class RoundRobinRule extends AbstractLoadBalancerRule {
Private AtomicInteger nextServerCyclicCounter;
Private static final Boolean AVAILABLE_ONLY_SERVERS=true;
Private static final Boolean ALL_SERVERS=false;
Private static Logger log=LoggerFactory. GetLogger (RoundRobinRule. Class);
Public RoundRobinRule () {
NextServerCyclicCounter=new AtomicInteger (0);
}
Public RoundRobinRule (ILoadBalancer lb) {
This ();
SetLoadBalancer (lb);
}
Public Server choose (ILoadBalancer lb, Object key) {
If (lb==null) {
The warn (" no load balancer ");
return null;
}
Server Server=null;
int count=0;
While (server==null & amp; & Count++ & lt; 10) {
ListReachableServers=lb. GetReachableServers ();
ListAllServers=lb. GetAllServers ();
Int upCount=reachableServers. The size ();
Int serverCount=allServers. The size ();
If ((upCount==0) | | (serverCount==0)) {
The warn (" No up the servers available from the load balancer: "+ lb);
return null;
}
Int nextServerIndex=incrementAndGetModulo (serverCount);
Server=allServers. Get (nextServerIndex);
If (server==null) {
Transient/*. */
Thread. The yield ();
continue;
}
If (for server isAlive () & amp; & (server. IsReadyToServe ())) {
Return (server);
}
//Next.
Server=null;
}
If (count & gt;=10) {
The warn (" No available alive the servers after 10 tries from the load balancer: "
+ lb);
}
Return the server;
}
/* *
* Inspired by the implementation of {@ link AtomicInteger# incrementAndGet ()}.
*
* @ param modulo The modulo to bound The value of The counter.
* @ return The next value.
*/
Private int incrementAndGetModulo (int modulo) {
For (;; ) {
Int current=nextServerCyclicCounter. The get ();
Int next=(current + 1) % modulo;
If (nextServerCyclicCounter.com pareAndSet (current and next))
Return next;
}
}
@ Override
Public Server choose (Object key) {
Return to choose (getLoadBalancer (), key);
}
@ Override
Public void initWithNiwsConfig (IClientConfig clientConfig) {
}
}
CodePudding user response:
embarrassed, didn't see him rewrite method is + up first and then, thought it was in addition to and then come out, it's all right,,, we broke up, the next appears divided among the people directly