Software must be able to use before reuse.
-> return column List <-
download Address: https://github.com/f641385712/netflix-learning
table of Contents
Foreword
If you already know too Netflix Hystrix
, then you are no stranger to the Command command mode. The same, Ribbon also used to perform the process based on RxJava command mode to control a request, which is the core content of this article will explain: LoadBalancerCommand
.
text
Based RxJava command modes, LoadBalancerCommand
B represents the load balancing command for transfer request to the load balancer ILoadBalancer
. It's not much related classes, the following screenshot:
One of the most important was undoubtedly LoadBalancerCommand
the API, before that, the first RBI of its base class.
ServerOperation
This interface is very simple, it is a function interface Function
only: "eat" into a Server, a spit Observable<T>
. Observable-based rxjava of support
// 仅仅继承自Func1,暴露出结果泛型T
public interface ServerOperation<T> extends Func1<Server, Observable<T>> {}
As a movement, as a passing into the reference LoadBalancerCommand#submit()
submitted to the command processing.
ExecutionInfo
A POJO, on behalf of the implementation of the information.
public class ExecutionInfo {
private final Server server;
// 在该Server上已经尝试过的总次数
private final int numberOfPastAttemptsOnServer;
// 已经尝试过的Server总个数
private final int numberOfPastServersAttempted;
// 构造器是私有化的。该静态方法用于创建实例
public static ExecutionInfo create(Server server, int numberOfPastAttemptsOnServer, int numberOfPastServersAttempted) {
return new ExecutionInfo(server, numberOfPastAttemptsOnServer, numberOfPastServersAttempted);
}
}
It is mainly used as a parameter to pass ExecutionListener
, so that the caller can be performed to snoop information.
ExecutionInfoContext
It is LoadBalancerCommand
an internal class, context info record information when executed.
LoadBalancerCommand:
class ExecutionInfoContext {
// 当前Server,每次调用set方法,此值就会变哦~~~~
// serverAttemptCount记录了当前请求已经换了几次Server了~~~
Server server;
// 已经重试的Server总数(每次换台Server就+1)
int serverAttemptCount = 0;
// 这次请求的重试总数(包括相同Server,以及换Server,总之是个总数)
int attemptCount = 0;
public ExecutionInfo toExecutionInfo() {
return ExecutionInfo.create(server, attemptCount-1, serverAttemptCount-1);
}
public ExecutionInfo toFinalExecutionInfo() {
return ExecutionInfo.create(server, attemptCount, serverAttemptCount-1);
}
}
The context is mainly a record request to the context number of retries when executed, generate a final ExecutionInfo
instance to the listener , the user can complete the personalization determination (such as a limitation: the retry would not have multiple retries).
ExecutionContext (important)
In each load balancer execution start context object is created, the metadata contains some of the load balancer and the status variable data.
public class ExecutionContext<T> {
// 存储元数据的Map
private final Map<String, Object> context;
// ChildContext继承自ExecutionContext
private final ConcurrentHashMap<Object, ChildContext<T>> subContexts;
// 请求。requestConfig的优先级比clientConfig的高哦,具有“覆盖”效果
private final T request;
private final IClientConfig requestConfig;
private final RetryHandler retryHandler;
private final IClientConfig clientConfig;
// 子上下文:它增加了一个属性ExecutionContext<T> parent;
// 这样便可以让两个ExecutionContext关联起来
private static class ChildContext<T> extends ExecutionContext<T> {
private final ExecutionContext<T> parent;
ChildContext(ExecutionContext<T> parent) {
super(parent.request, parent.requestConfig, parent.clientConfig, parent.retryHandler, null);
this.parent = parent;
}
@Override
public ExecutionContext<T> getGlobalContext() {
return parent;
}
}
... // 省略构造器赋值
// default访问权限的方法:获取子执行上下文
// 没有就会new一个然后放在ConcurrentHashMap缓存起来。key就是obj
ExecutionContext<T> getChildContext(Object obj) { ... }
...
// 获得指定name的value。一个简单的k-v而已
public void put(String name, Object value) {
context.put(name, value);
}
public Object get(String name) {
return context.get(name);
}
// 先去requestConfig里找,若没找再去clientConfig里找
public <S> S getClientProperty(IClientConfigKey<S> key) { ... }
// 注意:子上下文ChildContext的该方法返回的是parent,所以就打通了
public ExecutionContext<T> getGlobalContext() {
return this;
}
}
By the Father, Son context of design, to ensure that each listener
has its ownExecutionContext
, but you can also ExecutionContext#getGlobalContext()
be obtained for all listener
globally shared context.
ExecutionListener
Listeners call load balancer in different stages of implementation.
// I:input类型,给ExecutionContext用。这样ExecutionContext#getRequest()就能拿到这个请求对象
// O:负载均衡器执行的output输出
public interface ExecutionListener<I, O> {
// 在执行即将开始时调用。
public void onExecutionStart(ExecutionContext<I> context) throws AbortExecutionException;
// 当服务器被选中,请求**将在服务器上执行时**调用。
public void onStartWithServer(ExecutionContext<I> context, ExecutionInfo info) throws AbortExecutionException;
// 当在服务器上执行请求时接收到异常时调用(这时木有Response,只有异常信息)
public void onExceptionWithServer(ExecutionContext<I> context, Throwable exception, ExecutionInfo info);
// 执行正常,有response
public void onExecutionSuccess(ExecutionContext<I> context, O response, ExecutionInfo info);
// 在**所有重试**后认为请求失败时调用
// finalException:最终的异常。有可能包装着重试时的各种异常
public void onExecutionFailed(ExecutionContext<I> context, Throwable finalException, ExecutionInfo info);
}
And it has no built-realization for this interface, it is left to the user of a hook.
ExecutionContextListenerInvoker
It can be considered a utility class: for the execution context ExecutionContext
to call on a variety of ExecutionListener
them. Function very much like the familiar Spring Boot Lane SpringApplicationRunListeners
for executing SpringApplicationRunListener
the listener.
public class ExecutionContextListenerInvoker<I, O> {
// 执行上下文
private final ExecutionContext<I> context;
// 执行时的监听器们
private final List<ExecutionListener<I, O>> listeners;
private final IClientConfig clientConfig;
// key表示的是ExecutionListener实现类的全类名
// value:IClientConfigKey。它的值是"listener." + key+ ".disabled"
// 因为CommonClientConfigKey.valueOf()比较耗时,所以这里用了缓存
private final ConcurrentHashMap<String, IClientConfigKey> classConfigKeyMap;
... // 省略构造器
public void onExecutionStart() {
onExecutionStart(this.context);
}
public void onExecutionStart(ExecutionContext<I> context) {
for (ExecutionListener<I, O> listener : listeners) {
// 若这个Listener没有被禁用,那就执行它
if (!isListenerDisabled(listener)) {
// 请注意:这里的上下文使用的是子上下文哦
// 所以保证了每个监听器均有一个自己的上下文,各有一份自己的数据,线程安全
listener.onExecutionStart(context.getChildContext(listener));
}
}
}
... // 执行监听器其它方法的逻辑一毛一样,略
// 判断一个监听器是否被禁用。可通过动态配置来开/关
// "listener." + className + ".disabled":禁用
private boolean isListenerDisabled(ExecutionListener<?, ?> listener) {
if (clientConfig == null) {
return false;
} else {
String className = listener.getClass().getName();
IClientConfigKey key = classConfigKeyMap.get(className);
if (key == null) {
key = CommonClientConfigKey.valueOf("listener." + className + ".disabled");
IClientConfigKey old = classConfigKeyMap.putIfAbsent(className, key);
if (old != null) {
key = old;
}
}
return clientConfig.getPropertyAsBoolean(key, false);
}
}
}
- Ribbon listener is left to the developers involved in the process of execution of hook load balancing, the more important (without any built-realization)
- Each Listener executed by its own context, thread safe
- May be configured by externally of:
"listener." + className + ".disabled"
a way to disable execution of the listener, a certain flexibility to meet the design
to sum up
About Ribbon Load Balancing command: LoadBalancerCommand (a) on the basis of class RBI first introduced to this part. This article is relatively simple, mainly for the following services, so stay tuned.
statement
The original is not easy, not easy to code word, thank you for your thumbs, collection, attention. 把本文分享到你的朋友圈是被允许的,但拒绝抄袭
. You can also scan code [left / or add wx: fsx641385712] I invite you to join the Java Engineering, Architect Series family group learning and communication.
- [Learn to enjoy Netflix] a, Apache Commons Configuration: Configuration management experts around you
- [Learn to enjoy Netflix] two, Apache Commons Configuration event listener mechanism to implement and use ReloadingStrategy hot update
- [Learn to enjoy Netflix] three, Apache Commons Configuration2.x new event - sensing mechanism
- [Learn to enjoy Netflix] four, Apache Commons Configuration2.x file positioning system FileLocator and FileHandler
- [Learn to enjoy Netflix] five, Apache Commons Configuration2.x different kind of Builder mode: ConfigurationBuilder
- [Learn to enjoy Netflix] six, Apache Commons Configuration2.x quickly build tools Parameters and Configurations
- [Learn to enjoy Netflix] seven, Apache Commons Configuration2.x how to file the heat load / hot update?
- [Learn to enjoy Netflix] eight, Apache Commons Configuration2.x difference compared to what brought on the 1.x use?
- [Learn to enjoy Netflix] nine, Archaius configuration management library: first experience and explain the underlying API
- [Learn to enjoy Netflix] ten, Archaius extensions to the core of Commons Configuration API Configuration implementation
- [Science enjoy Netflix] eleven, Archaius configuration manager ConfigurationManager and dynamic properties to support DynamicPropertySupport
- [Learn to enjoy Netflix] twelve, Archaius dynamic properties DynamicProperty explain the principles (important)
- [Learn to enjoy Netflix] thirteen, Archaius property abstract Property and PropertyWrapper Detailed
- [Learn to enjoy Netflix] fourteen, Archaius how to provide configuration support for multi-environment, multi-zone, cloudy deployment?
- [Learn to enjoy Netflix] fifteen, Archaius and Spring Cloud Integration: spring-cloud-starter-netflix-archaius
- [Learn to enjoy Netflix] sixteen, Hystrix breaker: early experience and RxJava Profile
- [Science enjoy Netflix] seventeen, MAMMALIA, and abstract properties and achieve integration Archaius external configuration, dynamic
- [Science enjoy Netflix] eighteen, Hystrix Disposing: Global instance configuration and
- [Learn to enjoy Netflix] nineteen, Hystrix plug-in mechanism: SPI Interface Detailed Description and HystrixPlugins
- [Learn to enjoy Netflix] twenty, Hystrix transfer data across threads Solution: HystrixRequestContext
- [Science enjoy Netflix] twenty-one, Hystrix index data collection (warm-up): sliding window algorithm (with code sample)
- [Learn to enjoy Netflix] twenty-two, Hystrix event source and an event stream: HystrixEvent and HystrixEventStream
- [Learn to enjoy Netflix] twenty-three, Hystrix bucket counter: BucketedCounterStream
- [Learn to enjoy Netflix] twenty-four, Hystrix statistics in the sliding window: BucketedRollingCounterStream, HealthCountsStream
- [Science enjoy Netflix] twenty-five, cumulative statistics MAMMALIA, flow, flow distribution, maximum concurrent flow, flow configuration, flow function (with code sample)
- [Science enjoy Netflix] twenty-six, Hystrix index data collector: HystrixMetrics (HystrixDashboard data sources)
- [Learn to enjoy Netflix] twenty-seven, Hystrix What is the half-open state of the circuit breaker? Detailed HystrixCircuitBreaker
- [Learn to enjoy Netflix] twenty-eight, Hystrix event counter EventCounts and the results ExecutionResult
- [Learn to enjoy Netflix] twenty-nine, Hystrix execution core interface: HystrixExecutable, HystrixObservable and HystrixInvokableInfo
- [Learn to enjoy Netflix] thirty, Hystrix the fallback fallback / downgrade logical interpretation Source: getFallbackOrThrowException
- [Science enjoy Netflix] xxxi, five cases and degraded sample code fallback trigger logic Hystrix
- [Learn to enjoy Netflix] thirty-two, Hystrix throw HystrixBadRequestException why the exception does not trigger the fuse?
- [Learn to enjoy Netflix] 33 or when Hystrix execution of the target method, how to call the thread pool resources?
- [Science enjoy Netflix] thirty-four, Hystrix method of performing certain logic source Interpretation: executeCommandAndObserve
- [Learn to enjoy Netflix] thirty-five, Hystrix execution of a master: AbstractCommand Comments
- [Science enjoy Netflix] thirty-six, Hystrix request command: HystrixCommand and HystrixObservableCommand
- [Learn to enjoy Netflix] thirty-seven, Primal Ribbon Introduction - client load balancer
- [Science enjoy Netflix] thirty-eight, Ribbon core API parses source: ribbon-core (a) requesting client IClient
- [Learn to enjoy Netflix] thirty-nine, Ribbon core API source code parsing: ribbon-core (two) IClientConfig configuration in detail
- [Science enjoy Netflix] forty, Ribbon core API parses source: ribbon-core (three) RetryHandler retry processor
- [Learn to enjoy Netflix] forty-one, Ribbon core API source code analysis: abnormal ribbon-core (four) ClientException client
- [Learn to enjoy Netflix] forty-two, LoadBalancer five components of the Ribbon: IPing heartbeat
- [Learn to enjoy Netflix] forty-three, LoadBalancer five components of the Ribbon: ServerList list of services
- [Learn to enjoy Netflix] forty-four, netflix-statistics Detailed, taught you how to write a super simple version of the monitoring system
- [Learn to enjoy Netflix] forty-five, Ribbon server status: ServerStats its principle breaker
- [Learn to enjoy Netflix] forty-six, Ribbon policy server load balancing state total control: LoadBalancerStats
- [Learn to enjoy Netflix] forty-seven, Ribbon multi-region selection: ZoneAvoidanceRule.getAvailableZones () Gets the available area
- [Science enjoy Netflix] forty-eight, Ribbon server filtering logic basic components: AbstractServerPredicate
- [Learn to enjoy Netflix] forty-nine, LoadBalancer five components of the Ribbon: ServerListFilter Service List Filter
- [Learn to enjoy Netflix] fifty, LoadBalancer the five components of the Ribbon: ServerListUpdater list of services updater
- [Science enjoy Netflix] fifty-one, Ribbon LoadBalancer the five components of: IRule (a) polling and weighted round-robin
- [Learn to enjoy Netflix] fifty-two, Ribbon LoadBalancer of the five components of: IRule (II) used in large-scale cluster of configurable rules
- [Learn to enjoy Netflix] fifty-three, Ribbon LoadBalancer of the five components of: IRule (iii) random and retry all IRule realize summary
- [Learn to enjoy Netflix] fifty-four, Ribbon initiate the connection operation: IPrimeConnection detect Server is able to provide services
- [Learn to enjoy Netflix] fifty-five, Ribbon load balancer execution context: LoadBalancerContext
- [Science enjoy Netflix] fifty-six, Ribbon load balancer ILoadBalancer (a): BaseLoadBalancer
- [Learn to enjoy Netflix] fifty-seven, Ribbon load balancer ILoadBalancer (two): ZoneAwareLoadBalancer have regional awareness, dynamic load balancer service list