2018-03-12
springboot-多环境开发配置
springboot 评论:0 浏览:89

转载请注明出处:https://oldnoop.tech/c/168.html

springboot支持多环境配置的灵活切换

一般大型项目开发可能设置多套服务器环境,

比如

test

测试环境

uat

需求方或客户测试环境

staging

预发布环境

production

生产环境

每套环境都包含了 一整套 常用的服务器,比如mysql,oracle,redis,mongodb,zk,amq,hadoop等等

各企业配置可能还有更多或者不同,

使用springboot
可以编写多份profile配置, 使用application-xxx.properties或者application-xxx.yml
在application.properties或者application.yml中使用哪一份配置profile
通过 spring.profiles.active指定profile的名称 就是application-后面的xxx

举例如下:编写3份配置


编写生产环境配置
application-prod.yml
----------------------------------------------
domain: oldnoop.com
server:
  port: 80
redis:
  host: ${domain}
  port: 6379
memcached:
  host: ${domain}
  port: 11211
mongodb:
  host: ${domain}
  port: 27017

编写开发配置,服务默为8080端口
application-dev.yml
----------------------------------------------
management:
  security:
    enabled: false
host: localhost
redis:
  host: ${host}
  port: 6379
memcached:
  host: ${host}
  port: 11211
mongodb:
  host: ${host}
  port: 27017
 
编编写开发配置,服务为80端口
application-dev-80.yml
----------------------------------------------
management:
  security:
    enabled: false
server:
  port: 80
host: localhost
redis:
  host: ${host}
  port: 6379
memcached:
  host: ${host}
  port: 11211
mongodb:
  host: ${host}
  port: 27017

编写application.yml
----------------------------------------------
spring:
  profiles:
    active: dev

这里在application.yml中指定使用application-dev.yml的配置

运行启动类,可以通过控制台查看启动端口号,

另外,可以通过浏览器,访问 http://localhost:8080/env

查看配置的数据值

除了通过在application的配置中指定 profile,也可以在java进程运行参数中指定

按照springboot 参数配置优先级,命令行启动参数优先级更高

比如在STS的IDE中,如下图所示,指定程序启动参数

 



  • 转载请注明出处:https://oldnoop.tech/c/168.html

Copyright © 2018 oldnoop.tech. All Rights Reserved

鄂ICP备2023022735号-1