1. 程式人生 > >SpringBoot系統列 2 - 配置檔案,多環境配置(dev,qa,online) SpringBoot系統列 1 - HelloWorld!

SpringBoot系統列 2 - 配置檔案,多環境配置(dev,qa,online) SpringBoot系統列 1 - HelloWorld!

 實現專案的多環境配置的方法有很多,比如通過在Pom.xml中配置profiles(最常見) 然後在Install專案打War包的時候,根據需求打不同環境的包,如圖:

這種配置多環境的方法在SSM框架中使用的最多,但在SpringBoot中使用最多的是在啟動SpringBoot專案的時候指定執行環境,下面也是主要描述這種配置的方法:

1.新增配置檔案

在SpringBoot的Resources目錄下建4個配置檔案 application.yml、application-dev.yml、application-qa.yml、application-online.yml

dev:開發環境

qa:測試環境

online:生產環境

然後在application.yml配置檔案中配置預設的執行環境:

spring:
  profiles:
    active: dev

然後在dev、qa、online中分別配置不同的配置內容,例如變更埠:

dev

server:
  port: 8085
  servlet:
    context-path: /api
  tomcat:
    max-threads: 100
  connection-timeout: 5000
spring:
  profiles: dev

qa

server:
  port: 
8086 servlet: context-path: /api tomcat: max-threads: 100 connection-timeout: 5000 spring: profiles: qa

online

server:
  port: 8087
  servlet:
    context-path: /api
  tomcat:
    max-threads: 100
  connection-timeout: 5000
spring:
  profiles: online

然後在 SpringBoot系統列 1 - HelloWorld!

  的基礎上繼續新增程式碼,新建WebConfig用於存放SpringBoot的一些配置資訊(SpringBoot的配置即可以在配置檔案中配置,也可以在類中配置):

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;

/**
 * 配置類
 * @author XIHONGLEI
 * @date 2018-10-31
 */
@SpringBootConfiguration
public class WebConfig {

    @Value("${server.port}")
    public String port;
}

然後改造一下HelloContrlller,為了區分環境,我們在請求/api/hello的時候將埠號展示出:

import com.hello.WebConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

    @Autowired
    private WebConfig webConfig;

    @RequestMapping("hello")
    public String hello() {
        return "Hello World! port:".concat(webConfig.port);
    }
}

然後在pom.xml配置Jar包的打包配置:

<packaging>jar</packaging>
    <build>
        <finalName>spring-boot-hello</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>com.hello.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

然後Install,找打Jar包 spring-boot-hello.jar;

在Window控制檯或者Linux中可以使用java -jar spring-boot-hello.jar來啟動SpringBoot專案,然後通過在後方新增--spring.profiles.active來指定啟動SpringBoot專案時使用的環境:

# Dev環境
$ java -jar spring-boot-hello.jar --spring.profiles.active=dev

# qa環境
$ java -jar spring-boot-hello.jar --spring.profiles.active=qa

# online環境
$ java -jar spring-boot-hello.jar --spring.profiles.active=online

例啟動Online環境:

然後通過 http://localhost:8087/api/hello 來訪問,因為Online中配置的埠是8087

完成!

 

在IDEA中怎麼在執行的時候選定執行環境,可以通過配置Application的program arguments中配置執行環境: