springboot 常用配置之多环境配置
在上一课中我们通过idea工具没有做任何配置就构建了一个springboot项目,并且已经成功启动了,但我们都很清楚这些都远远不能达到我们实际项目的需求,比如我们要引入我们自己的redis配置、mysql配置等,应该如何处理呢?在spring mvc中我们都是通过spring.xml相关文件配置,在springboot中这些都已经不存在了,我们应该怎样配置呢?别急,马上为大家揭晓谜底,跟着我一起来吧!
NO1.我们在做项目的时候是不是都会区分很多环境呢?比如开发环境、测试环境、生产环境等,那么第一步我将先带大家配置好各个环境;
1.首先打开我们项目的pom.xml文件加入以下内容:
<build> <finalName>${project.artifactId}-${project.version}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf8</encoding> </configuration> </plugin> </plugins> <filters> <filter>src/main/resources/application-${filter-resource-name}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>filters/*</exclude> <exclude>filters/*</exclude> <exclude>application-dev.properties</exclude> <exclude>application-test.properties</exclude> <exclude>application-alpha.properties</exclude> <exclude>application-prod.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application-${filter-resource-name}.properties</include> </includes> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <filter-resource-name>dev</filter-resource-name> </properties> </profile> <profile> <id>test</id> <properties> <filter-resource-name>test</filter-resource-name> </properties> </profile> <profile> <id>alpha</id> <properties> <filter-resource-name>alpha</filter-resource-name> </properties> </profile> <profile> <id>prod</id> <properties> <filter-resource-name>prod</filter-resource-name> </properties> </profile> </profiles>
这一段相信大家都很熟悉了吧,我就不多做解释了(有疑问的童鞋可以私信我哦);
2.然后打开application.properties文件,并在其中加入以下内容:
#表示激活的配置文件(dev|prod) spring.profiles.active=@filter-resource-name@
整个项目变成了如下结构:
至此我们的springboot多环境配置已经完成;
3.设置日志级别
#log level logging.level.root=debug
4.设置自定义端口以及实例名
#端口 server.port=8888 #实例名 spring.application.name=demo-springboot
5.logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="demo" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>demo/demo.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滚 daily --> <fileNamePattern>demo/demo.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 日志最大的历史 10天 --> <maxHistory>10</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <logger name="com.example.demo" level="INFO" additivity="false"> <appender-ref ref="demo"/> </logger> <logger name="com.example.demo.dao" level="DEBUG" /> <logger name="com.example.demo.service" level="INFO" /> <logger name="druid.sql.Statement" level="DEBUG" /> <logger name="druid.sql.ResultSet" level="DEBUG" /> <logger name="org.apache" level="INFO" /> <logger name="org.mybatis.spring" level="ERROR" /> <logger name="org.springframework" level="INFO"></logger> <logger name="springfox" level="ERROR"></logger> <root level="INFO"> <appender-ref ref="demo" /> </root> </configuration
至此,我们项目的基本环境配置已经搭建好,通过maven clean install以下选择dev|test|prod打入你指定的配置,然后run application运行,如果通过localhost:8888可以访问说明你的配置worked了;但是这还远远不够,我们项目开发总得操作数据库吧,哈哈 是的,接下来让我们进入springboot + mysql + mybatis的世界吧!