SpringBoot实现多环境配置
2018-04-10 20:36
344 查看
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学习(三)--Spring profile多环境方式实现logback日志配置
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- Springboot使用maven的profiles实现多环境配置文件切换
- spring boot使用logback实现多环境日志配置详解
- Spring Boot+Profile实现不同环境读取不同配置
- SpringBoot环境下配置AOP切面实现权限拦截
- Spring Boot教程 - Spring Boot Profiles实现多环境下配置切换
- spring boot 整合 quartz 集群环境 实现 动态定时任务配置【原】
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- 第三节:SpringBoot使用properties配置文件实现多环境配置
- Spring Boot中实现logback多环境日志配置
- SpringBoot 使用properties配置文件实现多环境配置
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- 【spring boot】配置ssl证书实现https
- Spring Boot 基础知识学习(二)——配置文件多环境配置
- 解决maven工程中使用spring-boot后导致的profile多环境配置失效的问题
- 第十四章 springboot + profile(不同环境读取不同配置)
- SpringBoot+ZooKeeper+ZKUI+Drools 实现应用配置中心及业务规则动态加载
- 加入spring-boot后导致maven的profile多环境配置失效的问题解决
- 使用IDEA从零配置springboot项目环境