This article is shared from HUAWEI CLOUD Community's "Hands-on Experience of Sermant Custom Plug-in Development-Cloud Community-HUAWEI CLOUD", author: HUAWEI CLOUD Open Source.
1. Reasons for the study
Since we are currently in the automotive industry, we used the relevant plug-ins in Sermant when migrating cloud services to the project, in order to deepen our understanding of Sermant's development and operation mechanism, we experienced the development of Sermant custom plug-ins from scratch.
下面我们就Sermant-example中的first-plugin-demo来进行研究说明。
二、下载Sermant-example
首先我们下载sermant-example的demo:
Once downloaded, let's start with the simplest custom plugin, which is first-plugin-demo.
3. Perform packaging
Execute the packaging for first-plugin-demo, and complete the structure of the package:
You can see that our project and the corresponding plugin template project are all in it.
Fourth, start the project
java -javaagent:sermant-agent.jar -jar Application.jar
Then access the controller method
You can see the effect of the interception:
You can see that in the process of starting, the interception effect is completed.
That is, it took the front and rear methods of the interceptor.
5. Dynamic configuration verification
The configuration center configured in the dynamic configuration is zookeeper, so we need to start a zookeeper as the configuration center before starting the project, and enable the dynamic configuration switch at the same time.
After that, we need to create a configuration item under the corresponding listener node to test the dynamic configuration function:
If you visit it again, you can see the following effect in the console:
That is to say, the function of dynamic configuration is completed. And we can see
The essence of the program is to create a configuration listener and implement the process method for configuration processing, in which DynamicConfigEvent is the configuration change event that is listened to, including the configuration information such as group, key, and content. The implementation of dynamic configuration is to call the process method of its own implementation, and store and process the configuration based on the map, so as to realize configuration switching.
6. What is the parameter argsMap in the whole process?
I've always been curious about what the information in the configuration is, but I found it after the recent debugging.
We can see the relevant parameters in the loaded argMap:
We can see that the parameters in argsMap are basically the same as the above configuration, and the configuration information in the plugin will be loaded at the same time. After completing these operations, you can complete the loading of the corresponding information in the whole process.
ConfigManager.initialize(argsMap) is mainly used to parse some configurations, because the configuration has different forms such as yaml and properties, so the policy mode is used here to parse. For more information, please refer to the implementation of the BaseConfig interface.
七、插件的加载是在plugins.yaml
The loading of the plugin is configured in plugins.yaml, such as our custom plugin:
The key to implementing plugins is which plugins are configured in plugins.yaml. Because these plugins are the basis for subsequent interception. That is, it tells the program which plugins need to be loaded and which plugins do not need to be loaded. With this foundation, the subsequent accurate conversion and installOn operations will be carried out.
8. The principle of interception
We can see the interception information printed in the console above, so how to achieve interception?
You can see when we execute the first-plugin-demo example: we will find that when we execute the business method, it will weave into the interceptor, execute the corresponding onMethodEnter and onMethodExit methods after the method is executed.
The functionality of these two methods is similar to that implemented using byte-buddy. Execute the woven logic in the method that needs to be intercepted.
At the same time, you can see the call from the information in the console debug:
Once we have completed the above call, we can see the information output from the console.
Here we take the entry method as an example to illustrate:
You can see that after entering the corresponding weaving method, it will eventually go to the iterator that we need to extend to implement the iteration.
You can see that the plugin interception in first-plugin-demo has entered:
Complete the interception into the business method, and realize the weaving of the interception function. Then enter the rear interception to complete the interception logic of after.
Reference:
官网: Sermant
仓库地址:GitHub - sermant-io/Sermant: A Cloud-Native Proxyless Service Mesh based on Java Bytecode Enhancement Technology
Demo仓库:GitHub - sermant-io/Sermant-examples: examples for sermant
Follow #HUAWEI CLOUD Developer Alliance# Click below to learn about HUAWEI CLOUD's fresh technologies~
HUAWEI CLOUD Blog_Big Data Blog_AI Blog_Cloud Computing Blog_Developer Center-HUAWEI CLOUD