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

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

一、内嵌 Server 配置

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

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

# 项目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的默认容器,下面是几个常用配置:

# 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容器的依赖,如下:

<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.war

二、部署到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容器。

三、使用Profile区分环境

spring boot 可以在 “配置文件”、“Java代码类”、“日志配置” 中来配置profile区分不同环境执行不同的结果

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

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

2、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指定的环境实例化对应的类
} }

3、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

四、指定外部的配置文件

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

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

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

五、创建一个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

SpringBoot项目部署与服务配置的更多相关文章

  1. springboot项目部署云服务器

    Springboot项目部署云服务器 springboot项目部署云服务器还是挺简单的 首先你要有java运行环境,就是jdk的安装,如果还没有装没有参考安装:阿里云ECS建网站(建站)超详细全套完整 ...

  2. 2019-03-26 SpringBoot项目部署遇到跨域问题,记录一下解决历程

    近期SpringBoot项目部署遇到跨域问题,记录一下解决历程. 要严格限制,允许哪些域名访问,在application.properties文件里添加配置,配置名可以自己起: cors.allowe ...

  3. React+SpringBoot项目部署

    静态资源访问配置 https://www.jianshu.com/p/b6e0a0df32ec https://segmentfault.com/q/1010000012240531/a-102000 ...

  4. springboot项目部署(war包)

    将springboot项目打包成war,并且部署到tomcat.比较麻烦,自己踩的坑也比较多.算了一下,找bug的时间,有两天熬到凌晨2点. 修改pom.xml使得打包成war <groupId ...

  5. 多个springboot项目部署在同一tomcat上,出现jmx错误

    多个springboot项目部署在同一tomcat上,出现jmx错误 原因:因为jmx某些东西重复,禁用jmx就可以了 endpoints.jmx.unique-names=true

  6. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  7. linux小白成长之路10————SpringBoot项目部署进阶

    [内容指引] war包部署: jar包部署: 基于Docker云部署. 一.war包部署 通过"云开发"平台初始化的SpringBoot项目默认采用jar形式打包,这也是我们推荐的 ...

  8. SpringBoot项目部署进阶

    一.war包部署 通过“云开发”平台初始化的SpringBoot项目默认采用jar形式打包,这也是我们推荐的方式.但是,因为某些原因,软件需求方特别要求用war形式打包,我们该怎么做? 1.项目尚未开 ...

  9. springboot项目部署到独立tomcat的爬坑集锦

    目录 集锦一:普通的springboot项目直接部署jar包 集锦二:springboot项目不能直接打war包部署 集锦三:因为tomcat版本问题导致的lombok插件报错:Invalid byt ...

随机推荐

  1. 【转】Spark性能优化指南——基础篇

    http://mp.weixin.qq.com/s?__biz=MjM5NDMwNjMzNA==&mid=2651805828&idx=1&sn=2f413828d1fdc6a ...

  2. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

  3. theano报一种float类型错误的处理办法

    我实际用的环境是Keras,查错误时查到是Theano的配置问题,所以在标题里就写成Theano的问题了, 是这样的,从Github上下载的别人的代码,准备复现别人的实验,结果在机器上部署好环境之后跑 ...

  4. poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8481   Accepted: 5479 Description In an ...

  5. 线段树(多维+双成段更新) UVA 11992 Fast Matrix Operations

    题目传送门 题意:训练指南P207 分析:因为矩阵不超过20行,所以可以建20条线段的线段树,支持两个区间更新以及区间查询. #include <bits/stdc++.h> using ...

  6. 【转】如果成为一个牛比的BI售前

    转自:天善智能 没有最厉害,只有更厉害啊.也没有一定哪儿厉害,会因人定制各有不同啊.打个比方,如果你长得很庄重,年长,光头或布满银丝,然后以专业的态度,以饱满的激情去跟你客户宣讲,杀伤力巨大.所以,卖 ...

  7. Codeforces Round #246 (Div. 2) A. Choosing Teams

    给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...

  8. 深入浅出 - Android系统移植与平台开发(七)- 初识HAL

    作者:唐老师,华清远见嵌入式学院讲师. 1. HAL的module与stub HAL(Hardware AbstractLayer)硬件抽象层是Google开发的Android系统里上层应用对底层硬件 ...

  9. springmvc入门基础之注解和参数传递

    一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...

  10. zabbix3.2.0beta2 监控模版

    Zabbix监控中用到了一系列模版,nginx后端检测状态 微信告警等一系列常规的服务应用监控 memcached监控模版,可以自己重新定义memcached的端口 http://files.cnbl ...