利用tomcat7-maven-plugin插件来实现SpringBoot工程的远程自动化部署,每次都拷贝war包太费劲。SpringBoot版本2.1.3.RELEASE,部署服务器tomcat7 apache-tomcat-7.0.77。
1.tomcat服务器配置
配置apache-tomcat-7.0.77/conf/tomcat-users.xml,tomcat特殊权限用户,其中角色为manager-gui为以后需要使用的用户
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="1234" roles="manager-script"/>
<user username="root" password="1234" roles="manager-gui"/>
目录/apache-tomcat-7.0.77/conf/Catalina/localhost下新建manager.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context privileged="true" antiResourceLocking="false"
docBase="${catalina.home}/webapps/manager">
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>
2.SpringBoot工程配置
pom.xml文件配置
修改打包方式为war
<groupId>com.example</groupId>
<artifactId>vehicle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vehicle</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
打包时去除springboot tomcat依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 发布时去除tomcat依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
添加依赖javax.servlet-api
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
配置tomcat7-maven-plugin插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://10.3.10.144:8888/manager/text</url>
<username>admin</username>
<password>1234</password>
<update>true</update>
<path>/</path>
</configuration>
</plugin>
url为远程tomcat服务器地址,username,password为tomcat-users.xml中manager-gui用户名以及密码
path为工程上传服务器的根路径,即相对于webapps路径
新建类SpringBootStartApplication
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(DemoApplication.class);
}
}
3.打包远程部署
mvn tomcat7:deploy
日志信息:
[INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ vehicle ---
[INFO] Deploying war to http://10.3.10.144:8888/
Uploading: http://10.3.10.144:8888/manager/text/deploy?path=%2F&update=true
Uploaded: http://10.3.10.144:8888/manager/text/deploy?path=%2F&update=true (58710 KB at 11725.5 KB/sec)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.813 s
[INFO] Finished at: 2019-04-19T13:56:12+08:00
[INFO] Final Memory: 52M/318M
[INFO] ------------------------------------------------------------------------
错误处理由于tomcat7中el-api.jar版本过低,缺少类ELManager,可能会报错
java.lang.NoClassDefFoundError: javax/el/ELManager
下载el-api.ajr https://mvnrepository.com/artifact/javax.el/javax.el-api/3.0.0 替换掉tomcat lib目录下el-api.jar即可解决。
配置maven Profile实现配置环境切换
由于内外网限制等,使得本地开发环境与远程服务器环境不一致,每次都需要手动切换环境太繁琐,这里使用profile来动态切换环境变量,结合配置文件以及pom中profile
profile
<profiles>
<profile>
<id>remote</id>
<properties>
<spring-data-elasticsearch-cluster-nodes>192.168.1.12:9300</spring-data-elasticsearch-cluster-nodes>
<spring-datasource-primary-jdbc-url>jdbc:mysql://192.168.1.18:3306/ac-device?characterEncoding=utf8&serverTimezone=UTC</spring-datasource-primary-jdbc-url>
<spring-datasource-primary-username>root</spring-datasource-primary-username>
<spring-datasource-primary-password>Smart@001</spring-datasource-primary-password>
<spring-datasource-secondary-jdbc-url>jdbc:mysql://192.168.1.18:3306/warehouse?characterEncoding=utf8&serverTimezone=UTC</spring-datasource-secondary-jdbc-url>
<spring-datasource-secondary-username>root</spring-datasource-secondary-username>
<spring-datasource-secondary-password>Smart@001</spring-datasource-secondary-password>
</properties>
</profile>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring-data-elasticsearch-cluster-nodes>10.3.10.147:9300</spring-data-elasticsearch-cluster-nodes>
<spring-datasource-primary-jdbc-url>jdbc:mysql://127.0.0.1:3306/ac-device?characterEncoding=utf8&serverTimezone=UTC</spring-datasource-primary-jdbc-url>
<spring-datasource-primary-username>root</spring-datasource-primary-username>
<spring-datasource-primary-password>1234</spring-datasource-primary-password>
<spring-datasource-secondary-jdbc-url>jdbc:mysql://127.0.0.1:3306/warehouse?characterEncoding=utf8&serverTimezone=UTC</spring-datasource-secondary-jdbc-url>
<spring-datasource-secondary-username>root</spring-datasource-secondary-username>
<spring-datasource-secondary-password>1234</spring-datasource-secondary-password>
</properties>
</profile>
</profiles>
配置文件:
#服务器开放端口
server.port=8888
spring.data.elasticsearch.repositories.enabled=true
#es连接地址
spring.data.elasticsearch.cluster-nodes = @spring-data-elasticsearch-cluster-nodes@
#es集群名称
spring.data.elasticsearch.cluster-name = elasticsearch
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.cache.use_query_cache=true
spring.jpa.hibernate.cache.use_second_level_cache=true
spring.jpa.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.datasource.primary.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.primary.jdbc-url=@spring-datasource-primary-jdbc-url@
spring.datasource.primary.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.primary.username=@spring-datasource-primary-username@
spring.datasource.primary.password=@spring-datasource-primary-password@
# JPA hikari 数据库连接池配置
spring.datasource.primary.hikari.maximum-pool-size=20
spring.datasource.primary.hikari.minimum-idle=5
spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.secondary.jdbc-url=@spring-datasource-secondary-jdbc-url@
spring.datasource.secondary.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.username=@spring-datasource-secondary-username@
spring.datasource.secondary.password=@spring-datasource-secondary-password@
# JPA hikari 数据库连接池配置
spring.datasource.secondary.hikari.maximum-pool-size=20
spring.datasource.secondary.hikari.minimum-idle=5
这里一定要注意springboot中占位符为@key@,而非${}
查看spring-boot-starter-parent可知
可以将配置文件中${}使用@@来替换
或者覆盖springboot中占位符
期间遇到各种错误,是由于变量命名以及占位符使用不正确造成的
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring-data-elasticsearch-cluster-nodes' in value "${spring-data-elasticsearch-cluster-nodes}"
Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'spring.data.elasticsearch.cluster-nodes' in property definitions
参考链接: