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
    

    .

    .

  • 相关阅读:
    centos7.4 系统安装指导
    win10下硬盘安装CentOS7
    CentOs7.X下配置FTP
    pyspider 安装使用过程的一些坑
    .Net Core 商城微服务项目系列(十三):搭建Log4net+ELK+Kafka日志框架
    .Net Core自动化部署系列(二):使用Jenkins打造镜像发布流水线
    Kubernetes 系列(六):Kubernetes部署Prometheus监控
    Kubernetes 系列(五):Prometheus监控框架简介
    .Net Core 商城微服务项目系列(十二):使用k8s部署商城服务
    Kubernetes 系列(四):使用Traefik访问.net core api
  • 原文地址:https://www.cnblogs.com/milton/p/9729010.html
Copyright © 2011-2022 走看看