Initialize beans asynchronously

When I was watching SOFABoot two days ago, I saw something that caught my eye. Let me review it for you.

SOFABoot may not be familiar to you, but it doesn't matter. This article is not here to tell you about it. Just think of it as a variant of SpringBoot.

Because it is endorsed by Ant Financial, this framework is mainly used by some financial companies:

picture

The official introduction is as follows:

SOFABoot is Ant Financial's open-source R&D framework based on Spring Boot. Based on Spring Boot, it provides capabilities such as readiness check, class isolation, and log space isolation. While enhancing Spring Boot, SOFABoot provides users with the ability to use SOFA middleware in Spring Boot very conveniently.

The above functions are all powerful, but I mainly share this small function:

https://help.aliyun.com/document_detail/133162.html

picture

This function allows the Bean's initialization method to be executed in an asynchronous thread, thereby speeding up the Spring context loading process and improving application startup speed.

Why did my eyes light up when I saw the function? It's because I wrote this article a long time ago. "I really didn't expect that this interview question has been discussed since 11 years ago, and the official position was only announced this year."

The interview questions mentioned there are as follows:

Spring will do class scanning during startup and put ioc in singleton mode. But spring only processes classes one by one. If, in order to speed up, we cancel the class scanning function that comes with spring and use multi-threaded code to process in parallel, is this solution feasible? Why?

At that time, I found the official response to this issue through issue and summarized it as follows: We should first find the root cause of the slow startup instead of blaming the problem on Spring. For Spring, if you can't move this part, don't move it.

Judging from the title "Startup Acceleration - Asynchronous Initialization Method", SOFABoot supports things that Spring does not officially support. So this thing caught my eye. I want to see how you do it.

Let’s talk about the conclusion first: SOFABoot’s solution can solve the problem to a certain extent, but it relies on us specifying which beans can be initialized asynchronously when coding. The advantage of this is that we don’t have to consider various complexities such as circular dependencies, dependency injection, etc. The disadvantage is that programmers need to identify which classes can be initialized asynchronously.

I think programmers should have the ability to "identify which classes in their own projects can be initialized asynchronously".

However, once the programmer is asked to take the initiative to identify it, he has already "lost", it is no longer amazing enough, and the implementation difficulty is not at the same level. What Spring thinks is that the framework will handle everything for you. At most, it will leave you a switch. You can use it out of the box without having to worry about anything.

But in general, as a learning case for the evolution of ideas into source code, it is still very good.

We mainly look at the implementation plan and specific logic code, using SOFABoot as the starting point, focusing on its "asynchronous initialization method", using the source code as a link, and working with Spring to create a set of "I saw it -> I can use it - >I’ll take it over -> I understand it -> it’s mine -> put it on my resume.”

Demo

Let’s make a demo first to demonstrate a wave of effects so that you can intuitively see what this is.

This Demo is very simple and can be completed with just a few lines of code.

First create two java classes, which have an init method:

picture

Then they are handed over to Spring for management as beans, and the demo is set up:

picture

Start the project directly, the startup time only takes 1.152s, very smooth:

picture

And then, notice, I'm going to deform it a little bit.

When injecting a bean, trigger the initialization method to simulate the initialization phase of the bean in the actual project. During the startup process of the Spring project, do some data preparation, configuration pulling and other related operations:

picture

Restart the project again. Because two Bean initialization actions need to be performed, each taking 5 seconds, and they are executed serially, the startup time directly comes to 11.188s:

picture

Then, it’s time to witness the miracle.

I added an annotation like @SofaAsyncInit:

picture

Don't worry about where this annotation comes from. From the name of this annotation, you also know what it does: asynchronous execution initialization.

At this time I will start the project again:

picture

You can see from the log:

  1. The init methods of whyBean and maxBean are executed in parallel by two different threads.
  2. The startup time is shortened to 6.049s.

Therefore, the @SofaAsyncInit annotation implements "specifying the initialization method of the Bean to implement asynchronousization".

Think about it, if you have 10 Beans, each Bean takes 1 second to initialize, totaling 10 seconds.

However, there is actually no need for serial initialization between these beans, so using this annotation, parallelization only takes 1 second, and it is done.

At this point, you have seen that such a thing exists, which is "I saw it".

Next, we enter the "I can use it" link.

how come.

Before explaining the principle, I have to tell you where this annotation comes from.

It belongs to the annotations in the SOFABoot framework. First, you have to modify your SpringBoot to SOFABoot.

This step refers to the "Quick Start" section in the official documentation, which is very simple:

https://www.sofastack.tech/projects/sofa-boot/quick-start/

The first step is to put the following in the pom.xml of the project:

<parent>
    <groupId>org.springframework.boot</groupId>
    <art

Guess you like

Origin blog.csdn.net/mmmmm44444/article/details/132825297