In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What to do when the SpringBoot project is started but there is no listening port? to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Premise
Our SpringBoot project relies on Dubbo, RocketMQ, Nacos, etc., and occasionally finds that it is not registered in SpringBootAdmin after startup. Therefore, we check the reasons and search for information on the Internet to find that the springboot project is running normally, but the events that cannot be heard by the port are similar to the solution. After starting, our project also starts a dead loop in the thread, as follows:
@ PostConstruct public void init () {log.info ("= initialization = start"); try {/ / there is a piece of code that opens the dead loop xxx.xxx} catch (Exception e) {log.error (", e);}} in the thread.
According to that article, it may be that the dead loop has been running all the time before waiting for the listening port, so you need to delay the start of the code that opens the dead loop in multiple threads, as follows:
@ Slf4j@Component// here comes 10th @ Order (value = 10) public class InitRunner implements ApplicationRunner {@ Override public void run () {log.info ("= initialization = start"); try {/ / there is a piece of code that opens the dead loop xxx.xxx} catch (Exception e) {log.error (", e) in the thread. }}}
The test starts, but it still doesn't work. It doesn't seem to be the problem here. Continue to query the information to see this article SpringBoot built-in Tomcat startup time, since Tomcat does not listen to the port, then debug to see where the program goes without listening to the port.
Debug to key code:
Class:AbstractApplicationContext @ Override public void refresh () throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {/ / Prepare this context for refreshing. PrepareRefresh (); / / Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory (); / / Prepare the bean factory for use in this context. PrepareBeanFactory (beanFactory); try {/ / Allows post-processing of the bean factory in context subclasses. PostProcessBeanFactory (beanFactory); / / Invoke factory processors registered as beans in the context. InvokeBeanFactoryPostProcessors (beanFactory); / / Register bean processors that intercept bean creation. RegisterBeanPostProcessors (beanFactory); / / Initialize message source for this context. InitMessageSource (); / / Initialize event multicaster for this context. InitApplicationEventMulticaster (); / / Initialize other special beans in specific context subclasses. OnRefresh (); / / Check for listener beans and register them. RegisterListeners (); / / Instantiate all remaining (non-lazy-init) singletons. FinishBeanFactoryInitialization (beanFactory); / / Last step: publish corresponding event. FinishRefresh () } catch (BeansException ex) {if (logger.isWarnEnabled ()) {logger.warn ("Exception encountered during context initialization -" + "cancelling refresh attempt:" + ex) } / / Destroy already created singletons to avoid dangling resources. DestroyBeans (); / / Reset 'active' flag. CancelRefresh (ex); / / Propagate exception to caller. Throw ex;} finally {/ / Reset common introspection caches in Spring's core, since we / / might not ever need metadata for singleton beans anymore... ResetCommonCaches ();}
The onRefresh (); method here calls the onRefresh () method in this class. See the note:
/ * Template method which can be overridden to add context-specific refresh work. * Called on initialization of special beans, before instantiation of singletons. *
This implementation is empty. * @ throws BeansException in case of errors * @ see # refresh () * / protected void onRefresh () throws BeansException {/ / For subclasses: do nothing by default. }
The general meaning of translation is:
Template method, which can be overridden to add context-specific refresh work. Called when a special bean is initialized, and then instantiates the singleton.
This implementation is empty.
Take a look at the following results of this method:
It's obvious that the implementation in ServletWebServerApplicationContext should be called, but I didn't get into this method when I went to the breakpoint to debug:
@ Override protected void onRefresh () {super.onRefresh (); try {createWebServer ();} catch (Throwable ex) {throw new ApplicationContextException ("Unable to start web server", ex);}}
Instead, the null method is directly adjusted:
That's amazing.
I found a normal project, and sure enough, I entered ServletWebServerApplicationContext. Then there is a difference between the two projects, which is really too own.
I don't rely on spring-boot-starter-web for this project.
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-logging
Try to add this paragraph, and it's all right.
2021-04-15 10 org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start 33 org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start 52.538 [main] INFO [org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start: 204]-Tomcat started on port (s): 8080 (http) with context path '2021-04-15 10 seconds 33 seconds [main] INFO [org.springframework.boot.StartupInfoLogger.logStarted: 61]-Started XXXMainApplication in 59.654 seconds (JVM running for 64.354)
Maybe I didn't rely on it because I thought I used Dobbo instead of spring-web for this project. Later, I added SpringBootAdmin monitoring which requires the project to launch the port and then SpringBootAdmin to call the interface to query information, and then forgot to add this dependency. It's really a big own, but I also learned something from debugging the source code. I feel that the source code is really a good thing.
The answer to the question about how to start the SpringBoot project without listening port is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.