preface

I recently tried to build a SpringCloud project myself, and sure enough, I stumbled at the beginning. After setup, the program can run normally and register to eureka registry, but after registration, it will be immediately unregistered. At first it was thought that a thread throw exception had hung, causing the service to be logged out. Then various debug checks, and finally found that the normal exit. As for the specific reasons, I will explain them in detail below.


Eureka Registry


Had the service side


Initial run result

Here registry startup is completely no problem, the problem is the client startup, startup log is as follows:

Looking at the log above, you can see that the service is initially registered with the Eureka registry normally, but then you notice that. Unregistering Application Eureka-client with EUREKA with status DOWN, followed by Shutting DOWN DiscoveryClient… The last Unregistering… .


Part of the solution

No obvious exceptions are found in the log, and then debug a wave, no errors are found. I googled it. Sure enough, there are friends who have encountered the same problem. Add a dependency to the CLIENT POM:

Let’s focus on the initialize method:


Other Solutions

In fact, the solution to this problem is very simple, as long as you can ensure the existence of a normal user thread on the line, it is possible that our own service providing applications do not need to use the Web container like Tomcat, but like other RPC services when called, You can create your own user thread (by default, all threads are user threads, unless you call setDaemon and set its daemon property to true). Then we can provide a status value to let it exit normally. Here’s a simple example:


conclusion

Through this step pit, or make up for their understanding of the JVM some blind spots. For example, a process does not terminate immediately as long as there is a user thread in the process. A process terminates immediately only if there are only daemons or no threads in the process. The most common gC-like thread of operation is the daemon thread, so that if our own business thread dies in the event of an exception, the process will terminate.


End