springboot多环境加载yml和logback配置

大家好,我是烤鸭:
这是一篇关于springboot多环境加载yml和logback配置文件。


环境:

开发工具 idea(推荐)/eclipse(对yml支持不好)

        jdk  1.8

springboot  1.5.6.RELEASE

1. yml和logback文件

    1.1 结构,如图所示:

springboot多环境加载yml和logback配置_第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



    
    


    
    


    
    
        
        
            
            [%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)
 */
@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


你可能感兴趣的:(JAVA,springboot)