当前位置: 首页 > 编程日记 > 正文

spring boot 服务 正确关闭方式

引言

Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分Spring Boot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(MicroServices)相当契合的微框架。
网络上关于Spring Boot的QuickStart式中文内容已经相当丰富,但是对于部署后怎样便捷、安全地停止服务(shutdown),还比较缺乏,最近发现Spring Boot的官方指南更新了相关内容,因此结合该部分更新,对如何基于官方提供的特性正确地停止Spring Boot应用进行简单说明。

主要有两种方式:通过HTTP发送shutdown信号,或者通过service stop的方式

方式一:通过HTTP发送shutdown信号

该方式主要依赖Spring Boot Actuatorendpoint特性,具体步骤如下:

1. 在pom.xml中引入actuator依赖

<dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

2. 开启shutdown endpoint

Spring Boot Actuatorshutdown endpoint默认是关闭的,因此在application.properties中开启shutdown endpoint

#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false

3. 发送shutdown信号

shutdown的默认urlhost:port/shutdown,当需要停止服务时,向服务器post该请求即可,如:
curl -X POST host:port/shutdown
将得到形如{"message":"Shutting down, bye..."}的响应

4. 安全设置

可以看出,使用该方法可以非常方便的进行远程操作,但是需要注意的是,正式使用时,必须对该请求进行必要的安全设置,比如借助spring-boot-starter-security进行身份认证:

  1. pom.xml添加security依赖

    <dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
  2. 开启安全验证
    application.properties中变更配置,并

    #开启shutdown的安全验证
    endpoints.shutdown.sensitive=true
    #验证用户名
    security.user.name=admin
    #验证密码
    security.user.password=secret
    
  3. 此时post的url 需附加密码和账号:  http://admin:secret@localhost:8888

方式二:部署为Unix/Linux Service

该方式主要借助官方的spring-boot-maven-plugin创建"Fully executable" jar ,这中jar包内置一个shell脚本,可以方便的将该应用设置为Unix/Linux的系统服务(init.d service),官方对该功能在CentOS和Ubuntu进行了测试,对于OS X和FreeBSD,可能需要自定义。具体步骤如下:

1. 在pom.xml中引入插件:

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

2. 设置为系统服务

将你的应用打成jar包,部署到服务器,假设部署路径为/var/app,包名为app.jar,通过如下方式将应该设置为一个系统服务:

注意软连接必须使用完整路径
sudo ln -s /var/app/app.jar /etc/init.d/app

3. 赋予可执行权限:

chmod u+x app.jar

4. 以系统服务的方式管理

接下来,就可以使用我们熟悉的service foo start|stop|restart来对应用进行启停等管理了
sudo service app start|stop
命令将得到形如Started|Stopped [PID]的结果反馈

(设置开机启动 chkconfig servicename on/off)

默认PID文件路径:/var/run/appname/appname.pid
默认日志文件路径:/var/log/appname.log

这可能是我们更熟悉也更常用的管理方式。

自定义参数

在这种方式下,我们还可以使用自定义的.conf文件来变更默认配置,方法如下:

  1. 在jar包相同路径下创建一个.conf文件,名称应该与.jar的名称相同,如appname.conf
  2. 在其中配置相关变量,如:

    JAVA_HOME=/usr/local/jdk
    JAVA_OPTS=-Xmx1024M
    LOG_FOLDER=/custom/log

安全设置

作为应用服务,安全性是一个不能忽略的问题,如下一些操作可以作为部分基础设置参考:

  • 为服务创建一个独立的用户,同时最好将该用户的shell绑定为/usr/sbin/nologin
  • 赋予最小范围权限:chmod 500 app.jar
  • 阻止修改:sudo chattr +i app.jar
  • 对.conf文件做类似的工作:chmod 400 app.conf,sudo chown root:root app.conf

References:

  1. Installing Spring Boot applications
  2. Endpoints
  3. Securing sensitive endpoints

