SpringBoot系統列 2 - 配置文件,多環境配置(dev,qa,online)

 實現項目的多環境配置的方法有不少,好比經過在Pom.xml中配置profiles(最多見) 而後在Install項目打War包的時候,根據需求打不一樣環境的包,如圖:html

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

1.添加配置文件web

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

dev:開發環境apache

qa:測試環境api

online:生產環境tomcat

而後在application.yml配置文件中配置默認的運行環境:app

spring:
  profiles:
    active: dev

而後在dev、qa、online中分別配置不一樣的配置內容,例如變動端口:框架

devmaven

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中配置運行環境:

相關文章
相關標籤/搜索