Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the service cannot be registered in Nacos when the Spring Boot project is deployed under tomcat

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve the problem that the Spring Boot project can not be registered in Nacos when deployed under tomcat". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

problem

The Spring Boot project, which uses Nacos as the registry, is deployed to the server as a war package, and the startup project finds that the service cannot be registered in Nacos.

Analysis.

To check the source code, you need to start with the registration class of nacos. After searching, it is found that the nacos registration class NacosAutoServiceRegistration inherits the AbstractAutoServiceRegistration in Spring Cloud, but binds a listening event in AbstractAutoServiceRegistration to listen for the built-in container startup completion event. After listening to get the container port, register with the registry.

@ EventListener ({WebServerInitializedEvent.class}) public void bind (WebServerInitializedEvent event) {ApplicationContext context = event.getApplicationContext (); if (! (context instanceof ConfigurableWebServerApplicationContext) | |! "management" .equals (ConfigurableWebServerApplicationContext) context). GetServerNamespace ()) {this.port.compareAndSet (0, event.getWebServer (). GetPort ()); this.start ();}}

When using an external container, events cannot be heard, so automatic registration fails.

Solution

Spring Boot provides an ApplicationRunner interface to perform some initialization actions after the application is ready. Through this interface, we can implement the registration service after starting the project. Using this method, you need to configure the port number in the configuration file. If an application deploys many ports, each application needs to be configured, which is very inconvenient. Therefore, the external tomcat automatic setting port can be obtained. The test shows that the method is feasible.

Package com.bjbde.trade.configurer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration;import org.springframework.stereotype.Component;import javax.management.MBeanServer;import javax.management.ObjectName;import javax.management.Query;import java.lang.management.ManagementFactory;import java.util.Set @ Componentpublic class NacosConfig implements ApplicationRunner {@ Autowired (required = false) private NacosAutoServiceRegistration registration; @ Value ("${baseConfig.nacos.port}") Integer port; @ Override public void run (ApplicationArguments args) {if (registration! = null & & port! = null) {Integer tomcatPort = port; try {tomcatPort = new Integer (getTomcatPort ()) } catch (Exception e) {e.printStackTrace ();} registration.setPort (tomcatPort); registration.start ();}} / * get external tomcat port * / public String getTomcatPort () throws Exception {MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer () Set objectNames = beanServer.queryNames (new ObjectName ("*: type=Connector,*"), Query.match (Query.attr ("protocol"), Query.value ("HTTP/1.1")); String port = objectNames.iterator (). Next (). GetKeyProperty ("port"); return port;}} prompt

In the deployment project, we should pay attention to the version problems, such as Spring Boot 2.0.6 should be deployed in tomcat8 or above, the author ignored this version and deployed to tomcat7, resulting in project startup error. For questions related to Spring Boot and tomcat version, please refer to another blog post.

This is the end of how https://my.oschina.net/u/3193075/blog/3084074" solves the problem that the Spring Boot project cannot register services in Nacos when deployed under tomcat. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report