foreword

First of all, let’s clarify what is called hot deployment. Hot deployment is the behavior of automatically updating the class without restarting the java virtual machine, thereby updating the logic of the entire runtime.

In the field of java development, hot deployment has always been a difficult problem to solve. The java virtual machine can only realize the hot deployment of method body modification in theory. For the change of the entire class structure, it is still necessary to restart the virtual machine and reload the class to complete the update. operate.

OSGI

In fact, there are some solutions in the java industry, such as the osgi architecture. This thing has been around for a long time, but it has not become popular. The emergence of the osgi architecture can make the java system into a modular form, making it possible to restart the module. To a certain extent, it is also a hot deployment solution. It’s a pity that this thing was developed before and felt very anti-human. Not to mention a lot of configuration files, the learning cost is also very high. Combined with spring, it is actually a module and a spring context system. And if there is a call relationship between the modules, restarting the related modules will cause the application to experience a short functional shock, that is to say, the entire hot start process is not smooth.

It is estimated that many small partners have never heard of this technology, and it is gradually withdrawing from the stage of history. It is estimated that few companies use it.

ASM

ASM is a framework for modifying bytecode, and there is Cglib for the same type of framework. These frameworks can load a class information, and users can enhance and modify the information according to their own needs, and finally output it into a new class.

The specific implementation process will not be expanded here. You can download it on Baidu, and there are many related technical implementation articles.

However, simply modifying the bytecode generally needs to be combined with other technologies, and this alone cannot complete the hot update. Although the framework of the ASM class can modify the class, the modification logic of these ASMs is also written in java, and this code also needs to be executed. of.If you write the ASM code in java, you can’t realize from外部Come update.

Javaagent&Attach API combined with ASM

This is the solution mentioned in the above paragraph that ASM needs to be combined with other technologies to achieve hot updates, and it is also the solution adopted by many open source frameworks.

For example, the well-known Arthas uses javaagent to load the target Java program at runtime through the Attach API, and finally uses the Instrumention API or ASM to enhance the class to achieve code tracking and code hot modification features.

However, the author believes that using Arthas to complete the hot update of online code can only be used in some very urgent scenarios. Not a replacement for routine business logic modifications. And it’s quite complicated to operate.

You need to modify the java code first, then put the java code on the server, find the hashcode of the class loader of this class in arthas, and then use the command provided by arthas to compile the java code and output it into a new class file.redefinecommand for hot update.

Imagine if a lot of logic changes. What a hassle.

So it is more to use arthas to diagnose and track online applications, and hot change code is actually one of the many functions of arthas, not the main function.

another way of thinking

The above solutions are based on modifying the class itself, and then reloading the JVM’s class loader.

So is there a better solution?

In fact, java code can run some scripts. jdk itself supports calling scripts. Starting from JDK 1.6, java supports JSR223. Some scripting languages ​​can be executed on the JVM in a consistent form, and can be compiled in real time. Comparable.

Some students may be confused when they see this: scripts can only be used to change part of the logic, it is impossible to write all the logic in scripts, your article is not about “is it possible所有的逻辑Can hot update”?

Don’t worry, let’s make sure of one thing first.Does your java application system need所有的All logic can be hot updated? Many codes are roughly fixed, such as util classes, and some vo definitions do not change much, and some fixed services do not require hot update requirements. Only some decision-making parts that change frequently may require hot updates.

Then we only need to define the parts that need to be changed frequently with scripts.

Is there any similar open source framework in the industry?

There really are, and it is a popular open source project with high stars, which can help you use scripts for hot update business.

Open source orchestration rule engine

Some friends may want to say again: Didn’t you introduce the hot deployment of java code, why did the topic turn to the rule engine?

Here I want to say that a major feature of the rule engine is to strip the logic of the decision-making part to the outside, which can realize rapid hot changes of logic changes.

The rule engine framework introduced this time is more powerful. In addition to stripping the logic, it can also decouple the system, so that all your logic blocks can be changed at will.theoretically achievable所有logic can be changed, not部分Oh.

This is the orchestrated rule engine framework that is very popular in the industry now:LiteFlow.

The concept of LiteFlow is very simple, which is to divide each logic in the system into small pieces, called组件, these components can be written by java code, and can also be written by scripts. Then a complete business is to put together various components to form a complete business chain.

The advantage of this mode is that the parts that do not require hot updates can be written in java components, and the parts that need to be changed frequently can be written in scripts. All components can be mixed and matched into one business. How to arrange these components, LiteFlow has created the ELF syntax, which has a very easy-to-use programming syntax. As a programmer, you can get started in ten minutes. The pink part in the above picture is the simplest form of series connection.

Components in the business link can be replaced or added in real time to form a new business chain. At the same time, the defined components can also be reused in other links.

LiteFlow’s scripting solution is also implemented using JSR223. There are three types of scripts that have been implemented so far:

Why use the LiteFlow orchestration engine framework, all your logic can be changed. Because you can implement all the logic with script components, LiteFlow provides very powerful script support, which is completely connected to the bottom layer of Java. You can import java classes in scripts, or call java class methods, and even You can define methods and classes in scripts, and everything is written exactly the same as in java.

More exaggeratedly, LiteFlow allows you to call spring context beans in scripts, you can call DAO to fetch data in scripts, and you can send rpc to other microservices in scripts. As long as you want, you can completely move the business to the script component without writing a single line of java business code.

And you can even change the order of logic blocks at will, because LiteFlow’s layout rules and scripts can be hot changed.

LiteFlow also provides native support for commonly used storage middleware:

LiteFlow supports all relational databases. In addition, zk, etcd, and nacos all support it. It also provides additional extension interfaces for you to expand into other storage methods.

Have you ever thought about it, all your logic and rule-based grammar exist outside the system. As long as its scripts and logic are changed, the systems of all your nodes do not need to do anything, and hot changes are performed in real time.

And all of this, LiteFlow is very smooth. The so-called smoothness means that you don’t have to worry about any interruption of your business during hot changes, and there will be no abnormalities in the executing links due to hot changes.

How powerful is LiteFlow’s orchestration ability? A few simple keywords can produce unimaginable effects:

epilogue

In the field of java business hot deployment, LiteFlow, as a rule engine, provides a new solution.

In addition to what was introduced above, the LiteFlow framework also has many advanced features to improve the flexibility of your system from all directions.

And as open source software, LiteFlow has a very good community atmosphere and culture in China.

Finally, the official website and warehouse address of LiteFlow are released. If you think this open source framework is helpful to your business, come and learn about it

Project official website:

https://liteflow.yomahub.com

gitee managed warehouse:

https://gitee.com/dromara/liteFlow

github hosted warehouse:

https://github.com/dromara/liteflow

#Java #project #code #logic #hotdeployable #Personal #Space #Platinum #Saidong #News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *