In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the Sentinel extension mechanism". In the daily operation, I believe many people have doubts about what the Sentinel extension mechanism is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the Sentinel extension mechanism?" Next, please follow the editor to study!
Sentinel provides a variety of SPI interfaces to provide extended capabilities. Users can extend the interface implementation on the basis of the same sentinel-core, so that they can easily add custom logic to Sentinel.
Initialize logical extension mechanism
In order to unify the initialization process, we abstract some initialization logic that the InitFunc interface represents Sentinel, such as:
Register a dynamic rule source (example)
Register the StatisticSlot callback function (example)
Start Command Center
Initialize heartbeat transmission
We can set the priority of InitFunc execution through annotations. When the application accesses the resource for the first time, the registered initialization functions are executed in turn. If you want to trigger initialization manually in advance, you can call the InitExecutor.doInit () function at the appropriate location (such as Spring Bean), and the repeated call will only be executed once.
Slot Chain extension mechanism
Sentinel is composed of a series of slot slot chain to complete various functions, including the construction of call chain, call data statistics, rule checking and so on. The order between the slot is very important. Sentinel extends SlotChainBuilder as a SPI interface, which makes SlotChain have the ability to expand. Users can add custom slot and arrange the order between slot, so that they can add custom functions to Sentinel.
Cdn.nlark.com/lark/0/2018/png/47688/1539165202133-64062dc7-88f1-45ac-8177-c29cd9ca0e9d.png ">
For example, if we want to record the current context and resource information after requesting a pass, we can implement a simple slot:
Public class DemoSlot extends AbstractLinkedProcessorSlot {@ Override public void entry (Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object...) Args) throws Throwable {System.out.println ("Current context:" + context.getName ()); System.out.println ("Current entry resource:" + context.getCurEntry (). GetResourceWrapper (). GetName ()); fireEntry (context, resourceWrapper, node, count, args);} @ Override public void exit (Context context, ResourceWrapper resourceWrapper, int count, Object...) Args) {System.out.println ("Exiting for entry on DemoSlot:" + context.getCurEntry (). GetResourceWrapper (). GetName ()); fireExit (context, resourceWrapper, count, args);}}
Then implement a SlotChainBuilder that adds our new slot to the end of the chain based on the DefaultSlotChainBuilder (or you can freely combine the existing slot without DefaultSlotChainBuilder):
Package com.alibaba.csp.sentinel.demo.slot;public class DemoSlotChainBuilder implements SlotChainBuilder {@ Override public ProcessorSlotChain build () {ProcessorSlotChain chain = new DefaultSlotChainBuilder () .build (); chain.addLast (new DemoSlot ()); return chain;}}
Finally, add the class name of the implemented SlotChainBuilder to the SPI configuration file com.alibaba.csp.sentinel.slotchain.SlotChainBuilder in the resources/META-INF/services directory to take effect:
# Custom slot chain buildercom.alibaba.csp.sentinel.demo.slot.DemoSlotChainBuilder
The hot spot current limiting module of Sentinel uses the extension mechanism of Slot Chain to add the hot spot current limiting function to the original function chain.
StatisticSlot Callback
Before StatisticSlot contains too much logic, such as ordinary QPS and hot parameter QPS addPass/addBlock and other logic statistics are in StatisticSlot, each logic is mixed together, which is not conducive to expansion. Therefore, it is necessary to abstract a series of callback for StatisticSlot, so that StatisticSlot has the basic extensibility, and a series of logic is decoupled from StatisticSlot, which is more clear. Currently, Sentinel provides two kinds of callback:
ProcessorSlotEntryCallback: contains two callback functions, onPass and onBlocked, which are executed when the request is passed through StatisticSlot and when the request is blocked.
ProcessorSlotExitCallback: contains the onExit callback function, which is executed when the request passes through StatisticSlot exit.
Users only need to register the implemented callback with StatisticSlotCallbackRegistry to take effect.
Dynamic rule source
Sentinel's dynamic rule data source is used to read and write rules from external storage. Sentinel divides dynamic rule data sources into two types: read data sources (ReadableDataSource) and write data sources (WritableDataSource), thus making the responsibilities of different types of data sources clearer:
The read data source is only responsible for listening or polling for remote storage changes.
The write data source is only responsible for writing rule changes to the rule source.
We just need to implement the dynamic rule source ourselves and register it with the corresponding RuleManager so that the rules can be configured and pulled / pushed in real time. When registering a dynamic rule source, you can automatically register at initialization with the help of Sentinel's InitFunc SPI.
Transport extension mechanism
CommandCenter Extensible: users can use different network protocols or different libraries to implement Transport API Server.
HeartbeatSender Extensible: users can use different network protocols and heartbeat strategies to send heartbeats (report to the console).
CommandHandler Extensible: users can implement CommandHandler and register in the SPI configuration file to add custom commands for CommandCenter.
At this point, the study on "what is the Sentinel extension mechanism" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.