Docker---(5)Docker 部署SpringBoot web项目

注:博主系统是ubuntu。


SpringBoot项目发布到服务器,是一件很简单的事情,但是发布到服务器上docker启动的tomcat容器中,有一些坑,需要注意。

下面完整的介绍一下:将SpringBoot web项目发布到docker启动的tomcat容器中。

Spring项目发布,请参考:docker---(3)docker 发布spring web项目

SpringBoot入门,请参考:SpringBoot--从0开始学SpringBoot

Docker入门,请参考: Docker---从零开始学Docker

1.pom.xml

将打包方式改为war包

[java]  view plain  copy
  1. <packaging>war</packaging>  
移除springBoot内置的tomcat,添加servlet-api

[java]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-web</artifactId>  
  4.     <!--移除内置的tomcat-->  
  5.     <exclusions>  
  6.         <exclusion>  
  7.             <groupId>org.springframework.boot</groupId>  
  8.             <artifactId>spring-boot-starter-tomcat</artifactId>  
  9.         </exclusion>  
  10.     </exclusions>  
  11. </dependency>  
  12. <!--添加servlet-api-->  
  13. <dependency>  
  14.     <groupId>org.apache.tomcat</groupId>  
  15.     <artifactId>tomcat-servlet-api</artifactId>  
  16.     <version>8.0.36</version>  
  17.     <scope>provided</scope>  
  18. </dependency>  
在build中添加项目访问路径
[java]  view plain  copy
  1. <finalName>tweb</finalName>  

(我第一次发布时,看了不下30篇文章,没有提到这一点,发布了20多次都没成功)

[java]  view plain  copy
  1. <build>  
  2.     <!--打包后的项目访问名称-->  
  3.     <finalName>tweb</finalName>  
  4.     <plugins>  
  5.         <plugin>  
  6.             <groupId>org.springframework.boot</groupId>  
  7.             <artifactId>spring-boot-maven-plugin</artifactId>  
  8.         </plugin>  
  9.     </plugins>  
  10. </build>  
2.自定义一个Servletinitializer类,继承SpringBootServletInitializer,用来启动上下文

(和启动类目录同级)

[java]  view plain  copy
  1. package com.jd;  
  2.   
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  4. import org.springframework.boot.builder.SpringApplicationBuilder;  
  5. import org.springframework.boot.web.support.SpringBootServletInitializer;  
  6. import org.springframework.context.annotation.ComponentScan;  
  7. import org.springframework.context.annotation.Configuration;  
  8.   
  9. /** 
  10.  * Date 2018/1/30 
  11.  * Description:启动spring的上下文 
  12.  */  
  13.   
  14. public class Servletinitializer extends SpringBootServletInitializer{  
  15.   
  16.     @Override  
  17.     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
  18.         return builder.sources(TwebApplication.class);  
  19.     }  
  20. }  


3.添加一个测试接口,供测试访问使用

[java]  view plain  copy
  1. package com.jd;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. /** 
  10.  * 启动类 
  11.  */  
  12. @SpringBootApplication  
  13. @RestController  
  14. public class TwebApplication {  
  15.   
  16.     public static void main(String[] args) {  
  17.         SpringApplication.run(TwebApplication.class, args);  
  18.     }  
  19.   
  20.   
  21.     @RequestMapping(value = "hello",method = RequestMethod.GET)  
  22.     public String testGood(String name){  
  23.         return "Welcome to tweb Mr."+ name;  
  24.     }  
  25.   
  26.     @RequestMapping(value = "test",method = RequestMethod.GET)  
  27.     public String test(){  
  28.         return "Welcome to tweb";  
  29.     }  
  30. }  
然后,maven clean package,打包完成后,项目的target目录下就会有此war包。

此时,项目就准备好了,接下来,就是服务器上的docker的使用了。

4.把war包上传至服务器

看tomcat下有没有其他项目,有就删除,没有就拉倒,我这里有项目,我先删除一下

不熟悉docker基本命令的,可以参考:docker---(1)常用命令

[java]  view plain  copy
  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash  
  2. root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/  
  3. root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls  
  4. ROOT  docs  examples  host-manager  manager  tweb  tweb.war  
  5. root@91ebdd8d5c65:/usr/local/tomcat/webapps# rm tweb.war   
  6. root@91ebdd8d5c65:/usr/local/tomcat/webapps# rm -rf tweb  
  7. root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls  
  8. ROOT  docs  examples  host-manager  manager  

然后,将war包上传至服务器

rz用不了,看这里:阿里云---阿里云服务器ECS安装jdk环境

[java]  view plain  copy
  1. root@iZuf6axmuekh1n14dwcufmZ:~# rz   
  2. root@iZuf6axmuekh1n14dwcufmZ:~# ls  
  3. tweb.war  
获取tomcat容器的id,我这里启动时名字叫tomcat2

[java]  view plain  copy
  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker inspect -f '{{.Id}}' tomcat2  
  2. 91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854  
复制war包到tomcat容器的webapps下,然后查看是否复制成功

[java]  view plain  copy
  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker cp tweb.war 91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854:usr/local/tomcat/webapps  
  2. root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash  
  3. root@91ebdd8d5c65:/usr/local/tomcat# ls  
  4. LICENSE  NOTICE  RELEASE-NOTES  RUNNING.txt  bin  conf  include  lib  logs  native-jni-lib  temp  webapps  work  
  5. root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/  
  6. root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls  
  7. ROOT  docs  examples  host-manager  manager  tweb  tweb.war  
  8. root@91ebdd8d5c65:/usr/local/tomcat/webapps# exit  
  9. exit  
  10. root@iZuf6axmuekh1n14dwcufmZ:~#   
复制成功后,重启tomcat即可

[java]  view plain  copy
  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker restart tomcat2  
  2. tomcat2  
查看状态,我这里启动时端口映射为8083

[java]  view plain  copy
  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker ps -a  
  2. CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                      PORTS                                                                                   NAMES  
  3. 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  
  4. 35350de323ca        tomcat:latest        "catalina.sh run"        41 hours ago        Up 44 minutes               0.0.0.0:8085->8080/tcp                                                                  tweb_tomcat  
  5. de67495c8a87        redis:latest         "docker-entrypoint..."   12 days ago         Exited (25522 hours ago   0.0.0.0:6379->6379/tcp                                                                  redis1  
  6. 91ebdd8d5c65        tomcat:latest        "catalina.sh run"        2 weeks ago         Up 28 seconds               0.0.0.0:8083->8080/tcp                                                                  tomcat2  
  7. f3b0f76293f9        tomcat:latest        "catalina.sh run"        2 weeks ago         Exited (25516 hours ago   0.0.0.0:8080->8080/tcp                                                                  tomcat1  
  8. 4ce1634ce6f1        mysql:latest         "docker-entrypoint..."   6 weeks ago         Exited (25516 hours ago   0.0.0.0:3306->3306/tcp                                                                  mysql1  
  9. root@iZuf6axmuekh1n14dwcufmZ:~# ^C  
去浏览器访问

猜你喜欢

转载自blog.csdn.net/weixin_39800144/article/details/79214978