大家好,我是烤鸭:
这是一篇关于springboot多环境加载yml和logback配置文件。
环境:
这是一篇关于springboot多环境加载yml和logback配置文件。
环境:
开发工具 idea(推荐)/eclipse(对yml支持不好)
jdk 1.8
springboot 1.5.6.RELEASE
1. yml和logback文件
1.1 结构,如图所示:
1.2 application.yml (默认加载的初始化文件)
#开发环境配置
spring:
profiles:
# active: dev
active: @profiles.active@
1.3 application-dev/test/pro.yml
logging:
level:
org.springframework.web: DEBUG,CONSOLE
config: classpath:logback-dev.xml
#查看springboot开启了哪些配置
debug: true
#server:
# port: 8131 #配置程序端口,默认为8080
# session-timeout: 5000 #用户会话session过期时间,以秒为单位
# context-path: #配置访问路径,默认为/
spring:
datasource:
name: dev
url: jdbc:mysql://localhost:3306/jeesite?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:com.test.test.mapper/*.xml
type-aliases-package: com.test.test.pojo
jedis :
pool :
host : localhost
port : 9001
password: admin
config :
maxTotal: 100
maxIdle: 10
maxWaitMillis : 100000
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
thread:
pool:
corePoolSize: 10
maxPoolSize: 15
queueCapacity: 20
主要是集成了mysql,mybatis,redis,logback。配置了线程池参数。
1.4 logback-dev.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<!-- 项目名称 -->
<property name="PROJECT_NAME" value="bq_interface" />
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<property name="LOG_HOME" value="/opt/logs/bq_interface" />
<!-- 控制台输出 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<!--<withJansi>true</withJansi>-->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %highlight([%-5level] %logger{50} - %msg%n)</pattern>
<charset>UTF-8</charset>
<!-- <charset>GBK</charset> -->
</encoder>
</appender>
<!-- 按照每天生成日志文件 -->
<appender name="SYSTEM_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 过滤器,只打印ERROR级别的日志 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.system-dev.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>15</MaxHistory>
<!--日志文件最大的大小-->
<MaxFileSize>10MB</MaxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%-5level] %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="system_error" additivity="true">
<appender-ref ref="SYSTEM_FILE"/>
</logger>
<!-- 自己打印的日志文件,用于记录重要日志信息 -->
<!-- <appender name="MY_INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> -->
<!-- 过滤器,只打印ERROR级别的日志 -->
<!-- <filter class="ch.qos.logback.classic.filter.LevelFilter"> -->
<!-- <level>INFO</level> -->
<!-- <onMatch>ACCEPT</onMatch> -->
<!-- <onMismatch>DENY</onMismatch> -->
<!-- </filter> -->
<!-- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> -->
<!-- 日志文件输出的文件名 -->
<!-- <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}.my_info.%d{yyyy-MM-dd}.%i.log</FileNamePattern> -->
<!-- 日志文件保留天数 -->
<!-- <MaxHistory>15</MaxHistory> -->
<!-- 日志文件最大的大小 -->
<!-- <MaxFileSize>10MB</MaxFileSize> -->
<!-- </rollingPolicy> -->
<!-- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> -->
<!-- 格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<!-- <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%-5level] %logger{50} - %msg%n</pattern> -->
<!-- <charset>UTF-8</charset> -->
<!-- </encoder> -->
<!-- </appender> -->
<!-- <logger name="my_info" additivity="true"> -->
<!-- <appender-ref ref="MY_INFO_FILE"/> -->
<!-- </logger> -->
<!-- 设置Spring&Hibernate日志输出级别 -->
<logger name="org.springframework" level="WARN" />
<logger name="org.mybatis" level="WARN" />
<logger name="com.ibatis" level="DEBUG" />
<logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" />
<logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" />
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" />
<logger name="java.sql.Connection" level="DEBUG" />
<logger name="java.sql.Statement" level="DEBUG" />
<logger name="java.sql.PreparedStatement" level="DEBUG" />
<logger name="com.ruidou.baoqian.mapper" level="DEBUG" />
<!-- 开发环境下的日志配置 -->
<root level="INFO,DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="SYSTEM_FILE" />
</root>
</configuration>
2. pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<groupId>com.test.test</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>test_interface</artifactId>
<packaging>war</packaging>
<name>test_interface</name>
<description>test项目</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.45</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
<profiles>
<profile>
<!--本地开发环境-->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!--测试环境-->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!--生产环境-->
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
</properties>
</profile>
</profiles>
<build>
<finalName>myTest</finalName> <!-- 指定package生成的文件名为my-spring-boot.jar -->
<filters>
<filter>src/main/resources/application-${profiles.active}.yml</filter>
<filter>src/main/resources/logback-${profiles.active}.xml</filter>
</filters>
<!-- 替换${key}内容 -->
<resources>
<resource>
<filtering>true</filtering>
<!-- 要到达最底层目录 -->
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.test.TestMainApplication</mainClass>
</manifest>
<manifestEntries>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
中间有很多jar包不需要的,自己删掉吧。
3. main方法
3.1 TestMainApplication:
/**
* @author Binary Wang(https://githpaub.com/binarywang)
*/
@SpringBootApplication
public class TestMainApplication {
public static void main(String[] args) {
SpringApplication.run(RootConfiguration.class, args);
}
}
3.2 RootConfiguration:
package com.test.test.config;
import com.test.test.constants.IDBConstant;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan(value = "com.test.test", excludeFilters = { @Filter(Controller.class),
@Filter(type = FilterType.ASSIGNABLE_TYPE, value = { RootConfiguration.class }) })
@MapperScan({"com.test.test.dao"})
public class RootConfiguration extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {
//配置tomcat启动端口号
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8131);
container.setSessionTimeout(30);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RootConfiguration.class);
}
@PostConstruct
public void postConstruct() {
IDBConstant.threadPool = Executors.newFixedThreadPool(20);
}
}
4. maven打包
clean package -Ptest -U
这样生成的war包就加载指定的yml和logback文件了。-Ptest指的是加载profiles.active=test的配置文件。
之前一直想打成jar包启动
nohup java -jar ./myTest-1.0-SNAPSHOT.jar -profiles.active=pro &
无奈还是无法加载指定的配置文件。
没办法就打成了war包放到tomcat下了。
5. jenkins/hudson 打包脚本
cd /opt/source/test_interface
rm -rf ./*
cp -rf /root/.hudson/jobs/test_interface/workspace/target/myTest.war ./
kill -9 `ps aux | grep myTest| grep -v grep | awk '{print $2}'`
unzip -o myTest.war
cd /opt/tomcat/tomcat_test_interface/bin/
chmod +x *.sh
sh ./startup.sh