博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot多环境加载yml和logback配置
阅读量:2062 次
发布时间:2019-04-29

本文共 10926 字,大约阅读时间需要 36 分钟。

大家好,我是烤鸭:
这是一篇关于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.pojojedis :  pool :    host : localhost    port : 9001    password: admin    config :       maxTotal: 100       maxIdle: 10       maxWaitMillis : 100000#pagehelper分页插件pagehelper:   helperDialect: mysql   reasonable: true   supportMethodsArguments: true   params: count=countSqlthread:  pool:      corePoolSize: 10      maxPoolSize: 15      queueCapacity: 20

   主要是集成了mysql,mybatis,redis,logback。配置了线程池参数。

      1.4  logback-dev.xml

   
   
   
   
   
   
       
       
           
           
[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %highlight([%-5level] %logger{50} - %msg%n)
           
UTF-8
           
       
   
   
   
       
       
           
           
${LOG_HOME}/${PROJECT_NAME}.system-dev.%d{yyyy-MM-dd}.%i.log
           
           
15
           
           
10MB
       
       
           
           
[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%-5level] %logger{50} - %msg%n
           
UTF-8
       
   
   
       
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
       
       
   

 2.   pom文件

4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
com.test.test
1.0.0-SNAPSHOT
test_interface
war
test_interface
test项目
1.8
1.8
UTF-8
UTF-8
zh_CN
org.springframework.boot
spring-boot-starter-jdbc
org.codehaus.jackson
jackson-mapper-asl
1.9.13
org.codehaus.jackson
jackson-core-asl
1.9.13
org.jsoup
jsoup
1.11.2
org.projectlombok
lombok
1.16.20
provided
org.dom4j
dom4j
2.1.0
com.alibaba
fastjson
1.2.45
org.apache.httpcomponents
httpclient
4.5.4
org.springframework.boot
spring-boot-starter-web
com.fasterxml.jackson.core
jackson-databind
org.springframework.boot
spring-boot-starter-thymeleaf
mysql
mysql-connector-java
5.1.35
org.springframework.boot
spring-boot-starter-test
provided
redis.clients
jedis
2.9.0
commons-beanutils
commons-beanutils
1.8.3
org.apache.commons
commons-lang3
3.7
com.alibaba
druid
1.0.11
com.github.pagehelper
pagehelper-spring-boot-starter
1.1.2
com.alibaba
druid-spring-boot-starter
1.1.0
org.aspectj
aspectjweaver
1.8.10
dev
dev
true
test
test
pro
pro
myTest
src/main/resources/application-${profiles.active}.yml
src/main/resources/logback-${profiles.active}.xml
true
src/main/resources/
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
org.apache.maven.plugins
maven-jar-plugin
true
false
lib/
com.test.test.TestMainApplication
${project.version}

中间有很多jar包不需要的,自己删掉吧。

3.  main方法

    3.1  TestMainApplication:

/** * @author Binary Wang(https://githpaub.com/binarywang) */@SpringBootApplicationpublic 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_interfacerm -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.warcd /opt/tomcat/tomcat_test_interface/bin/chmod +x *.sh sh ./startup.sh

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-27》17.电话号码的字母组合
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-40》64.最小路径和
查看>>
Leetcode C++《热题 Hot 100-41》75.颜色分类
查看>>
Leetcode C++《热题 Hot 100-42》78.子集
查看>>
Leetcode C++《热题 Hot 100-43》94.二叉树的中序遍历
查看>>
Leetcode C++ 《第175场周赛-1 》5332.检查整数及其两倍数是否存在
查看>>
Leetcode C++ 《第175场周赛-2 》5333.制造字母异位词的最小步骤数
查看>>