Springboot2项目配置(热部署+war+外部tomcat+外部配置文件)

7 篇文章 0 订阅

很多人在学习springboot时感叹于它便捷的自动化配置、内置tomcat、jar包启动,当然,这是我们很多人转用boot开发的原因。但是,在有些场景下或者实际工作中,往往有很多原因使我们陷入困扰,比如将boot的jar交给docker部署,比如我们是旧的项目改造,比如我们的配置文件是外部平台来管理,比如我们必须用外部tomcat来运行等等,经常使我们有些初入坑的同学感到头疼。当然,这些问题对于springboot来说都不是问题。springboot除了可以在application.properties配置外,其实是将很多配置交给了代码来管理,我们可以通过继承配置类或者实现配置接口来进行很多的自定义开发方案实现。

本文主要记录一下几种定制化配置

  • 开发/测试/生产多环境配置文件切换
  • 将springboot打成war包
  • 在IDEA中用外部tomcat运行部署
  • 通过启动环境变量读取配置文件
  • IDEA下配置设部署,提高开发效率

环境介绍

  • OS:windows10
  • IDE:IDEA2017.2
  • JDK:1.8
  • MAVEN:3.5

一、开发/测试/生产多环境配置文件切换

在这里插入图片描述

如图,在application配置文件外新建n个后缀自定义的配置文件,然后在application中方不经常变化的默认的系统配置,在新建的配置
文件中放例如像数据库连接这类的需要切换环境的配置;
在application中通过spring.profiles.active=dev可以自由切换不同的环境
二、将springboot打成war包,并用外部tomcat运行部署

首先上pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>com-jum-demo</artifactId>
	<packaging>war</packaging>
	<parent>
		<groupId>com.jum.demo</groupId>
		<artifactId>jum-base</artifactId>
		<version>1.0.0-SNAPSHOT</version>
		<relativePath/>
	</parent>

	<dependencies>
		<!-- 其他你必须的 或者 可能需要的依赖-->
		<!-- tomcat -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		
	</dependencies>

	<build>
		<plugins>
			<!-- spring boot 打包  -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- 如果是war包 并且配置外部配置参数  则打包时可以选择要忽略的配置文件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<packagingExcludes>
						WEB-INF/classes/application.properties
					</packagingExcludes>

				</configuration>
			</plugin>
		</plugins>
		<finalName>demo</finalName>
	</build>
</project>

在pom中排除不打进war的配置文件,指定打包类型为war
然后,修改启动类

package com.jum;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.WebApplicationInitializer;

/**
 * @author zhoujumbo
 *
 */
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer implements WebApplicationInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
		return application.sources(DemoApplication.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

配置外部tomcat

在这里插入图片描述

左上角“+”号添加tomcat>local,右侧面板上部,设置自定义名称(这个名称值是自己给tomcat起的名字,随便都可以
Application server 设置自己的tomcat安装根路径
VM参数选填
保存
进入面板上部tab页Deployment

在这里插入图片描述

如上图,选第一个Artifacts

在这里插入图片描述
在这里插入图片描述

这一步,右侧的Application context是你项目启动时候到名字
启动tomcat

三、通过启动环境变量读取配置文件

基本配置沿用标题二中的基本配置,在此基础上

在任意java包路径下创建任意名称的类(尽量符合你项目的规范),加入如下代码:

package com.jum.config;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
/**
 * 读取外部配置文件
 */
public class ExternalProperties implements EnvironmentPostProcessor {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private static final String  LOCATION = "application.properties";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        logger.info("系统参数>>>"+System.getProperty("DEMO-HOME-CONFIG"));
        File file = new File(System.getProperty("DEMO-HOME-CONFIG"), LOCATION);
        if (file.exists()) {
            MutablePropertySources propertySources = environment.getPropertySources();
            logger.info("Loading local settings from {}" , file.getAbsolutePath());
            Properties properties = loadProperties(file);
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
        }
    }

    private Properties loadProperties(File f) {
        FileSystemResource resource = new FileSystemResource(f);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
        }
    }
}

注意,DEMO-HOME-CONFIG是默认指定的要在环境变量中设置的启动参数名称,可以定义设置;
然后配置项目启动参数:-DDEMO-HOME-CONFIG=D:\temp\config_spaces\
注意:启动参数前是有-D符号的

在这里插入图片描述

resources下建立文件夹META-INF,添加文件spring.factories,添加内容org.springframework.boot.env.EnvironmentPostProcessor=com.jum.config.ExternalProperties
完成

在这里插入图片描述

四、IDEA下配置热部署,提高开发效率

涉及到的几个点

  1. 设置菜单中设置自动编译

在这里插入图片描述
2. 引入devtools相关依赖包,并进行插件参数配置

		<!--热部署-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- tomcat -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- 插件出配置 -->
		<build>
			<finalName>xxx</finalName>
			<plugins>
				<!-- spring boot 打包  -->
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
					<configuration>
						<!--fork :  如果没有该项配置,肯定devtools不会起作用,即应用不会restart -->
						<fork>true</fork>
						<addResources>true</addResources>
					</configuration>
				</plugin>
			</plugins>
		</build>
  1. 如果是war包部署,必须在服务器设置中进行如下选择
    在这里插入图片描述
  2. 打开idea系统配置,勾选自动编译选项。在界面按“shift+ctrl+alt+/”
    在这里插入图片描述
    在这里插入图片描述
    找到“compiler.automake.allow.when.app.running” 勾选,然后close。
  3. 如果是springboot,在配置文件加上相关参数配置
#热部署配置 仅开发环境用
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=resources/**,static/**,basepages/**
spring.devtools.restart.additional-exclude=error/**
#说明:spring.devtools.restart.additional-paths:由于devtool默认是不会监听静态资源的,此处将boot下的静态目录配置上,
#	  其中basepages是自己后来自定义设置的静态目录,可不加或者配置成你自己的

END

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值