SpringBoot项目发布到服务器,是一件很简单的事情,但是发布到服务器上docker启动的tomcat容器中,有一些坑,需要注意。
下面完整的介绍一下:将SpringBoot web项目发布到docker启动的tomcat容器中。
Spring项目发布,请参考:docker---(3)docker 发布spring web项目
SpringBoot入门,请参考:SpringBoot--从0开始学SpringBoot
Docker入门,请参考: Docker---从零开始学Docker
1.pom.xml
将打包方式改为war包
<packaging>war</packaging>移除springBoot内置的tomcat,添加servlet-api
<dependency>在build中添加项目访问路径
<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>
<!--添加servlet-api-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
<scope>provided</scope>
</dependency>
<finalName>tweb</finalName>
(我第一次发布时,看了不下30篇文章,没有提到这一点,发布了20多次都没成功)
<build>2.自定义一个Servletinitializer类,继承SpringBootServletInitializer,用来启动上下文
<!--打包后的项目访问名称-->
<finalName>tweb</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(和启动类目录同级)
package com.jd;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Date 2018/1/30
* Description:启动spring的上下文
*/
public class Servletinitializer extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TwebApplication.class);
}
}
3.添加一个测试接口,供测试访问使用
package com.jd;然后,maven clean package,打包完成后,项目的target目录下就会有此war包。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 启动类
*/
@SpringBootApplication
@RestController
public class TwebApplication {
public static void main(String[] args) {
SpringApplication.run(TwebApplication.class, args);
}
@RequestMapping(value = "hello",method = RequestMethod.GET)
public String testGood(String name){
return "Welcome to tweb Mr."+ name;
}
@RequestMapping(value = "test",method = RequestMethod.GET)
public String test(){
return "Welcome to tweb";
}
}
此时,项目就准备好了,接下来,就是服务器上的docker的使用了。
4.把war包上传至服务器
看tomcat下有没有其他项目,有就删除,没有就拉倒,我这里有项目,我先删除一下
不熟悉docker基本命令的,可以参考:docker---(1)常用命令
root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash
root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/
root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager tweb tweb.war
root@91ebdd8d5c65:/usr/local/tomcat/webapps# rm tweb.war
root@91ebdd8d5c65:/usr/local/tomcat/webapps# rm -rf tweb
root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
然后,将war包上传至服务器
rz用不了,看这里:阿里云---阿里云服务器ECS安装jdk环境
root@iZuf6axmuekh1n14dwcufmZ:~# rz获取tomcat容器的id,我这里启动时名字叫tomcat2
root@iZuf6axmuekh1n14dwcufmZ:~# ls
tweb.war
root@iZuf6axmuekh1n14dwcufmZ:~# docker inspect -f '{{.Id}}' tomcat2复制war包到tomcat容器的webapps下,然后查看是否复制成功
91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854
root@iZuf6axmuekh1n14dwcufmZ:~# docker cp tweb.war 91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854:usr/local/tomcat/webapps复制成功后,重启tomcat即可
root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash
root@91ebdd8d5c65:/usr/local/tomcat# ls
LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work
root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/
root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager tweb tweb.war
root@91ebdd8d5c65:/usr/local/tomcat/webapps# exit
exit
root@iZuf6axmuekh1n14dwcufmZ:~#
root@iZuf6axmuekh1n14dwcufmZ:~# docker restart tomcat2查看状态,我这里启动时端口映射为8083
tomcat2
root@iZuf6axmuekh1n14dwcufmZ:~# docker ps -a去浏览器访问
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
996ff5cc0db6 webcenter/activemq "/app/run.sh" 24 hours ago Up 16 hours 1883/tcp, 5672/tcp, 0.0.0.0:8161->8161/tcp, 61613-61614/tcp, 0.0.0.0:61616->61616/tcp activemq
35350de323ca tomcat:latest "catalina.sh run" 41 hours ago Up 44 minutes 0.0.0.0:8085->8080/tcp tweb_tomcat
de67495c8a87 redis:latest "docker-entrypoint..." 12 days ago Exited (255) 22 hours ago 0.0.0.0:6379->6379/tcp redis1
91ebdd8d5c65 tomcat:latest "catalina.sh run" 2 weeks ago Up 28 seconds 0.0.0.0:8083->8080/tcp tomcat2
f3b0f76293f9 tomcat:latest "catalina.sh run" 2 weeks ago Exited (255) 16 hours ago 0.0.0.0:8080->8080/tcp tomcat1
4ce1634ce6f1 mysql:latest "docker-entrypoint..." 6 weeks ago Exited (255) 16 hours ago 0.0.0.0:3306->3306/tcp mysql1
root@iZuf6axmuekh1n14dwcufmZ:~# ^C