3、Spring Boot之使用init.d启动jar

1.springboot启动方式

springboot的程序包支持如下两种方式启动:

  • java -jar
  • 可执行文件(./jar)

Linux中支持将可执行文件以init.d和systemd的方式启动,以init.d或systemd的启动方式比java -jar启动更有优势:

  • 启动的时候会判断是否已经启动成功
  • 停止的时候能确保停止
  • 支持start|stop|force-stop|restart|force-reload|status|run等initd.d的服务命令选项

我们生产环境的系统都为centos,且大部分机器centos版本都为6.*,不支持systemd,所以统一使用init.d的方式启动。

2.pom配置

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

3.conf配置

默认下,jar包会读取同目录下相同文件名.conf文件中的配置信息。

1).目录结构

[root@*-*-* deploy]# ll
total 30096
-r-------- 1 root root     1396 May 10 18:32 springboot-admin-0.0.1-SNAPSHOT.conf
-r-x------ 1 root root 30813005 May 10 18:03 springboot-admin-0.0.1-SNAPSHOT.jar

2).conf配置信息

JAVA_HOME="/usr/local/java/jdk1.8.0_71"
JAVA_OPTS="-server
 -Xms1G
 -Xmx1G
 -XX:+DisableExplicitGC
 -verbose:gc
 -Xloggc:/data/springboot-admin/10037/logs/gc.%t.log
 -XX:+PrintHeapAtGC
 -XX:+PrintTenuringDistribution
 -XX:+PrintGCApplicationStoppedTime
 -XX:+PrintGCTaskTimeStamps
 -XX:+PrintGCDetails
 -XX:+PrintGCDateStamps
 -XX:PermSize=128m
 -XX:MaxPermSize=128m
 -XX:NewRatio=4
 -XX:SurvivorRatio=8
 -XX:TargetSurvivorRatio=90
 -XX:MaxTenuringThreshold=8
 -XX:+UseCMSInitiatingOccupancyOnly
 -XX:CMSInitiatingOccupancyFraction=70
 -XX:ParallelGCThreads=19
 -XX:ConcGCThreads=19
 -XX:-UseGCOverheadLimit
 -XX:+UseParNewGC
 -XX:+UseConcMarkSweepGC
 -XX:CMSFullGCsBeforeCompaction=1
 -XX:+CMSParallelRemarkEnabled
 -XX:+CMSScavengeBeforeRemark
 -XX:+ParallelRefProcEnabled
 -XX:+UseCMSCompactAtFullCollection
 -XX:CMSMaxAbortablePrecleanTime=6000
 -XX:CompileThreshold=10
 -XX:MaxInlineSize=1024
 -Dsun.net.client.defaultConnectTimeout=60000
 -Dsun.net.client.defaultReadTimeout=60000
 -Dnetworkaddress.cache.ttl=300
 -Dsun.net.inetaddr.ttl=300
 -Djsse.enableCBCProtection=false
 -Djava.security.egd=file:/dev/./urandom
 -Dlog.console.level=off
 -Dlog.root.level=warn
 -Dlog.path=/data/springboot-admin/10037/logs
 -Dlog4j2.DiscardThreshold=INFO
 -Dlog4j2.AsyncQueueFullPolicy=Discard"
RUN_ARGS="-Dspring.profiles.active=test"
LOG_FOLDER=/data/springboot-admin/10037/logs

4.注册及运行脚本

[root@* deploy]# chmod 400 springboot-admin-0.0.1-SNAPSHOT.conf 
[root@* deploy]# chmod 500 springboot-admin-0.0.1-SNAPSHOT.jar 
ln -s /data/springboot-admin/10037/deploy/springboot-admin-0.0.1-SNAPSHOT.jar /etc/init.d/springboot-admin
[root@* deploy]# service springboot-admin start
Started [16299]
[root@* deploy]# service springboot-admin restart
Stopped [16299]
Started [16544]
[root@* deploy]# service springboot-admin stop
Stopped [16544]

5.添加启动项

chkconfig --add springboot-admin
==

6.探究

通过查看springboot源码,实现方式主要是给jar包了层启动脚本,该脚本源码在spring-boot-tools/spring-boot-loader-tools下的launch.script文件中,可以通过bash -x来查看脚本的执行过程。

bash -x /data/springboot-admin/10037/deploy/springboot-admin-0.0.1-SNAPSHOT.jar

stop和force_stop的区别是一个是kill,另一个是kill -9。

  • kill 向进程发送SIGTERM信号,类似于Ctrl+C的效果
  • kill -9 向进程发送是SIGKILL,即exit。exit信号不会被系统阻塞
    PS:使用stop某些特殊情况下可能无法完全停止

7.权限

在Spring Boot的启动脚本中,假如jar包对应的拥有者是user1,那么执行service springboot-admin start时,会使用user1账号启动该服务及对应的jar包

转载于:https://my.oschina.net/andChow/blog/896958

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptMapper' defined in file [D:\WorkSpace\work13\djd_server\target\classes\com\jiading\djd\mapper\DeptMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; 元素内容必须由格式正确的字符数据或标记组成。 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at com.jiading.djd.DjdApplication.main(DjdApplication.java:14) [classes/:na]报错了
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值