zoukankan      html  css  js  c++  java
  • 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
    

    .

    .

  • 相关阅读:
    Python异常处理
    python抽象类
    python传参*和**的区别
    python 多重继承构造函数调用顺序
    linux 自启动 | 三种方式自启动
    Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP
    Go 基础学习笔记 (5)| 数据类型说明与使用
    GO 基础学习笔记(4)| 参数传递
    生活问题 | 对华为畅玩手机5X进行升级
    markdown 语法
  • 原文地址:https://www.cnblogs.com/milton/p/9729010.html
Copyright © 2011-2022 走看看