一、场景:分布式测试、开发内存消耗比较大采用统一发布,Tomcat核心配置参考
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<!-- APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<!-- <Service name="Catalina">-->
<!-- <Connector port="8080" protocol="HTTP/1.1"-->
<!-- connectionTimeout="20000"-->
<!-- redirectPort="8443" />-->
<!-- <Engine name="Catalina" defaultHost="localhost">-->
<!-- <Realm className="org.apache.catalina.realm.LockOutRealm">-->
<!-- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"-->
<!-- resourceName="UserDatabase"/>-->
<!-- </Realm>-->
<!-- <Host name="localhost" appBase="webapps"-->
<!-- unpackWARs="true" autoDeploy="true">-->
<!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"-->
<!-- prefix="localhost_access_log" suffix=".txt"-->
<!-- pattern="%h %l %u %t "%r" %s %b" />-->
<!-- </Host>-->
<!-- </Engine>-->
<!-- </Service>-->
<!-- schedule-service-->
<Service name="CatalinaScheduleService">
<Connector port="9009" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
<Engine name="CatalinaScheduleService" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="scheduleService"
unpackWARs="true" autoDeploy="true">
<Context path="/app"
docBase="D:\ProductDoc\apache-tomcat-9.0.63-windows-x64\apache-tomcat-9.0.63\wars\schedule-service-1.0.1.war"
debug="0"
privileged="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b"/>
</Host>
</Engine>
</Service>
<!-- common-message-service-->
<Service name="CatalinaCommonMessageService">
<Connector port="9015" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
<Engine name="CatalinaCommonMessageService" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="commonMessageService"
unpackWARs="true" autoDeploy="true">
<Context path=""
docBase="D:\ProductDoc\apache-tomcat-9.0.63-windows-x64\apache-tomcat-9.0.63\wars\common-message-service-0.0.1.war"
debug="0"
privileged="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b"/>
</Host>
</Engine>
</Service>
<!-- crms-service-->
<Service name="CatalinaCrmsService">
<Connector port="9010" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
<Engine name="CatalinaCrmsService" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="crmsService"
unpackWARs="true" autoDeploy="true">
<Context path="/v1"
docBase="D:\ProductDoc\apache-tomcat-9.0.63-windows-x64\apache-tomcat-9.0.63\wars\crms-service-1.0.war"
debug="0"
privileged="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b"/>
</Host>
</Engine>
</Service>
</Server>
二、springboot打包成war包
1、在pom配置文件中指定打包为war类型,并移除web中自带的tomcat
<packaging>war</packaging>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
2、修改build配置
<build>
<!-- 打包后的服务名称 -->
<finalName>base-service-1.0.1</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--可以不用添加web.xml-->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-deploy-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<!--deploy 时忽略 model-->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
3、修改启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4、新增Tomcat注册到nacos配置
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
@Component
public class NacosListener implements ApplicationListener<ApplicationReadyEvent> {
@Resource
private NacosRegistration registration;
@Resource
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
String property = event.getApplicationContext().getEnvironment().getProperty("server.port");
if (!StringUtils.isEmpty(property)) {
registration.setPort(Integer.parseInt(property));
}
nacosAutoServiceRegistration.start();
}
@EventListener(ApplicationReadyEvent.class)
public void onWebServerReady(ApplicationReadyEvent event) {
String property = event.getApplicationContext().getEnvironment().getProperty("server.port");
if (!StringUtils.isEmpty(property)) {
registration.setPort(Integer.parseInt(property));
}
nacosAutoServiceRegistration.start();
}
}
三、打包部署
1、清理项目并打包
2、拷贝jar包到tomcat的webapps目录下,修改一下包名(包名即路由,直接打jar包有端口但是war就只能通过tomcat端口+包名来访问到服务)
3、进入tomcat的bin目录在文件地址栏里直接输入cmd回车
4、输入 startup启动(shutdown关闭命令)
5、查看应用输入localhost:8080 然后再输入用户名admin密码admin查看app是否正常启动
6、页面访问接口成功到此一个springboot打包成war的简单工作就完成了
四、idea2021配置tomcat
1、点击编辑配置
2、在第4步选择刚下载的tomcat解压的目录
3、点击Arfact...添加可运行的war包项目
4、直接启动
五、Tomcat下载及简单配置(jdk配置略本文采用的是jdk1.8)
1、官网(Apache Tomcat® - Welcome!)下载Tomcat相关版本,这里我下载的是tomcat9+win64做演示用,当然你可以根据自己的系统和tomcat版本要求自己下载。
2、演示账号配置,因为我下的是解压即用的版本所以解压后直接进入tomcat的bin目录下操作的,你也可以进行环境变量配置。我们先只加一个管理员账户其他的啥也不用配置
六、温馨提示:
1、 centos启动时报"-bash: ./startup.sh: Permission denied"可以运行(chmod u+x *.sh)命令赋予执行权限.
2、tomcat读取nacos配置出现乱码无法发布请在tomcat/bin/catalina.bat(.sh根据tomcat所在系统选择更改脚本)新增 set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dfile.encoding=UTF-8"
3、如果出现war包无法启动,可以通过日志分析查看原因:比如引用的jar包版本冲突可以删除启不启的项目下WEB-INF里lib相关冲突的jar包