springboot项目jar包改war包, 适配外置tomcat和金蝶中间件


前言

提示:这里主要讲适配tomcat, 金蝶基本与tomcat没有太大的差别:


一、修改pom.xml

修改打包方式

<!--改为war方式-->
    <packaging>war</packaging>

排除内置tomcat,

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
                相当于compile,但是打包阶段做了exclude操作-->
            <scope>provided</scope>
    </dependency>

删除内置undertow容器

 <!-- 删除掉以下的undertow容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

新增maven插件,忽略打包web.xml

		 <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-war-plugin</artifactId>
             <version>3.2.3</version>
             <configuration>
                 <failOnMissingWebXml>false</failOnMissingWebXml>
                 <!--如果依赖本地jar, 可以配置本地jar的包路径-->
                 <webResources>
                        <resource>
                            <directory>${project.basedir}/lib</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
             </configuration>
         </plugin>

二、修改配置文件

1.config中心的配置文件无法扫描到, 所以需要将外面的配置文件全部迁移到application.yml和bootstrap.yml中

代码如下(示例):

    nacos:
      config:
        server-addr: ${ucsp.nacos.universal-addr}
        file-extension: yml
        shared-dataids: common.yml
        refreshable-dataids: common.yml
      discovery:
        server-addr: ${ucsp.nacos.universal-addr}
        namespace: ${ucsp.nacos.namespace} 

三.项目启动入口改造

启动类同级目录下新建类:

package com.central;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;


public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        //此处的GatewayApp.class为带有@SpringBootApplication注解的启动类
        return builder.sources(GatewayApp.class);
    }
}

四. 添加nacos配置

提示:如果服务需要注册到nacos中的话, 使用外置tomcat可能无法注册, 需要添加以下配置:
来源于另一篇博文

package com.central.gateway.config;

import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.Set;


@Component
@Slf4j
public class NacosConfig {

    @Autowired
    private NacosRegistration registration;

    @Autowired
    private NacosAutoServiceRegistration nacosAutoServiceRegistration;

    @Value("${server.port}")
    String serverPort;

    @PostConstruct
    public void registerInstance() throws Exception {
        if (registration != null && serverPort != null) {
            String tomcatPort = serverPort;
            try {
                tomcatPort = getPort();
            } catch (Exception e) {
                log.warn("获取外部Tomcat端口异常:", e);
            }
            registration.setPort(Integer.parseInt(tomcatPort));
            nacosAutoServiceRegistration.start();
        }
    }

    /**
     * 获取外部tomcat端口
     */
    public String getPort() {
        try {
            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;
        } catch (Exception ex) {
            log.error("NacosRegister.getPort()动态获取端口异常:", ex.toString());
            return serverPort;
        }
    }
}


  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值