码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot实现多环境配置

时间:2018-04-10 21:57:04      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:测试环境   produces   多环境   main   mapping   boot   使用命令   通过   seconds   

1.为什么需要配置多环境配置

在实际的开发中,我们往往需要在不同的环境中使用不同的数据库、缓存配置,如果使用同一套配置文件,在不同环境部署的时候手动去修改配置文件,会使部署变得很繁琐。使用多环境配置文件可以很方便的实现此功能。

1.创建不同环境的配置文件

在resource文件夹中添加一下配置文件:

application-dev.properties //开发环境配置文件
application-rc.properties //线上环境配置文件
application-test.properties //测试环境配置文件

2. 选择使用的配置文件

在resource/application.properties配置文件中添加一下配置项目:

spring.profiles.active=dev

此配置用于选择使用的配置环境,值为application-{profile}.properties中的profile值。

3.使用命令行选择使用的配置文件

SpringBoot还支持通过命令行的方式修改配置,使用方式如下:

java -jar xxx.jar --spring.profiles.active=dev

其中--spring.profiles.active=dev相当于在application.properties文件中加入了此配置。

4.测试多环境配置

我们分别在

application-dev.properties //开发环境配置文件
application-rc.properties //线上环境配置文件
application-test.properties //测试环境配置文件

文件中添加以下配置:

#端口号
server.port=8081
server.port=8082
server.port=8083

依次修改application.properties文件中的以下配置:

spring.profiles.active=dev、test、rc

修改后依次重启服务。
当使用dev的时候控制台输出一下内容:

    2018-04-10 20:29:23.841  INFO 11492 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-10 20:29:23.871  INFO 11492 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-10 20:29:23.871  INFO 11492 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-10 20:29:23.961  INFO 11492 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-10 20:29:24.011  INFO 11492 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path '/demo'
2018-04-10 20:29:24.011  INFO 11492 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.271 seconds (JVM running for 2.608)

使用test的时候控制台输出一下内容:

2018-04-10 20:30:17.458  INFO 12824 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-10 20:30:17.498  INFO 12824 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path '/demo'
2018-04-10 20:30:17.508  INFO 12824 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.268 seconds (JVM running for 2.609)

使用rc的时候输出以下内容:

2018-04-10 20:31:14.302  INFO 11972 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path '/demo'
2018-04-10 20:31:14.312  INFO 11972 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.133 seconds (JVM running for 2.488)

可以看出在服务启动的时候分别用了不同的配置文件。

SpringBoot实现多环境配置

标签:测试环境   produces   多环境   main   mapping   boot   使用命令   通过   seconds   

原文地址:https://www.cnblogs.com/vitasyuan/p/8782612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!