本文主要研究一下artemis的ConnectionLoadBalancingPolicyjava
activemq-artemis-2.11.0/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorImpl.javagit
public final class ServerLocatorImpl implements ServerLocatorInternal, DiscoveryListener { //...... private TransportConfiguration selectConnector() { Pair<TransportConfiguration, TransportConfiguration>[] usedTopology; flushTopology(); synchronized (topologyArrayGuard) { usedTopology = topologyArray; } synchronized (this) { if (usedTopology != null && useTopologyForLoadBalancing) { if (logger.isTraceEnabled()) { logger.trace("Selecting connector from topology."); } int pos = loadBalancingPolicy.select(usedTopology.length); Pair<TransportConfiguration, TransportConfiguration> pair = usedTopology[pos]; return pair.getA(); } else { if (logger.isTraceEnabled()) { logger.trace("Selecting connector from initial connectors."); } int pos = loadBalancingPolicy.select(initialConnectors.length); return initialConnectors[pos]; } } } //...... }
activemq-artemis-2.11.0/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/client/loadbalance/ConnectionLoadBalancingPolicy.javagithub
public interface ConnectionLoadBalancingPolicy { /** * Returns the selected index according to the policy implementation. * * @param max maximum position index that can be selected */ int select(int max); }
activemq-artemis-2.11.0/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/client/loadbalance/FirstElementConnectionLoadBalancingPolicy.javaapache
public final class FirstElementConnectionLoadBalancingPolicy implements ConnectionLoadBalancingPolicy { /** * @param max param is ignored * @return 0 */ @Override public int select(final int max) { return 0; } }
activemq-artemis-2.11.0/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/client/loadbalance/RandomConnectionLoadBalancingPolicy.javaapi
public final class RandomConnectionLoadBalancingPolicy implements ConnectionLoadBalancingPolicy { /** * Returns a pseudo random number between {@code 0} (inclusive) and {@code max} exclusive. * * @param max the upper limit of the random number selection * @see java.util.Random#nextInt(int) */ @Override public int select(final int max) { return RandomUtil.randomInterval(0, max); } }
activemq-artemis-2.11.0/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/client/loadbalance/RoundRobinConnectionLoadBalancingPolicy.javadom
public final class RoundRobinConnectionLoadBalancingPolicy implements ConnectionLoadBalancingPolicy, Serializable { private static final long serialVersionUID = 7511196010141439559L; private boolean first = true; private int pos; @Override public int select(final int max) { if (first) { // We start on a random one pos = RandomUtil.randomInterval(0, max); first = false; } else { pos++; if (pos >= max) { pos = 0; } } return pos; } }
activemq-artemis-2.11.0/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/client/loadbalance/RandomStickyConnectionLoadBalancingPolicy.javaide
public final class RandomStickyConnectionLoadBalancingPolicy implements ConnectionLoadBalancingPolicy { private int pos = -1; /** * @see java.util.Random#nextInt(int) */ @Override public int select(final int max) { if (pos == -1) { pos = RandomUtil.randomInterval(0, max); } return pos; } }
ConnectionLoadBalancingPolicy定義了select接口,返回選中的index;它有四個實現類分別是FirstElementConnectionLoadBalancingPolicy、RandomConnectionLoadBalancingPolicy、RoundRobinConnectionLoadBalancingPolicy、RandomStickyConnectionLoadBalancingPolicythis