美文网首页
Spring Boot 官网文档简单翻译 Part VI

Spring Boot 官网文档简单翻译 Part VI

作者: icameisaw | 来源:发表于2018-10-13 23:19 被阅读19次

    Part VI. Deploying Spring Boot Applications

    文档说明:

    • 文档对应的版本为 2.1.0.M3
    • 这不是文档的完整中文翻译,也有可能跟原文文字不一一对应,只是我阅读文档时候做的简单笔记
    • 如果对应的章节没有任何中文,有可能是文档内容比较少,建议直接看原文,或者是我不感兴趣的部分
    • 目录标题没有做翻译,首先标题一般一眼就能看懂什么意思,不做翻译还能保证原文意思,其次也方便对应到原文位置

    62. Deploying to the Cloud

    Spring Boot 的可执行 jar 文件使用于大部分的 PaaS(Platform-as-a-Service)平台。这些平台就是希望你的应用自己准备所有的东西。他们管理的是应用进程,并不是 Java 应用。

    62.1 Cloud Foundry

    62.1.1. Binding to Services

    62.2 Heroku

    62.3 OpenShift

    62.4 Amazon Web Services (AWS)

    62.4.1. AWS Elastic Beanstalk

    Using the Tomcat Platform

    Using the Java SE Platform

    62.4.2. Summary

    62.5 Boxfuse and Amazon Web Services

    62.6 Google Cloud

    63. Installing Spring Boot Applications

    除了可以通过 java -jar 来运行 Spring Boot 应用,还可以对 Unix 操作系统制作完整可执行的应用。一个完整可执行的 jar 就是类似其他可执行的二进制程序一样,或者可以通过 init.d 或者 systemd 注册。这样的话,安装和管理在大部分生产环境都会变得非常容易。

    注意:Fully executable jars 是在文件的前面嵌入了一段脚步来实现的,可以把 jar 或者 war 解压出来查看。目前有些工具并不能识别这种文件格式,所以你不能在所有场景都使用这个功能。

    制作一个 fully executable jar,需要添加如下依赖:

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

    63.1 Supported Operating Systems

    默认的脚步支持大部分的 Linux 发行版本,并在 CentOS 和 Ubuntu 上验证通过。其他平台,例如 OS X 和 FreeBSD,需要自定义嵌入的脚本。

    63.2 Unix/Linux Services

    Installation as an init.d Service (System V)

    建立 jar 的连接到 init.d 并支持 start、stop、restart 和 status 命令:

    • jar 文件的属主可以启动服务
    • 可通过 /var/run/<appname>/<appname>.pid 来查看应用的进程 PID
    • 日志写到 /var/log/<appname>.log 文件

    假如你的应用在 /var/myapp,那么安装到 init.d 的操作如下:

    $ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
    $ service myapp start
    

    Securing an init.d Service

    不要以 root 用户来启动服务。

    保护 jar 文件:

    $ chmod 500 your-app.jar
    # This will prevent any user, including root, from modifying the jar
    $ sudo chattr +i your-app.jar
    

    保护 conf 配置文件:

    $ chmod 400 your-app.conf
    $ sudo chown root:root your-app.conf
    

    Installation as a systemd Service

    假设应用的位置是 /var/myapp,想要以 systemd 服务的形式来安装 Spring Boot 应用,你需要在 /etc/systemd/system 目录创建一个名为 myapp.service 的脚本文件,内容样例如下:

    [Unit]
    Description=myapp
    After=syslog.target
    
    [Service]
    User=myapp
    ExecStart=/var/myapp/myapp.jar
    SuccessExitStatus=143
    
    [Install]
    WantedBy=multi-user.target
    

    Description, User, 和 ExecStart 需根据自己应用的实际情况填写。

    Note: ExecStart 字段没有在脚本里面声明,意味着默认采用 run 命令来启动服务。

    PID 和 日记记录都交给 systemd 管理了。

    开机后启动:

    $ systemctl enable myapp.service
    

    Customizing the Startup Script

    默认嵌入的启动脚本是有 Maven 或者 Gradle 插件提供的,有很多方法可以实现定制化,甚至可以使用 embeddedLaunchScript 来完全使用自己写的脚本。

    Customizing the Start Script when It Is Written

    配置 Maven 或者 Gradle 插件的 embeddedLaunchScriptProperties 属性。

    Customizing a Script When It Runs

    通过环境变量或者一个配置文件

    63.3 Microsoft Windows Services

    [winsw][]

    []winsw]: https://github.com/kohsuke/winsw

    64. What to Read Next

    相关文章

      网友评论

          本文标题:Spring Boot 官网文档简单翻译 Part VI

          本文链接:https://www.haomeiwen.com/subject/azgnaftx.html