In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to dynamically configure subscription and downgrade in Seata. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Dynamically configure subscription
The Seata configuration Center has a listener benchmark interface, which mainly has an abstract method and a default method, as follows:
Io.seata.config.ConfigurationChangeListener
There are two main implementation types of the listener benchmark interface:
Implementation of registration configuration subscription event listener: dynamic configuration subscription for various functions. For example, GlobalTransactionalInterceptor implements ConfigurationChangeListener and dynamic degradation function based on dynamic configuration subscription.
Implement dynamic subscription function and adaptation of configuration center: for file type default configuration center that does not have dynamic subscription function at present, you can implement the benchmark interface to dynamically configure subscription function; for blocking subscription, you need another thread to perform, so the benchmark interface can be adapted and the thread pool of the benchmark interface can be reused. And there are asynchronous subscriptions, subscribing to a single key, subscribing to multiple key, and so on. We can all implement the benchmark interface to adapt to each configuration center.
Here we use the default file configuration center and take its implementation class FileListener as an example. Its implementation logic is as follows:
As above
DataId: configuration properties for subscriptions
Listener: configure a subscription event listener to use the incoming listener as a wrapper to perform the real change logic. It is important to note that this listener implements the ConfigurationChangeListener interface like FileListener, except that FileListener is used to provide dynamic configuration subscription functions to file, while listener is used to perform configuration subscription events.
Executor: the thread pool used to handle the configuration change logic, used in the ConfigurationChangeListener#onProcessEvent method.
The implementation of the FileListener#onChangeEvent method enables file to configure subscriptions dynamically, and its logic is as follows:
An infinite loop gets the current value of the configuration property of the subscription, gets the old value from the cache, determines whether there is a change, and executes the logic of passing in listener externally if there are any changes.
ConfigurationChangeEvent is used to save the event class of configuration changes, and its member properties are as follows:
How does the getConfig method here perceive changes in the file configuration? We click in and find that the final logic is as follows:
Find that it creates a future class, then wraps it as a Runnable and puts it into a thread pool to execute asynchronously, and finally calls the get method to block and get the value, so let's move on:
AllowDynamicRefresh: dynamic refresh configuration switch
The time cache of the last change in targetFileLastModified:file.
The above logic:
Get the time value tempLastModified of the last update of file, and then compare the cache value targetFileLastModified. If tempLastModified > targetFileLastModified indicates that the configuration has changed during this period, reload the file instance and replace the old fileConfig, so that the following operations can obtain the latest configuration value.
The logic to add a configuration property listener is as follows:
ConfigListenersMap is the configuration listener cache for FileConfiguration. Its data structure is as follows:
ConcurrentMap configListenersMap
As can be seen from the data structure, each configuration property can be associated with multiple event listeners.
Finally, the onProcessEvent method is executed, which is the default method in the listener benchmark interface, which calls the onChangeEvent method, that is, the implementation in FileListener.
Dynamic degradation
With the above dynamic configuration subscription functions, we only need to implement the ConfigurationChangeListener listener to do a variety of functions. At present, Seata can only dynamically downgrade useful to dynamically configure subscription functions.
In the article "Seata AT mode startup source code analysis", it is mentioned that when Spring integrates Seata, it will use GlobalTransactionalInterceptor instead of the method annotated by GlobalTransactional and GlobalLock when AT mode is started. GlobalTransactionalInterceptor implements MethodInterceptor and eventually executes the invoker method, so you can tamper here if you want to achieve dynamic degradation.
Add a member variable to GlobalTransactionalInterceptor:
Private volatile boolean disable
Initialize the assignment in the constructor:
The parameter ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION (service.disableGlobalTransaction) currently has two functions:
Decide whether to open a global transaction at startup
After opening the global transaction, decide whether to demote or not.
Implement ConfigurationChangeListener:
The simple logic here is to determine whether the listening event belongs to the ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION configuration property, and if so, update the disable value directly.
Next, do something in GlobalTransactionalInterceptor#invoke.
As above, when disable = true, global transactions and global locks are not executed.
Configure the central subscription degraded listener
Io.seata.spring.annotation.GlobalTransactionScanner#wrapIfNecessary
On how to dynamically configure subscription and downgrade in Seata to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.