Spring could 多环境配置-spring.profiles.active

  • 为了支持多环境,可以配置多个配置文件, 在启动时指定使用那个配置文件, 如下:

Spring could 多环境配置-spring.profiles.active

  • 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

  • sts 中定义启动

Spring could 多环境配置-spring.profiles.active

  • 多实例在Eureka server

Spring could 多环境配置-spring.profiles.active

  • 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>