• 周四. 9月 21st, 2023

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

There is no listening port for springboot project startup

[db:作者]

1月 5, 2022

Premise

Our this SpringBoot Project dependence Dubbo、RocketMQ、Nacos etc. , Once in a while, I found that I didn’t register to SpringBootAdmin Inside , So check the reasons and search for information online , Find this article springboot The project started up normally , But the port cannot be monitored Events and solutions Similar to , After our project started, we also started the dead loop in the thread , as follows :

 @PostConstruct
public void init() {
log.info("============== initialization ==============start");
try {
// Here's a piece of code to open a dead loop in a thread 
xxx.xxx
} catch (Exception e) {
log.error("", e);
}
}

Refer to the article , It may be that the dead loop has been running here before the listening port, so we need to delay the start of this code to open the dead loop in multithreading , The changes are as follows :


@Slf4j
@Component
// Here's in order to number one 10
@Order(value = 10)
public class InitRunner implements ApplicationRunner {
@Override
public void run() {
log.info("============== initialization ==============start");
try {
// Here's a piece of code to open a dead loop in a thread 
xxx.xxx
} catch (Exception e) {
log.error("", e);
}
}
}

Test startup , Not yet. , It doesn’t seem to be the problem here . Continue to search for information and see this article SpringBoot built-in Tomcat Starting time , since Tomcat If you don’t listen to the port, debug it to see if the program doesn’t listen to the port .

Debug to critical 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();
}
}
}

there onRefresh(); Method calls the onRefresh() Method , Read notes :


/**
* Template method which can be overridden to add context-specific refresh work.
* Called on initialization of special beans, before instantiation of singletons.
* <p>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 , You can override this method to add context specific refresh work . In the initialization special bean Called when the , Then instantiate the singleton .

This implementation is empty .

Take a look at this method. It has the following result implementation :

It’s obvious that ServletWebServerApplicationContext The realization inside , But I failed to enter this method after debugging at breakpoint :

 @Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}

It’s just the empty method :

It’s magic .

Found a normal project , It’s really into ServletWebServerApplicationContext Inside the . And then the difference between the two projects , It’s a real Wulong .

I don’t rely on this project spring-boot-starter-web

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

Add this paragraph and try , Sure enough .

2021-04-15 10:33: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:33:52.546 [main] INFO [org.springframework.boot.StartupInfoLogger.logStarted: 61] - Started XXXMainApplication in 59.654 seconds (JVM running for 64.354)

Maybe I thought about this project at the beginning Dobbo Without the need for spring-web So I didn’t rely on . Later added SpringBootAdmin Monitoring this is to start the project port and then SpringBootAdmin To call the interface to query information , Then forget to add the dependency . It’s really a big oolong , But I also learned something from debugging the source code , I think the source code is really a good thing .

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注