Seata dynamic configuration subscription to the realization of the principle of relegation

Seata downgrade requires a combination of dynamic distribution center dynamic configuration subscriptions. Dynamic configuration subscribing to listen to subscribe via configuration center, read the updated value of the cache as needed, ZK, Apollo, Nacos and other third-party distribution center have a ready listener can achieve dynamic refresh configuration; dynamic demotion, namely through dynamic update Specifies the configuration parameter values ​​can be dynamically controlled so that the global transaction Seata failure during operation (aT mode currently only has this feature).

Then the center Seata multiple configurations to support different is how to adapt and how to implement dynamic configuration subscription to downgrade it? Below the level of detail from source to explain something to you.

Subscribe dynamic configuration

Seata arranged a center reference listener interface, which has a main method and the default abstract methods, as follows:

io.seata.config.ConfigurationChangeListener

The listener standard interfaces to achieve two main types:

  1. Subscribe to implement the configuration register event listeners: Subscribe to implement dynamic configuration of various functions, such as GlobalTransactionalInterceptor realized ConfigurationChangeListener, dynamic downgrade dynamic configuration according to subscribe achieved;
  2. Configuration Center to achieve dynamic subscriptions and adaptation: For the file type is not a dynamic subscriptions default configuration center to realize the reference interface to achieve dynamic configuration subscriptions; a thread to perform the required subscription for obstruction from another, this time you can implement this standard interface adapter, you can reuse the reference interface thread pool; and there are asynchronous subscription, the subscription has a single key, multiple key and so have a subscription, we can achieve the standard interfaces to fit various configurations center.

Here center with the default configuration file, to the implementation of which sub FileListener example, its implementation logic is as follows:

As above,

  • dataId: configuration properties for the subscription;
  • listener: Configuring subscription event listener for external incoming listener as a wrapper, the implementation of real change logic, It should be noted in particular, the listener and FileListener also achieved ConfigurationChangeListener interface, although for FileListener to provide dynamic configuration file subscriptions, and the listener is used to perform configuration subscribe to events ;
  • executor: configuration changes for processing logic thread pool, used in ConfigurationChangeListener # onProcessEvent method.

Achieve FileListener # onChangeEvent method allows dynamic configuration file with a subscription function , its logic is as follows:

Infinite loop to get the current value of the subscription configuration attributes, get the old value from the cache to determine whether there is a change, if there is a change on the implementation of logic incoming external listener.

ConfigurationChangeEvent for saving the event class configuration changes, and its member properties as follows:

getConfig method here is how to change the perception of the configuration file it? We ordered in, found its ultimate logic is as follows:

Found that it is to create a future class, then packed into a Runnable into asynchronous thread pool, and finally call the get method blocks to get the value, we continue to read:

allowDynamicRefresh: dynamic refresh configuration switch;

targetFileLastModified: The last time file changes to the cache.

Above logic:

Gets file last updated value tempLastModified, then compare contrast cached values ​​targetFileLastModified, if tempLastModified> targetFileLastModified, description has changed during configuration, then you reload the file instance, replacing the old fileConfig, so that the operator can get back to the latest configuration values.

Add a listener logic configuration properties as follows:

configListenersMap is disposed listener FileConfiguration cache, its data structure is as follows:

ConcurrentMap<String/*dataId*/, Set<ConfigurationChangeListener>> configListenersMap复制代码

As can be seen from the data structure, each of the plurality of configuration properties may be associated with event listeners.

OnProcessEvent final execution method, this is a reference listener interfaces inside the default method, it calls onChangeEvent method that will eventually call implementation FileListener in.

Dynamic downgrade

With these dynamic configuration subscription feature, we only need to implement ConfigurationChangeListener listener, you can do all kinds of various functions, currently Seata only useful for dynamic downgraded to dynamically configure subscription features.

In the " Seata AT mode start source code analysis ," the article mentioned, Spring Seata integrated projects, in the AT mode is active, with the use of GlobalTransactionalInterceptor instead of the annotated method GlobalTransactional and GlobalLock, GlobalTransactionalInterceptor realized MethodInterceptor, eventually invoker execution method, then you want to achieve dynamic downgrade, you can play tricks here.

  • Add a member variable in GlobalTransactionalInterceptor in:
private volatile boolean disable; 复制代码

Initialize assignment in the constructor:

ConfigurationKeys.DISABLE GLOBAL TRANSACTION (service.disableGlobalTransaction) This parameter currently has two functions:

  1. Decide whether to open a global transaction at startup;
  2. After the global affairs, to decide whether to downgrade.
  • Achieve ConfigurationChangeListener:

The logic here is simple, is to determine whether an event listener ConfigurationKeys.DISABLE GLOBAL TRANSACTION configuration properties, if it is directly updated disable value.

  • Next to cheat in GlobalTransactionalInterceptor # invoke in

As above, when the disable = true, global transaction is not performed and the global lock.

  • Subscribe to downgrade listener configuration center

io.seata.spring.annotation.GlobalTransactionScanner#wrapIfNecessary

Wrap logic process carried out in Spring AOP, the current configuration of the center will subscribe to downgrade event listener.

No public more exciting articles please pay attention to the maintenance of a "back-end Advanced", which is a focus on back-end technology-related public number.

No public attention and reply "back-end" to receive free e-books related to the back-end.

Welcome to share, reproduced Please keep the source.

No public "back-end Advanced", focused on the back-end technology to share!

Guess you like

Origin juejin.im/post/5dff749d6fb9a015fd69c5fa