当前位置:   article > 正文

springBoot配置文件(四)application常见配置

springBoot配置文件(四)application常见配置

一、命令行参数

1、server.address=xxx.xxx.xx.xxx  

    服务器绑定ip地址,多网卡时可以指定

2、server.port=xxx 

 可以指定springboot内嵌容器启动的端口,默认使用tomcat容器时在8080端口,右键run- java application/springboot..,可以支持不同的容器,在引入不同的依赖时。当server.port=0时,表示自动扫面获取一个可用的端口。

3、ssl的安全访问配置:

  1. server.port=8443
  2. #ssl的安全访问配置
  3. server.ssl.key-store=classpath:keystore.jks
  4. server.ssl.key-store-password=secret
  5. server.ssl.key-password=another-secret

注意: 目前spring-boot不支持http和https同时启用的情况,只支持使用其中一个,如果需要同时使用,可以使用其他形式的实现方式。

该部分对应org.springframework.boot.autoconfigure.webServerProperties类。

此外还有一些不是很常用的如:server.http2.enable=true/false//该属性可以支持http2的协议类型,目前只支持tomcat和undertow的容器并且需要JDK1.8+,官文上对于内嵌tomcat的配置参数也有很多。

二、开发/测试/生产环境配置:

1、语法:

spring.profiles.active=xxxx

  //该系统变量可以指明要使用的配置文件,一般应用于多环境配置分离,如生产环境(production),开发环境(development),测试环境(test)等,可以自定义,如开发环境配置文件为application-dev.properties,则spring.profiles.active=dev,在启动时会加载application-dev.properties配置文件。

2、使用方法:

(1)手动指定:这种方法切换环境需要修改配置文件,不够方便

  1. spring.profiles.active = {profile}
  2. #如spring.profiles.active = prod​

 (2)打包自动指定。

spring.profiles.active=@spring.profiles.active@

3、demo:

启动类:

  1. package com;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class ProfilesApplication {
  6. public static void main(String args[]){
  7. SpringApplication.run(ProfilesApplication.class,args);
  8. }
  9. }

多环境配置文件:

开发环境application-dev.properties:

zt.profiles = this is dev

 测试环境application-test.properties

zt.profiles = this is test

生产环境application-prod.properties 

zt.profiles = this is prod

接口:

  1. package com.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class TestController {
  7. @Value("${zt.common}")
  8. private String common;
  9. @Value("${zt.profiles}")
  10. private String profilesStr;
  11. @RequestMapping("/test")
  12. public String getStr(){
  13. return "公共:"+common+";环境:"+profilesStr;
  14. }
  15. }

下面分别列下手动、自动方法的区别写法:

(1)手动方法:

 pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.profiles</groupId>
  7. <artifactId>profiles-demo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.4.1.RELEASE</version>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. </dependencies>
  20. </project>

 默认配置文件:

  1. server.port= 8888
  2. server.context-path=/profiles
  3. spring.profiles.active=prod
  4. zt.common = conmmon

mvn clean package打包测试一下: 

   

这时候我们打包带参数,如mvn clean package -Ptest、mvn clean package -Pdev、mvn clean package -Pprod,打包后的仍然是prod,就是说必须手动修改application.properties中的环境。

(2)自动方法:

在pom中添加以下配置:

  1. <profiles>
  2. <!-- 测试环境 -->
  3. <profile>
  4. <id>test</id>
  5. <activation>
  6. <activeByDefault>true</activeByDefault>
  7. </activation>
  8. <properties>
  9. <spring.profiles.active>test</spring.profiles.active>
  10. </properties>
  11. </profile>
  12. <!-- 开发环境 -->
  13. <profile>
  14. <id>dev</id>
  15. <activation>
  16. <activeByDefault>false</activeByDefault>
  17. </activation>
  18. <properties>
  19. <spring.profiles.active>dev</spring.profiles.active>
  20. </properties>
  21. </profile>
  22. <!-- 生产环境 -->
  23. <profile>
  24. <id>prod</id>
  25. <activation>
  26. <activeByDefault>false</activeByDefault>
  27. </activation>
  28. <properties>
  29. <spring.profiles.active>prod</spring.profiles.active>
  30. </properties>
  31. </profile>
  32. </profiles>

 并把application.properties中的profiles指定改为

spring.profiles.active=@spring.profiles.active@

这时候打包使用mvn clean package -Ptest、mvn clean package -Pdev、mvn clean package -Pprod,打出来的就是指定环境的包了。pom文件中test一项默认是true,所以如果不指定环境直接使用mvn clean package打包就是test环境。
idea启动则可以点击右侧的maven,选择环境勾上,再启动服务即可:

启动服务访问的为prod环境:

再去掉prod,勾上test:

重启服务访问的为test:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40456
推荐阅读
相关标签
  

闽ICP备14008679号