美文网首页
Spring Boot - Tomcat容器部署

Spring Boot - Tomcat容器部署

作者: 番薯IT | 来源:发表于2017-11-08 21:56 被阅读1834次

SpringBoot - Hello World中我们简单的结识了SpringBoot

注意到我们是直接执行main函数就把项目启动起来了,并且部署到了tomcat容器。但是我们从头到尾都没有配置过任何的容器,更何况是tomcat。而且,我们一般的线上启动web项目,可不是直接运行main函数的。

一般情况,都是把项目打包成war包,然后丢到tomcat,再启动tomcat,现在SpringBoot这样玩,那怎么上线?

这些就是接下来要讨论的。

环境工具

  • IDEA
  • Maven3
  • JDK1.8
  • Tomcat8

当前Spring Boot 最新稳定版本为1.5.8

创建项目

打开IDEA -> new Project -> Spring Initializr -> 填写Group,Article -> 选择Web,勾选Web -> Next -> Finish

也可以基于Helloworld那个项目

设置banner

我们发现在SpringBoot启动的时候会展示一个图案

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

修改banner

我们可以将其更换为我们自己想要的图案:

  • src/main/resources下面新建一个banner.txt文件
  • 将我们想更换的图案放入其中,比如我更换为下图:
.______        ______        ___       ______  __    __  
|   _  \      /  __  \      /   \     /      ||  |  |  | 
|  |_)  |    |  |  |  |    /  ^  \   |  ,----'|  |__|  | 
|      /     |  |  |  |   /  /_\  \  |  |     |   __   | 
|  |\  \----.|  `--'  |  /  _____  \ |  `----.|  |  |  | 
| _| `._____| \______/  /__/     \__\ \______||__|  |__| 
  • 重新启动项目,我们就能看到新的图案了。

关闭banner

有时候我们并不想看到这个banner,我们想关闭它。

只需要我们将main函数里的内容修改为下面的代码就可以了:

SpringApplication springApplication = new SpringApplication(SpringBootContainerApplication.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);

配置Tomcat

之所以我们不需要引入tomcat是因为SpringBoot帮我们内嵌了tomcat容器。我们在引入SpringBoot的时候,同时依赖引入了tomcat

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>1.5.8.RELEASE</version>
    <scope>provided</scope>
</dependency>

容器相关的配置(不只是tomcat)都在org.springframework.boot.autoconfigure.web.ServerProperties配置类中有定义。所以我们要修改内嵌tomcat容器相关的配置,只需要在application.properties中配置即可。

## 容器端口号,默认8080
server.port=8280
## 上下文路径,默认/
server.context-path=/tutorial
## tomcat编码,默认UTF-8
server.tomcat.uri-encoding=UTF-8

具体的配置可以查看源码。

新建DemoController

新建DemoController,代码如下:

@RestController
@RequestMapping("/demo")
public class DemoController {

    @GetMapping
    public List<Map<String, String>> get(){
        List<Map<String, String>> demos = new ArrayList<>();
        Map<String, String > demo1 = new HashMap<>();
        demo1.put("roach", "小强");
        demo1.put("crow", "乌鸦");
        demos.add(demo1);

        Map<String, String> demo2 = new HashMap<>();
        demo2.put("lion", "狮子");
        demo2.put("tiger", "老虎");
        demos.add(demo2);

        return demos;
    }
}
  • 启动项目
  • 在浏览器中输入http://localhost:8280/tutorial/demo,查看结果
  • 输出如下结果:
[
    {
        "roach": "小强",
        "crow": "乌鸦"
    },
    {
        "tiger": "老虎",
        "lion": "狮子"
    }
]

可见我们的配置是生效了的。

使用外置tomcat

  • pom.xml中的<packaging>jar</packaging>修改成<packaging>war</packaging>,即修改打包方式。

  • SpringBootContainerApplication类(注解@SpringBootApplication所在的类)继承org.springframework.boot.web.support.SpringBootServletInitializer类,并重写configure方法:

@SpringBootApplication
public class SpringBootContainerApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}
  • IDEA配置tomcat,配置项目到tomcat。具体如何配置项目到tomcat,这里不会涉及。当然你也可以先打war包,然后将war包丢到tomcat上。

需要注意的是:application.properties中对容器的配置只对内嵌的有效,对外置的容器是没有用的。

内嵌tomcat依赖设置

既然用外置的tomcat,那内嵌的就不需要了吧,就可以把依赖去掉了呀!
是的,我们只要把依赖去掉就可以了。

  • 去依赖设置
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

这样配置我们就能去掉内部的tomcat依赖了

  • 添加servlet-api依赖

去掉tomcat的依赖,但是我们还是要servlet的依赖的,不然我们的项目是启动不了的。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

内外tomcat皆可

有朋友说,我有时想用内嵌的有时用外置的,看我心情。当然可以,我们只需要单独将tomcat的依赖加进来就可以。这样就能覆盖spring-boot-starter-web依赖进来的tomcat。

  • 在pom.xml中加入如下配置

设置scope是因为我们只需要在编译的时候用到,打包的时候就不需要了。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>1.5.8.RELEASE</version>
    <scope>provided</scope>
</dependency>
  • 在项目根目录下执行:mvn spring-boot:run,就能使用内置的tomcat运行项目。

项目源码

参考

《Java EE开发的颠覆者 Spring Boot 实战》

把spring-boot项目部署到tomcat容器中

相关文章

网友评论

      本文标题:Spring Boot - Tomcat容器部署

      本文链接:https://www.haomeiwen.com/subject/ovksmxtx.html