Spring Boot部署到tomcat下无法在Nacos中注册服务

问题
使用Nacos作为注册中心的Spring Boot项目,以war包形式部署到服务器上,启动项目发现该服务无法在Nacos中注册。

分析
查看源码,需从nacos的注册类找起,查找后发现,nacos注册类NacosAutoServiceRegistration继承了Spring Cloud中AbstractAutoServiceRegistration, 而在AbstractAutoServiceRegistration中绑定了一个监听事件,监听内置容器启动完成事件,监听到获取容器端口后向注册中心注册。

@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();
        }

}

而使用外部容器时,不能监听到事件,所以自动注册失败。

解决方案
Spring Boot提供了ApplicationRunner接口,是在应用起好之后执行一些初始化动作。通过这个接口我们可以实现启动项目后注册服务。使用这种方法,需要在配置文件中配置端口号,如果一个应用部署很多端口,每个应用都要配置,很不方便。故可获取外部tomcat自动设置端口。经测试,方法可行
1.引入jar, 并将pom.xml文件 jar 改为 war

        <!-- 部署到外部的tomcat中运行 -->
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

2.修改启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
@EnableTransactionManagement
@EnableScheduling
public class ServerApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(ServerApplication.class, args);
	}

	/*
	 *  配置外部tommcat
	 */
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	   // 注意这里要指向原先用main方法执行的Application启动类
        return application.sources(ServerApplication.class);
    }
}

3.添加配置文件,外部容器启动后向nacos注册服务


import java.lang.management.ManagementFactory;
import java.util.Set;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;

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.stereotype.Component;

import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description 项目打包war情况下部署外部tomcat,需该方式注册nacos
 * @Classname NacosRegisterOnWar
 * @Author dct
 * @Date 2021-08-23 15:09
 * @Version V1.0
 */
@Component
@Slf4j
public class NacosRegisterOnWar implements ApplicationRunner {
 
    @Autowired
    private NacosRegistration registration;
    
    @Autowired
    private NacosAutoServiceRegistration nacosAutoServiceRegistration;
 
    @Value("${server.port}")
    Integer port;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (registration != null && port != null) {
            Integer tomcatPort = port;
            try {
                tomcatPort = new Integer(getTomcatPort());
            } catch (Exception e) {
                log.warn("获取外部Tomcat端口异常:",e);
            }
            registration.setPort(tomcatPort);
            nacosAutoServiceRegistration.start();
        }
    }
 
    /**
     *	获取外部tomcat端口
     */
    public String getTomcatPort() throws Exception {
        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 port;
    }
}

提示
在部署项目要注意版本问题,如Spring Boot 2.0.6应该部署在tomcat8以上版本,笔者就曾忽略这个版本而部署到tomcat7中,导致项目启动报错。 Spring Boot与tomcat版本对应的问题可参照另外一篇博客
https://my.oschina.net/u/3193075/blog/3084074

4.创建web.xml文件
因笔者用的是eclipse, 故在项目中创建目录及文件
在这里插入图片描述

web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>cloud-server</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

余下就是maven install 打包, 将war包部署tomcat即可

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值