SpringBoot项目部署与服务配置

spring Boot 其默认是集成web容器的,启动方式由像普通Java程序同样,main函数入口启动。其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat)。固然你也能够将项目打包成war包,放到独立的web容器中(Tomcat、weblogic等等),固然在此以前你要对程序入口作简单调整。java

项目构建咱们使用Maven或Gradle,这将使项目依赖、jar包管理、以及打包部署变的很是方便。mysql

1、内嵌 Server 配置

Spring Boot将容器内置后,它经过配置文件的方式类修改相关server配置。 
先看一下下面的图,为关于server的配置列项: 
配置 
配置linux

其中经常使用的配置只有少数几个,已经用紫色标记起来。红框圈起来的部分,看名称分类就能够明白其做用。 
对server的几个经常使用的配置作个简单说明:web

# 项目contextPath,通常在正式发布版本中,咱们不配置 server.context-path=/myspringboot # 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知 server.error.path=/error # 服务端口 server.port=9090 # session最大超时时间(分钟),默认为30 server.session-timeout=60 # 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的状况下才配置 # server.address=192.168.16.11

Tomcat 
Tomcat为Spring Boot的默认容器,下面是几个经常使用配置:spring

# tomcat最大线程数,默认为200 server.tomcat.max-threads=800 # tomcat的URI编码 server.tomcat.uri-encoding=UTF-8 # 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp) server.tomcat.basedir=H:/springboot-tomcat-tmp # 打开Tomcat的Access日志,并能够设置日志格式的方法: #server.tomcat.access-log-enabled=true #server.tomcat.access-log-pattern= # accesslog目录,默认在basedir/logs #server.tomcat.accesslog.directory= # 日志文件目录 logging.path=H:/springboot-tomcat-tmp # 日志文件名称,默认为spring.log logging.file=myapp.log

Jetty 
若是你要选择Jetty,也很是简单,就是把pom中的tomcat依赖排除,并加入Jetty容器的依赖,以下:sql

<dependencies> <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> <dependencies> 

打包 
打包方法: 
CMD进入项目目录,使用 mvn clean package 命令打包,以个人项目工程为例:数据库

E:\spring-boot-sample>mvn clean package

能够追加参数 -Dmaven.test.skip=true 跳过测试。 
打包后的文件存放于项目下的target目录中,如:spring-boot-sample-0.0.1-SNAPSHOT.jar 
若是pom配置的是war包,则为spring-boot-sample-0.0.1-SNAPSHOT.wartomcat

2、部署到JavaEE容器

  1. 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
public class SpringBootSampleApplication extends SpringBootServletInitializer{ private static final Logger logger = LoggerFactory.getLogger(SpringBootSampleApplication.class); @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(this.getClass()); } }
  1. 修改pom文件中jar 为 war
<!-- <packaging>jar</packaging> --> <packaging>war</packaging>
  1. 修改pom,排除tomcat插件
<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>
  1. 打包部署到容器 
    使用命令 mvn clean package 打包后,同通常J2EE项目同样部署到web容器。

3、使用Profile区分环境

spring boot 能够在 “配置文件”、“Java代码类”、“日志配置” 中来配置profile区分不一样环境执行不一样的结果安全

一、配置文件 
使用配置文件application.yml 和 application.properties 有所区别 
以application.properties 为例,经过文件名来区分环境 application-{profile}.properties 
application.propertiesspringboot

app.name=MyApp server.port=8080 spring.profiles.active=dev

application-dev.properties

server.port=8081

application-stg.properties

server.port=8082

在启动程序的时候经过添加 –spring.profiles.active={profile} 来指定具体使用的配置 
例如咱们执行 java -jar demo.jar –spring.profiles.active=dev 那么上面3个文件中的内容将被如何应用? 
Spring Boot 会先加载默认的配置文件,而后使用具体指定的profile中的配置去覆盖默认配置。

app.name 只存在于默认配置文件 application.properties 中,由于指定环境中不存在一样的配置,因此该值不会被覆盖 
server.port 默认为8080,可是咱们指定了环境后,将会被覆盖。若是指定stg环境,server.port 则为 8082 
spring.profiles.active 默认指定dev环境,若是咱们在运行时指定 –spring.profiles.active=stg 那么将应用stg环境,最终 server.port 的值为8082

二、Java类中@Profile注解 
下面2个不一样的类实现了同一个接口,@Profile注解指定了具体环境

// 接口定义 public interface SendMessage { // 发送短信方法定义 public void send(); } // Dev 环境实现类 @Component @Profile("dev") public class DevSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Dev Send()<<<<<<<<"); } } // Stg环境实现类 @Component @Profile("stg") public class StgSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Stg Send()<<<<<<<<"); } } // 启动类 @SpringBootApplication public class ProfiledemoApplication { @Value("${app.name}") private String name; @Autowired private SendMessage sendMessage; @PostConstruct public void init(){ sendMessage.send();// 会根据profile指定的环境实例化对应的类 } }

三、logback-spring.xml也支持有节点来支持区分

<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <logger name="org.springframework.web" level="INFO"/> <springProfile name="default"> <logger name="org.springboot.sample" level="TRACE" /> </springProfile> <springProfile name="dev"> <logger name="org.springboot.sample" level="DEBUG" /> </springProfile> <springProfile name="staging"> <logger name="org.springboot.sample" level="INFO" /> </springProfile> </configuration>

再说一遍文件名不要用logback.xml 请使用logback-spring.xml

4、指定外部的配置文件

有些系统,关于一些数据库或其余第三方帐户等信息,因为安全问题,其配置并不会提早配置在项目中暴露给开发人员。 
对于这种状况,咱们在运行程序的时候,能够经过参数指定一个外部配置文件。 
以 demo.jar 为例,方法以下:

java -jar demo.jar --spring.config.location=/opt/config/application.properties

其中文件名随便定义,无固定要求。

5、建立一个Linux 应用的sh脚本

下面几个脚本仅供参考,请根据本身须要作调整 
start.sh

#!/bin/sh rm -f tpid nohup java -jar myapp.jar --spring.config.location=application.yml > /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
相关文章
相关标签/搜索