本文转自(http://www.cnblogs.com/lobo/p/5657684.html),部分内容做了微调

转载于:https://www.cnblogs.com/flying607/p/8359219.html

相关文章:

如何在5美元的Raspberry Pi上构建个人开发服务器

In this article, youll learn how to build a personal dev server by installing Git, Node.js, Rust, and Docker on a Raspberry Pi. The cheapest option costs just $5. You can get a starter kit ($25) for free here.在本文中&#xff0c;您将学习如何通过在Raspberry…

Eclipse:xml文件中添加.xsd约束文件

今天在使用dubbo的时候&#xff0c;XML文件一直报错。找不到dubbo的xsd约束文件。 cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element dubbo:reference 解决方法&#xff1a; 找到dubbo的jar包&#xff0c;然后在META-…

029 浏览器不能访问虚拟机的问题解决

1.在搭建分布式时 ssh一直不能进行scp&#xff0c;后来发现一个问题。 windows中的hosts配置了三台虚拟机的映射&#xff0c;但是在虚拟机中的hosts没有配置。 做法是在每台虚拟机上都配置三台虚拟机的映射。 2.端口访问与防火墙 最近帮别人解决问题时才注意的。 以前安装好虚拟…

获取 一个文件 在沙盒Library/Caches/ 目录下的路径

(NSString *)getFullPathWithFile:(NSString *)urlName {//先获取 沙盒中的Library/Caches/路径NSString *docPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *myCacheDirectory [docPath stringByAppendingPat…

如何有效使用每一点脑力总结_如何更有效地节省脑力和编码

如何有效使用每一点脑力总结如果您知道这些工具的存在&#xff0c;那么您现在可能会使用它们。 (If you knew these tools existed, youd probably be using them by now.) This article isn’t going to tell you about saving your neck with a Roost stand, or your wrists …

C语言程序设计50例(一)(经典收藏)

【程序1】题目&#xff1a;有1、2、3、4个数字&#xff0c;能组成多少个互不相同且无重复数字的三位数&#xff1f;都是多少&#xff1f;1.程序分析&#xff1a;可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去      掉不满足条件的排列。 1 #include…

python多线程执行类中的静态方法

在python 中如果通过多线程的方式执行某个方法很简单&#xff0c;只需要把同步函数的第一个参数为该函数对象即可。但是如果函数对象是某个类的静态方法&#xff0c;这时候如果直接使用类的该函数对象会报错。此时需要构造一个代理的方法来实现。 如&#xff1a;上一个博文中的…

检测缓存文件是否超时

(BOOL)isTimeOutWithFile:(NSString *)filePath timeOut:(double)timeOut {//获取文件的属性NSDictionary *fileDict [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];//获取文件的上次的修改时间NSDate *lastModfyDate fileDict.fileModificat…

JavaScript创建对象–如何在JS中定义对象

Objects are the main unit of encapsulation in Object-Oriented Programming. In this article, I will describe several ways to build objects in JavaScript. They are:对象是面向对象编程中封装的主要单元。 在本文中&#xff0c;我将介绍几种使用JavaScript构建对象的方…

MyBatis中#{}和${}的区别

------------------------siwuxie095 MyBatis 中 #{} 和 ${} 的区别 1、在 MyBatis 的映射配置文件中&#xff0c;动态传递参数有两种方式&#xff1a; &#xff08;1&#xff09;#{} 占位符 &#xff08;2&#xff09;${} 拼接符 2、#{} 和 ${} 的区别 &#xff08;1&#xff…

十进制字符串转十六进制字符串

NSString *colorStr[self.model.sclass_color substringFromIndex:1]; unsigned long cor strtoul([colorStr UTF8String],0,16);

gi克隆github文件_如何构建GitHub文件搜索功能的克隆

gi克隆github文件In this article, we will build a project that mimics the lesser known but awesome file search functionality provided by GitHub.在本文中&#xff0c;我们将构建一个项目&#xff0c;该项目模仿GitHub提供的鲜为人知但功能强大的文件搜索功能。 To se…

ipython --pandas

d定义: pandas是一个强大的Python数据分析的工具包。 pandas是基于NumPy构建的。 安装方法: pip install pandas import pandas as pd pandas的主要功能 具备对其功能的数据结构DataFrame、Series 集成时间序列功能 提供丰富的数学运算和操作 灵活处理缺失数据 Series 定义:Ser…

玩转Android之二维码生成与识别

二维码&#xff0c;我们也称作QRCode&#xff0c;QR表示quick response即快速响应&#xff0c;在很多App中我们都能见到二维码的身影&#xff0c;最常见的莫过于微信了。那么今天我们就来看看怎么样在我们自己的App中集成二维码的扫描与生成功能。OK&#xff0c;废话不多说&…

属性字符串(富文本)的使用

改变字符串中某些字符串字体的颜色 NSMutableAttributedString *attrStr[[NSMutableAttributedString alloc] initWithString:str]; [attrStr addAttribute:NSForegroundColorAttributeName value:kUIColorFromRGB(0xb2151c) range:[str rangeOfString:[NSString stringWith…

如何使用create-react-app在本地设置HTTPS

Running HTTPS in development is helpful when you need to consume an API that is also serving requests via HTTPS. 当您需要使用同时通过HTTPS服务请求的API时&#xff0c;在开发中运行HTTPS会很有帮助。 In this article, we will be setting up HTTPS in development …

Swift强制解析

IDE:Xcode Version7.3.1 Swift中"数据类型?"表示这是可选类型&#xff0c;即 某个常量或者变量可能是一个类型&#xff0c;也可能什么都没有&#xff0c;不确定它是否有值&#xff0c;也许会是nil。 比如&#xff1a; let num1 “123” let num2 Int(number1) pri…

rfc6455 WebSockets

https://tools.ietf.org/html/rfc6455 转载于:https://www.cnblogs.com/cheungxiongwei/p/8385719.html

2020-mb面试指南_2020年最佳代码面试准备平台

2020-mb面试指南Software developer interviews are rapidly evolving. Years ago, mastering data structures and common algorithms was enough to ace an interview and get a job. Today though, employers want candidates with real-world experience and skills. 软件开…

设计模式学习笔记(一)之工厂模式、单例模式

一、工厂模式 (1)简单工厂模式&#xff1a; 1 public interface IProduct { 2 3 public void saleProduct(); 4 5 } 创建一个产品接口&#xff0c;有一个卖产品的方法。 1 public class ProductA implements IProduct{ 2 3 public void saleProduct(){ 4 Sy…

iOS使用支付宝支付步骤

开发平台 (http://open.alipay.com/index.htm(这个里面找不到sdk) 需要进入下面的链接&#xff09; 使用支付宝进行一个完整的支付功能&#xff0c;大致有以下步骤&#xff1a; 1>先与支付宝签约&#xff0c;获得商户ID&#xff08;partner&#xff09;和账号ID&#xff08…

heroku_了解如何使用Heroku部署全栈Web应用程序

herokuBuilding a full stack web app is no mean feat. Learning to deploy one to production so that you can share it with the world can add an additional layer of complexity.构建全栈式Web应用程序绝非易事。 学习将其部署到生产环境中&#xff0c;以便您可以与世界…

SpringMVC学习手册(三)------EL和JSTL(上)

1.含义 EL: Expression Language , 表达式语言JSTL: Java Server Pages Standard Tag Library, JSP标准标签库 2.测试项目构建 2.1 复制JSTL的标准实现 复制Tomcat中webapps\examples\WEB-INF\lib下的两个jar包到新建项目目录的WEB-INF\lib下2.2 在JSP文件中使用tagli…

OpenStack Heat模板详解

Heat模板全称为heat orchestration template&#xff0c;简称为HOT。 1 典型Heat模板结构 heat_template_version: 2015-04-30 description:# a description of the template parameter_groups:# a declaration of input parameter groups and order parameters:# declaration …

如何从头开始构建自己的Linux Dotfiles Manager

As a new linux &#x1f427; user, you might realize that there are a bunch of configuration files present in your system. These special files are called "dotfiles". 作为新的Linux用户&#xff0c;您可能会意识到系统中存在大量配置文件。 这些特殊文件…

D3.js、HTML5、canvas 开发专题

https://www.smartdraw.com/genogram/ http://www.mamicode.com/info-detail-1163777.html D3折线图 https://www.cnblogs.com/hwaggLee/p/5073885.html js-d3画图插件 http://www.xiaomlove.com/2014/06/29/d3-js简单画图-箭头连接随机圆圈/ 连线 http://www.decemberc…

单向链表JAVA代码

//单向链表类publicclassLinkList{ //结点类 publicclassNode{ publicObject data; publicNode next; publicNode(Object obj,Node next){ this.data obj; this.next next; } } Node head; //记录…

forkjoin rxjs_如何通过吃披萨来理解RxJS运算符:zip,forkJoin和Combine

forkjoin rxjs什么是RxJS&#xff1f; (What is RxJS?) Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change - Wikipedia响应式编程是一种与数据流和变更传播有关的异步编程范式 -Wikipedia RxJS is a…

SQLserver数据库操作帮助类SqlHelper

1 SqlHelper源码 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collections; namespace SQL.Access {/// <summary>/// SqlServer数据访问帮助类/// </summary>public sealed class SqlHelper{#region 私有构造…

python框架之Flask基础篇(一)

一.第一个hello world程序 # codingutf-8 from flask import Flaskapp Flask(__name__)app.route(/) def hello_world():return Hello World!if __name__ __main__:app.run(debugTrue) 1.app参数的设置&#xff1a; 以下几种方式全部拿debug模式举例&#xff1a; .方式一&…