3

SpringBoot部署到外部Tomcat无法注册到Nacos服务端 - 东北小狐狸

 1 year ago
source link: https://www.cnblogs.com/hellxz/p/17223646.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SpringBoot部署到外部Tomcat无法注册到Nacos服务端 - 东北小狐狸 - 博客园

近期做一个项目投标演示(POC)环境支持,需要集成Nacos服务端。考虑到现有项目中已经有了Nacos相关依赖,那还不简单?新建个服务端,配置几下重启不就搞定了吗?然而事情远没有想得这么简单。同样的代码在我本地IDE里运行就能注册成功,在演示环境 Tomcat+War 部署就不行了。

经过远程Debug代码,发现Nacos客户端的线程都有启动,却没有注册成功。

想到可能与Tomcat部署模式有关系,就去查了官方issueStackOverFlow

The event is published as part of Spring Boot starting the embedded Tomcat instance. If you're deploying to an external container, there's no embedded container to start and, therefore, no event is published. – Andy Wilkinson

大致是说只有当 Spring Boot 启动内嵌 Tomcat 成功后,才会发布 WebServerInitializedEvent 事件。而Nacos客户端在等这个事件出现才会向服务端注册自己。又因部署在外部Tomcat中就不会初始化内嵌Tomcat,也就没触发这个事件。

所以解决方法就是将Nacos等事件的部分代码调用下,让他们启动注册。

Nacos的自动注册类是 NacosAutoServiceRegistration,它继承Spring Cloud的AbstractAutoServiceRegistration,在AbstractAutoServiceRegistration等的 bind(WebServerInitializedEvent)方法监听事件,设置端口号并启动注册。这里边 this.port 是从事件中获取的,需要我们自行获取。

1149398-20230316173321457-1069480923.png

设置port的位置可见,是从org.springframework.cloud.client.serviceregistry.Registration中取到的,给它设置一下就可以了。

1149398-20230316174159515-1924891181.png

我写了一个完整的配置类放到了该ISSUE下边,这里直接贴在下边。

import java.lang.management.ManagementFactory;import java.util.Set; import javax.annotation.PostConstruct;import javax.management.MBeanServer;import javax.management.MalformedObjectNameException;import javax.management.ObjectName;import javax.management.Query; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment; import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;import com.alibaba.cloud.nacos.registry.NacosRegistration; @Configurationpublic class NacosWarDeployConfig { private static final Logger logger = LoggerFactory.getLogger(NacosWarDeployConfig.class); @Autowired private Environment env; @Autowired private NacosRegistration registration; @Autowired private NacosAutoServiceRegistration nacosAutoServiceRegistration; @PostConstruct public void nacosServerRegister() { if (registration != null) { registration.setPort(getTomcatPort()); nacosAutoServiceRegistration.start(); } } public int getTomcatPort() { try { return getProvideTomcatPort(); } catch (Exception e) { logger.warn("obtain provide tomcat port failed, fallback to embeded tomcat port."); } return getEmbeddedTomcatPort(); } private int getProvideTomcatPort() throws MalformedObjectNameException, NullPointerException { MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> 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 Integer.valueOf(port); } private int getEmbeddedTomcatPort() { return env.getProperty("server.port", Integer.class, 8080); } }

经过我这一波操作问题终于解决了。我是Hellxz,不在进坑就在爬坑的路上。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK