Spring Boot 使用Jar打包发布, 并使用 Embedded Jetty/Tomcat 容器

Jar包发布

在项目pom.xml中, 如果继承了Spring Boot的starter parent, 那么默认已经包含打包需要的plugins了, 设置为jar就能直接打包成包含依赖的可执行的jar

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

如果不使用Spring Boot的starter parent, 那么需要在<build>中添加plugins, 这样也能打包包含依赖的可执行jar

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>{你的Application入口class}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

使用内置Tomcat容器

pom.xml配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

使用内置Jetty容器

pom.xml配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

在Application入口, 增加启动参数设置, 在Spring Boot 2.0之后, JettyServletWebServerFactory代替了JettyEmbeddedServletContainerFactory.

@Bean
public JettyServletWebServerFactory jettyEmbeddedServletContainerFactory(
        @Value("${server.port:9090}") final String port,
        @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads,
        @Value("${jetty.threadPool.minThreads:8}") final String minThreads,
        @Value("${jetty.threadPool.idleTimeout:60000}") final String idleTimeout) {
    JettyServletWebServerFactory jettyContainer = new JettyServletWebServerFactory();
    jettyContainer.setPort(Integer.valueOf(port));
    final QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(Integer.valueOf(maxThreads));
    threadPool.setMinThreads(Integer.valueOf(minThreads));
    threadPool.setIdleTimeout(Integer.valueOf(idleTimeout));

    jettyContainer.setThreadPool(threadPool);
    return jettyContainer;
}

这样在命令行中启动时, 可以通过命令行参数进行配置

$ java -jar your-project.jar --server.port=8081 --jetty.threadPool.maxThreads=300

.

.

猜你喜欢

转载自www.cnblogs.com/milton/p/9729010.html