Springboot中本身集成tomcat,所以很多情况我们使用tomcat容器搭建Springboot服务,但是当我们的Springboot程序仅仅是作为一个数据交换程序,tomcat就显得太过笨重,介绍一种使用wrapper的部署方法。当然封装成Docker就更好了。

Springboot+Wrapper搭建

在这里插入图片描述

Springboot官方建议在windows作为Service的方法是使用Java Service Wrapper,目前Java Service Wrapper的方法是wrapper.tanukisoftware,贴一个maven路径,官方推荐的也是这个jar。wrapper.tanukisoftware非常好,支持跨平台部署,可以指定wrapper使用jre、内存空间等,类似于一个Docker方法。值得推荐。

		<dependency>
			<groupId>tanukisoft</groupId>
			<artifactId>wrapper</artifactId>
			<version>3.2.3</version>
		</dependency>

wrapper里面有很多配置文件,在网上搜一搜有很多教程,就不在这赘述了。

在Springboot中将主类增加implements WrapperListener就可以作为wrapper启动了。

package com.landsea.adapter.dss;

import com.landsea.adapter.dss.utils.SpringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class DssAdapterApplicationWrapper extends SpringBootServletInitializer implements WrapperListener {

	public static void main(String[] args) {
		ApplicationContext app = SpringApplication.run(DssAdapterApplicationWrapper.class, args);
		SpringUtils.setApplicationContext(app);
		DssAdapterApplicationWrapper instance = (DssAdapterApplicationWrapper) SpringUtils.getBean("dssAdapterApplicationWrapper");
		System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "|{}");
		System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
		WrapperManager.start(instance, args);
	}

	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(this.getClass());
	}

	@Override
	public Integer start(String[] args) {
		// 记录命令参数
		String commands = "";

		if (args.length > 0) {
			for (String s : args) {
				commands += " " + s;
			}
		}

		return null;
	}

	@Override
	public int stop(int exitCode) {
		return exitCode;
	}

	@Override
	public void controlEvent(int event) {

		if (event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT) {
			if (WrapperManager.isLaunchedAsService()) {
				// 运行服务时注销不处理
			} else {
				WrapperManager.stop(0);
			}
		} else if (event == WrapperManager.WRAPPER_CTRL_C_EVENT) {
			// WrapperManager.stop(0);
			// May be called before the runner is started.
		} else {
			WrapperManager.stop(0);
		}
	}
}
package com.landsea.adapter.dss.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

/**
 * Created by admin on 2018/9/10.
 */
public class SpringUtils {

    private static ApplicationContext applicationContext = null;
// 非@import显式注入,@Component是必须的,且该类必须与main同包或子包
    // 若非同包或子包,则需手动import 注入,有没有@Component都一样
    // 可复制到Test同包测试

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtils.applicationContext == null) {
            SpringUtils.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

}

Spring Boot Application as a Service
https://www.baeldung.com/spring-boot-app-as-a-service

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