zoukankan      html  css  js  c++  java
  • spring boot 部署为jar

    前言

    一直在ide中敲代码,使用命令行mvn spring-boot:run或者gradlew bootRun来运行spring boot项目。想来放到prod上面也应该很简单。然而今天试了下,各种问题。最大错误是1.4的bug:

    Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
    

    这个错误使我一直以为自己的代码有问题。找了半天没找到,最后想既然命令行可以运行ok,那么一个fat jar失败肯定不对了。于是上github去问,以为石沉大海准备睡觉的。想不到的是spring boot的成员秒回,找到问题是1.4版本中hibernate自动配置的问题,想我根本不需要hibernate,删除就可以了。

    github 原问题:https://github.com/spring-projects/spring-boot/issues/6927



    部署为可运行的jar

    spring boot已经尽可能把需要配置的东西自动化了,我还傻傻的像以前springmvc那样补充各种配置,比如加一个数据源druid。然而大可不必,使用默认的就好,等需求不满足的时候,在进行修改就可以了。

    同样的,既然内置的tomat可以很好的运行,为啥非要自己手动部署war包?

    在gradle build或者maven package之后,会得到一个jar,这个jar是spring boot修改过的jar,可以直接运行。
    运行方式:
    maven添加plugin

          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${springboot.version}</version>
          </plugin>
    

    然后执行打包

    mvn clean install package spring-boot:repackage
    
    java -jar xxxx.jar
    

    看到比较好的linux脚本:

    start.sh

    #!/bin/sh
    
    rm -f tpid
    
    nohup java -jar xx.jar --spring.profiles.active=dev > /dev/null 2>&1 &
    
    echo $! > tpid
    
    echo Start Success!
    

    stop.sh

    #!/bin/sh
    APP_NAME=myapp
    
    tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo 'Stop Process...'
        kill -15 $tpid
    fi
    sleep 5
    tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo 'Kill Process!'
        kill -9 $tpid
    else
        echo 'Stop Success!'
    fi
    

    check.sh

    #!/bin/sh
    APP_NAME=myapp
    
    tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
            echo 'App is running.'
    else
            echo 'App is NOT running.'
    fi
    

    kill.sh

    #!/bin/sh
    APP_NAME=myapp
    
    tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo 'Kill Process!'
        kill -9 $tpid
    fi
    

    博主不得转载,但还是看了怎么办:
    Spring Boot 部署与服务配置

  • 相关阅读:
    ehcache 详解 实例
    oscache ehcache oscache与ehcache的区别
    Nginx_lua
    Java程序员的发展前景
    JAVA MemCache 史无前例的详细讲解!看完包精通MEMCACHE!
    java ssl https 连接详解 生成证书
    使用django的ImageField和from制作上传图片页面
    1.python中的列表及其操作
    django分页
    django和javaweb的比较
  • 原文地址:https://www.cnblogs.com/woshimrf/p/springboot-jar.html
Copyright © 2011-2022 走看看