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 /data/app/myapp.jar --spring.profiles.active=stg > /dev/null 2>&1 &

echo $! > tpid

stop.sh

tpid=`cat tpid | awk '{print $1}'`
tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`
if [ ${tpid} ]; then
kill -9 $tpid
fi

check.sh

#!/bin/sh

tpid=`cat tpid | awk '{print $1}'`
tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`
if [ ${tpid} ]; then
echo App is running.
else
echo App is NOT running.
fi

kill.sh

#!/bin/sh
# kill -9 `ps -ef|grep 项目名称|awk '{print $2}'`
kill -9 `ps -ef|grep demo|awk '{print $2}'`

Spring Boot 部署与服务配置的更多相关文章

  1. 十六、Spring Boot 部署与服务配置

    spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项 ...

  2. 27. Spring Boot 部署与服务配置

    转自“https://www.cnblogs.com/zhchoutai/p/7127598.html” Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函 ...

  3. spring boot 部署为jar

    前言 一直在ide中敲代码,使用命令行mvn spring-boot:run或者gradlew bootRun来运行spring boot项目.想来放到prod上面也应该很简单.然而今天试了下,各种问 ...

  4. 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事

    微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...

  5. Spring Boot、微服务架构和大数据

    一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶 ...

  6. 在5分钟内将Spring Boot作为Windows服务启动

    分享优锐课学习笔记~来看一下如何使用Spring Boot创建Windows服务以及通过配置详细信息来快速启动并运行. 最近不得不将Spring Boot应用程序部署为Windows服务,感到惊讶的是 ...

  7. Spring Boot部署方法

    Spring Boot部署方法     网上搜到的部署方法无非是打成jar包,然后shell执行nohup java调用jar命令,或者是打成war包然后部署到tomcat或者jetty容器上面. S ...

  8. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

  9. Spring Boot初探之log4j2配置

    一.背景 下面讲在使用Spring Boot搭建微服务框架时如何配置log4j2,通过log4j2输出系统中日志信息. 二.添加log4j2的配置文件 在项目的src/main/rescources目 ...

随机推荐

  1. HttpClient post中文乱码解决

    在javase方式下使用HttpClient没有进行任何编码设置,本地从服务端获取到数据不存在中文乱码. 但是将此段代码部署到Tomcat下面出现了中文乱码,此时设置: post.getParams( ...

  2. LeetCode_Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. 并行编译加快VS C++项目的编译速度

    最近编译的项目都比较大,话说自己的电脑配置还行,但编译所花的时间还是很长,遇到需要重新编译整个项目的时候真的有回宿舍睡一觉的冲动.昨天一不小心被我发现了一款软件Xoreax IncrediBuild ...

  4. C# unsafe code

    (*) unsafe 和 fixed unsafe {                    ];     ; i < array.Length; i++)     {         arra ...

  5. Remove Nth Node From End of List 解答

    Question Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  6. 剑指offer-面试题8.旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数据的末尾,我们称之为 数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组 的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  7. Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  8. CDH 1、CDH简介

    1.Apache Hadoop 不足之处 • 版本管理混乱 • 部署过程繁琐.升级过程复杂 • 兼容性差 • 安全性低 2.Hadoop 发行版 • Apache Hadoop • Cloudera’ ...

  9. Unity 生命周期

    原文翻译:            Execution Order of Event Functions            事件函数的执行顺序                        Edit ...

  10. NSNotificationCenter 传对象

    [[NSNotificationCenter defaultCenter] postNotificationName:@"postCity" object:pro]; [[NSNo ...