美文网首页
SpringBoot项目配置放项目外部【2】

SpringBoot项目配置放项目外部【2】

作者: 伊夫_艾尔斯 | 来源:发表于2020-03-07 17:31 被阅读0次

    正式开始对接外部配置文件

    先以项目运行端口\color{red}{server.port}配置作为说明,其他(数据库,Kafaka...)的配置引入方式一样

    1. 增加一个application-out.properties作为过渡外部配置文件

    \color{blue}{虽然可以没有这个,但建议使用,把可变和不变的配置项区分开,方便管理}

    #SpringBoot项目外部配置过渡文件
    server.port=${config.out.server-port:8002}
    
    

    \color{red}{server.port}这里配置使用了动态取值,如果取不到会使用默认值:8002

    2. 配置SpringBoot的application.properties
    #SpringBoot项目主配置文件
    spring.profiles.active=out
    
    server.port=8001
    

    \color{red}{spring.profiles.active}是SpringBoot项目支持切换配置文件的参数,官方最初的使用方式是有多个
    配置文件

    • application-dev.properties : 开发环境配置文件
    • application-test.properties : 测试环境配置文件
    • application-prod.properties : 生产环境配置文件

    每次打包前更改此参数分别为:dev,test,prod即可,也可在启动jar的时候以参数的方式实现切换配置,但项目如果打war包放tomcat就不行了。
    这里利用了一下这个功能,实现配置项动静分离(姑且这么描述)

    \color{red}{server.port}由于out配置文件也配了这个优先级更高,所以此处不会生效,只是为了确认out配置文件生效没。

    \color{red}{运行结果}(贴得多,感受一下springboot的启动画面):

    The following profiles are active: out | 代表配置起效了,由于还未配置外部文件,所以使用了out的默认值8002。

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.2.5.RELEASE)
    
    2020-03-07 16:53:25.178  INFO 13164 --- [           main] c.s.demo.SpringbootConfigoutApplication  : Starting
    2020-03-07 16:53:25.182  INFO 13164 --- [           main] c.s.demo.SpringbootConfigoutApplication  : The following profiles are active: out
    2020-03-07 16:53:26.489  INFO 13164 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8002 (http)
    2020-03-07 16:53:26.501  INFO 13164 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-03-07 16:53:26.501  INFO 13164 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
    2020-03-07 16:53:26.624  INFO 13164 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-03-07 16:53:26.624  INFO 13164 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1352 ms
    2020-03-07 16:53:26.832  INFO 13164 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-03-07 16:53:27.046  INFO 13164 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8002 (http) with context path ''
    2020-03-07 16:53:27.050  INFO 13164 --- [           main] c.s.demo.SpringbootConfigoutApplication  : Started SpringbootConfigoutApplication in 2.555 seconds (JVM running for 4.78)
    
    3. 新增项目外部配置文件:configout_setting.properties
    #springboot-configout项目外部配置文件
    config.out.server-port=8003
    
    4. 新增项目外部配置文件加载器
    package com.skxe.demo.config;
    
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @PropertySource(value = "file:${user.dir}/config/configout_setting.properties", encoding = "UTF-8")
    public class ApplicationOutConfigLoader {
        static {
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("                           ------->>> 加载项目配置文件:"+System.getProperty("user.dir")+"/config/configout_setting.properties <<<-------");
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        }
    
    }
    

    \color{red}{@PropertySource}
    \color{red}{value} : 可以加载classpath和file定位的文件(具体介绍可以看源码注释 ) ,
    我们使用了\color{red}{file:/*/out.properties} 这种资源定位方式,配合\color{red}{user.dir} 实现了相对路径,也可直接写成绝对路径(但要保证所有使用到项目的地方都要有这个路径的文件)
    \color{red}{encoding } : 默认是按Unicode编码方式读取文件,我的文件时UTF-8编码,所以必须配置

    • 静态方式中打印的日志,主要是为了确认我们配置文件的真实绝对路径。

    再次启动项目,确认运行日志:Tomcat started on port(s): \color{red}{8003} (http) with context path ''

    到这里为止,引入项目外部配置已确认成功!

    但目前只是把外部配置读到springboot运行中,并可以动态配置给application-out.properties的配置项;
    实际项目中,我们可能还有部分配置需要读取到项目中,已Java代码的方式获取到。

    相关文章

      网友评论

          本文标题:SpringBoot项目配置放项目外部【2】

          本文链接:https://www.haomeiwen.com/subject/yhwcdhtx.html