SpringBoot官方文档中对于linux类系统配置service给出了比较完整的文档,按照文档一步步的操作就可以很简单的将SpringBoot项目安装为系统服务,但是windows系统只给了一个demo,demo中注释也比较少,因此特写此文章,利人利己.
- 修改pom文件
<!--添加properties-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--winsw配置文件目标目录-->
<dist.dir>${project.build.directory}/dist</dist.dir>
<!--exe的名称-->
<dist.project.id>${project.artifactId}</dist.project.id>
<!--最终转变为service的名称-->
<dist.project.name>${project.name}</dist.project.name>
<!--最终转变为service的描述-->
<dist.project.description>${project.description}</dist.project.description>
<!--springboot的main class-->
<dist.start.class>com.qinze.medical.MedicalServer</dist.start.class>
<!--jmx控制端口-->
<dist.jmx.port>50201</dist.jmx.port>
</properties>
<!--windows service wrapper,用于包装项目为exe-->
<dependency>
<groupId>com.sun.winsw</groupId>
<artifactId>winsw</artifactId>
<classifier>bin</classifier>
<version>2.2.0</version>
<type>exe</type>
<scope>provided</scope>
</dependency>
<build>
<finalName>qinze</finalName>
<plugins>
<!--必须移除该依赖,因为会导致jar包的结构有所变化,导致无法启动服务-->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<!--将winsw.exe复制到${dist.dir}目录下面,并重名为service.exe-->
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.sun.winsw</groupId>
<artifactId>winsw</artifactId>
<classifier>bin</classifier>
<type>exe</type>
<destFileName>service.exe</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${dist.dir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<!--将winsw的配置文件复制到${dist.dir}目录下-->
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${dist.dir}</outputDirectory>
<resources>
<resource>
<directory>src/main/dist</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<!--不同系统下的打包配置-->
<configuration>
<descriptors>
<descriptor>src/main/assembly/unix.xml</descriptor>
<descriptor>src/main/assembly/windows.xml</descriptor>
</descriptors>
</configuration>
<executions>
<!--根据上面定义的配置进行打包-->
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 在src/main下面创建一个目录assembly,然后创建一个windows.xml文件(src/main/assembly/windows.xml),内容如下,该文件是maven-assembly-plugin需要使用的配置文件
<?xml version="1.0"?>
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>windows</id>
<!--打包格式-->
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<!-- Workaround to create logs directory -->
<fileSets>
<fileSet>
<directory>${dist.dir}</directory>
<outputDirectory>logs</outputDirectory>
<excludes>
<exclude>*/**</exclude>
</excludes>
</fileSet>
</fileSets>
<files>
<file>
<source>${dist.dir}/service.exe</source>
<outputDirectory/>
<destName>${dist.project.id}.exe</destName>
</file>
<file>
<source>${dist.dir}/service.xml</source>
<outputDirectory/>
<destName>${dist.project.id}.xml</destName>
</file>
<file>
<source>${dist.dir}/service.exe.config</source>
<outputDirectory/>
<destName>${dist.project.id}.exe.config</destName>
</file>
</files>
</assembly>
- 在src/main下创建dist目录,然后创建两个文件
4.1 service.exe.config,定义了支持.net的运行时版本
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
</configuration>
4.2 service.xml,定义了service的相关配置,启动/停止规则等
<service>
<id>@dist.project.id@</id>
<name>@dist.project.name@</name>
<description>@dist.project.description@</description>
<workingdirectory>%BASE%\</workingdirectory>
<logpath>%BASE%\logs</logpath>
<logmode>rotate</logmode>
<executable>java</executable>
<startargument>-Dspring.application.admin.enabled=true</startargument>
<startargument>-Dcom.sun.management.jmxremote.port=@dist.jmx.port@</startargument>
<startargument>-Dcom.sun.management.jmxremote.authenticate=false</startargument>
<startargument>-Dcom.sun.management.jmxremote.ssl=false</startargument>
<!--关闭devtool,否则会导致启动失败,原因是devtool会启动一个线程,但是该线程无法正常启动-->
<startargument>-Dspring.devtools.restart.enabled=false</startargument>
<startargument>-cp</startargument>
<startargument>lib/*</startargument>
<!--替换为你的class类名-->
<startargument>com.qinze.daemon.StartSpringBootService</startargument>
<startargument>@dist.start.class@</startargument>
<stopexecutable>java</stopexecutable>
<stopargument>-cp</stopargument>
<stopargument>lib/*</stopargument>
<stopargument>com.qinze.daemon.StopSpringBootService</stopargument>
<stopargument>@dist.jmx.port@</stopargument>
</service>
- 创建class
import org.springframework.boot.SpringApplication;
import org.springframework.util.ClassUtils;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import java.io.IOException;
/**
* 启动/停止springboot项目的服务
*/
public class SpringBootService {
public void start(String[] args) throws Exception {
if (args.length == 0) {
throw new IllegalStateException("Spring Boot application class must be provided.");
}
Class<?> springBootApp = ClassUtils.resolveClassName(args[0],
SpringBootService.class.getClassLoader());
System.out.println("Starting Spring Boot application [" + springBootApp.getName() + "]");
SpringApplication.run(springBootApp);
}
public void stop(String[] args) throws IOException {
System.out.println("Stopping Spring Boot application...");
int jmxPort = Integer.parseInt(args[0]);
String jmxName = SpringApplicationAdminClient.DEFAULT_OBJECT_NAME;
JMXConnector connector = SpringApplicationAdminClient.connect(jmxPort);
try {
MBeanServerConnection connection = connector.getMBeanServerConnection();
try {
new SpringApplicationAdminClient(connection, jmxName).stop();
} catch (InstanceNotFoundException ex) {
throw new IllegalStateException("Spring application lifecycle JMX bean not " +
"found, could not stop application gracefully", ex);
}
} finally {
connector.close();
}
}
}
public class StartSpringBootService {
public static void main(String[] args) throws Exception {
new SpringBootService().start(args);
}
}
public class StopSpringBootService {
public static void main(String[] args) throws Exception {
new SpringBootService().stop(args);
}
}
import org.springframework.jmx.JmxException;
import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
/**
* A JMX client for the {@code SpringApplicationAdmin} MBean. Permits to obtain
* information about a given Spring application.
*
* @author Stephane Nicoll
*/
class SpringApplicationAdminClient {
// Note: see SpringApplicationAdminJmxAutoConfiguration
static final String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";
private final MBeanServerConnection connection;
private final ObjectName objectName;
SpringApplicationAdminClient(MBeanServerConnection connection, String jmxName) {
this.connection = connection;
this.objectName = toObjectName(jmxName);
}
/**
* Create a connector for an {@link javax.management.MBeanServer} exposed on the
* current machine and the current port. Security should be disabled.
*
* @param port the port on which the mbean server is exposed
* @return a connection
* @throws IOException if the connection to that server failed
*/
public static JMXConnector connect(int port) throws IOException {
String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + port + "/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
return JMXConnectorFactory.connect(serviceUrl, null);
}
/**
* Check if the spring application managed by this instance is ready. Returns
* {@code false} if the mbean is not yet deployed so this method should be repeatedly
* called until a timeout is reached.
*
* @return {@code true} if the application is ready to service requests
* @throws org.springframework.jmx.JmxException if the JMX service could not be contacted
*/
public boolean isReady() {
try {
return (Boolean) this.connection.getAttribute(this.objectName, "Ready");
} catch (InstanceNotFoundException ex) {
return false; // Instance not available yet
} catch (AttributeNotFoundException ex) {
throw new IllegalStateException(
"Unexpected: attribute 'Ready' not available", ex);
} catch (ReflectionException ex) {
throw new JmxException("Failed to retrieve Ready attribute",
ex.getCause());
} catch (MBeanException ex) {
throw new JmxException(ex.getMessage(), ex);
} catch (IOException ex) {
throw new JmxException(ex.getMessage(), ex);
}
}
/**
* Stop the application managed by this instance.
*
* @throws JmxException if the JMX service could not be contacted
* @throws IOException if an I/O error occurs
* @throws InstanceNotFoundException if the lifecycle mbean cannot be found
*/
public void stop() throws IOException,
InstanceNotFoundException {
try {
this.connection.invoke(this.objectName, "shutdown", null, null);
} catch (ReflectionException ex) {
throw new JmxException("Shutdown failed", ex.getCause());
} catch (MBeanException ex) {
throw new JmxException("Could not invoke shutdown operation", ex);
}
}
private ObjectName toObjectName(String name) {
try {
return new ObjectName(name);
} catch (MalformedObjectNameException ex) {
throw new IllegalArgumentException("Invalid jmx name '" + name + "'");
}
}
}