Spring could 多环境配置-spring.profiles.active
为了支持多环境,可以配置多个配置文件, 在启动时指定使用那个配置文件, 如下:application-dev.propertiesserver.port=7765spring.application.name=sfgservereureka.client.serviceUrl.defaultZone=http://localhost:7761/eureka/...
·
- 为了支持多环境,可以配置多个配置文件, 在启动时指定使用那个配置文件, 如下:
- application-dev.properties
server.port=7765 spring.application.name=sfgserver eureka.client.serviceUrl.defaultZone=http://localhost:7761/eureka/ spring.main.allow-bean-definition-overriding=true |
- 启动命令中指定环境配置:
java -jar -Dspring.profiles.active=dev sfgserver-0.0.1-SNAPSHOT.jar
java -jar sfgserver-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
- sts 中定义启动
- 在application.properties中指定,application.properties作为一个公共资源,一直被调用的。
spring.profiles.active=dev
dev中定义的属性会覆盖application.properties 定义的属性
- 在代码中指定
- 在config class中,定义component, bean的使用环境
@Profile("dev"), 定义的bean在 dev 环境被创建
@Profile("prod"), 定义的bean在 prod环境被创建
- 多实例在Eureka server
- pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>net.fsd</groupId> <artifactId>sfgserver</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sfgserver</name> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>********+${project.build.directory}\${project.artifactId}-${project.version}.jar</echo> <echo>${project.basedir}</echo> <copy overwrite="true" tofile="${project.basedir}\\..\\..\\sfglib\\${project.artifactId}-${project.version}.jar" file="${project.build.directory}\\${project.artifactId}-${project.version}.jar"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
更多推荐
已为社区贡献2条内容
所有评论(0)