文章目录
前言
提示:这里主要讲适配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;
}
}
}