部署Spring Boot应用

原文地址:http://blog.csdn.net/xiaoyu411502/article/details/47865561java

在开发Spring Boot应用的过程当中,Spring Boot直接执行public static void main()函数并启动一个内嵌的应用服务器(取决于类路径上的以来是Tomcat仍是jetty)来处理应用请求。对于生产环境,这样的部署方式一样有效,同时Spring Boot也支持传统的部署方式——将war包放入应用服务器中启动运行。web

内嵌应用服务器

在使用Maven或Gradle构建Spring Boot应用的过程当中,Spring Boot插件提供了巨大的帮助,除了生命各种预约义的依赖,它还可以构建能够直接运行的jar包——包含了全部的依赖以及内嵌应用服务器。应用的分发也就变得很是简单,任何人拿到了这个jar包,只须要简单运行java -jar your.jar就能够启动应用,无需任何构建工具、安装过程以及应用服务器。spring

内嵌应用服务器配置

在生产环境中,应用服务器须要各种配置,Spring Boot自己提供了一种很是简单的配置机制——application.properties数据库

server.port=8080 # 监听端口 server.address= # 绑定的地址 server.session-timeout= #session有效时长 server.context-path= #默认为/ server.ssl.* #ssl相关配置

Tomcat

默认状况下,Spring Boot启动的内嵌容器就是Tomcat,对于Tomcat有几个很是重要的配置:tomcat

server.tomcat.basedir=/tmp

tomcat的baseDir,日志、dump等文件都存在于这个目录中,通常是系统的临时文件夹/tmp,但也能够按照本身的需求变动位置。安全

server.tomcat.access-log-pattern= # log pattern of the access log server.tomcat.access-log-enabled=false # is access logging enabled

这两个配置打开Tomcat的Access日志,并能够设置日志格式。服务器

Jetty

若是你不喜欢Tomcat,Jetty也是一个很是不错的选择。使用Jetty的方式也很是简单——把tomcat依赖从Maven或Gradle中移除,加入Jetty内嵌容器的依赖:session

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

Java EE应用服务器

除了内嵌容器的部署模式,Spring Boot也支持将应用部署至已有的Tomcat容器, 或JBoss, WebLogic等传统Java EE应用服务器。app

以Maven为例,首先须要将<packaging>jar改为war,而后取消spring-boot-maven-plugin,而后修改Application.javassh

package demo;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @Configuration @ComponentScan @EnableAutoConfiguration public class Application extends SpringBootServletInitializer {   public static void main(String[] args) {  SpringApplication.run(applicationClass, args);  }   @Override  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  return application.sources(applicationClass);  }   private static Class<Application> applicationClass = Application.class; } 

接下来打包应用,将生成的war包放入应用服务器目录便可。

使用外部配置文件

在应用程序中有不少配置项,例如数据库链接地址、日志文件位置、应用服务器配置等等。为了安全与灵活性,咱们推荐将Spring Boot的配置文件放在生产环境的服务器上,并严格控制访问权限。在运行应用时能够经过命令行参数指定配置文件:

java -jar location_of_your_jar_file.jar --spring.config.location=location_of_your_config_file.properties

这样作的好处是:

  • 配置位于生产环境中,数据库链接等私密信息不容易泄露
  • 灵活性强,同一份代码(包括构建的jar包)能够应用于不一样的环境配置(开发、测试、生产)

使用Profile区分环境

在某些状况下,应用的某些业务逻辑可能须要有不一样的实现。例如邮件服务,假设EmailService中包含的send(String email)方法向指定地址发送电子邮件,可是咱们仅仅但愿在生产环境中才执行真正发送邮件的代码,而开发环境里则不发送以避免向用户发送无心义的垃圾邮件。

咱们能够借助Spring的注解@Profile实现这样的功能,这样须要定义两个实现EmailService借口的类:

@Service @Profile("dev") class DevEmailService implements EmailService {   public void send(String email) {  //Do Nothing  } }  @Service @Profile("prod") class ProdEmailService implements EmailService {   public void send(String email) {  //Real Email Service Logic  } } 

@Profile("dev")代表只有Spring定义的Profile为dev时才会实例化DevEmailService这个类。那么如何设置Profile呢?

在配置文件中指定

application.properties中加入:

spring.profiles.active=dev

经过命令行参数

java -jar app.jar --spring.profiles.active=dev

以服务的形式运行应用

使用java命令运行应用很是简单,可是一般咱们都是经过ssh命令链接到服务器并运行它,一旦ssh链接断开,那么由它fork的java子进程也就随之销毁了。因此咱们必须借助工具将应用做为服务运行在服务器上:

Systemd

systemd 是Linux 下的一款系统和服务管理器。能够为Spring Boot应用编写启动脚本:

[Unit] Description=Spring Boot Application  [Service] ExecStart=/usr/bin/java -jar location_of_jar_file.jar --spring.config.location=location_of_config.properties --spring.profiles.active=profile User=${your expected user}  [Install] WantedBy=multi-user.target

Supervisord

Supervisord是用Python实现的一款很是实用的进程管理工具。能够为Spring Boot应用编写:

[program:app] command=/usr/bin/java -jar location_of_jar_file.jar --spring.config.location=location_of_config.properties --spring.profiles.active=profile user=${your expected user} autostart=true autorestart=true startsecs=10 startretries=3
相关文章
相关标签/搜索