背景
很多時候,我們專案在開發環境和生成環境的環境配置是不一樣的,例如,資料庫配置,在開發的時候,我們一般用測驗資料庫,而在生產環境的時候,我們是用正式的資料,這時候,我們可以利用profile在不同的環境下配置用不同的組態檔或者不同的配置,
解決方案
spring boot允許你通過命名約定按照一定的格式(application-{profile}.properties)來定義多個組態檔,然后通過在application.properyies通過spring.profiles.active來具體激活一個或者多個組態檔,如果沒有沒有指定任何profile的組態檔的話,spring boot默認會啟動application-default.properties,
一、新建組態檔
注:組態檔優先級(由高到低):
bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml
此處使用.yml檔案格式,在src/main/resources下新建如下檔案

application.yml (主配置)
server:
port: 9990
spring:
profiles:
active: dev #選定配置
#自定義默認值
curvar:
context: default.curvar
application-pro.yml (開發配置)
#模擬開發配置
curvar:
context: "開發配置變數"
application-pro.yml(生產配置)
#模擬生產配置
curvar:
context: "生產配置變數"
二、 服務呼叫測驗
2.1 新建呼叫類
@Slf4j @RestController public class IndexController { @Value("${curvar.context}") private String cusvar; /** * . * 使用哪一個配置 * * @return */ @RequestMapping("/test") public String test() { log.debug("使用:[{}]", cusvar); return "使用配置: " + cusvar; } }
2.2 使用樣例專案
打開瀏覽器輸入:http://localhost:9990/test


三、擴展練習
3.1 使用注解標記配置,首先定義一個介面
public interface Connector { String configure(); }
3.2 分別定義倆個實作類來實作它
import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @Component @Profile("pro-db")//標記檔案,環境切換 public class ProConnector implements Connector { @Override public String configure() { return "pro生產標記檔案..."; } }
import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @Component @Profile("dev-db")//標記檔案,環境切換 public class DevConnector implements Connector { @Override public String configure() { return "dev開發標記檔案..."; } }
3.3 修改application.yml檔案激活配置
spring:
profiles:
#active: dev #選定配置
active: pro-db #選定配置激活標記檔案
3.4 新增查詢方法
@Autowired private Connector connector; //注入 /** * . * 使用哪一個被標記檔案 * * @return */ @GetMapping("/proFile") public String proFile() { log.debug("使用組態檔:[{}]", connector.configure()); return connector.configure(); }
打開瀏覽器輸入:http://localhost:9990/proFile

3.5 使用一個或多個組態檔及激活標記檔案
3.5.1 修改application.yml檔案,進行屬性疊加
spring: profiles: include: pro,dev-db #指定組態檔及激活標記檔案 #active: pro-db #選定標記檔案
3.5.2 新增查詢方法
/** * . * 使用哪一個組態檔及標記檔案 * * @return */ @GetMapping("/include") public String include() { return String.format("使用組態檔:%s,使用標記檔案:%s", cusvar, connector.configure()); }
打開瀏覽器輸入:http://localhost:9990/include

3.6 切換日志檔案
3.6.1 新建logback檔案

logback-pro.yml (生產日志)
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true"> <contextName>logback</contextName> <!--定義檔案名及存盤路徑--> <property name="log.path" value="./pro.log"/> <!-- ConsoleAppender:控制臺設定 --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} [%file : %line] - %msg%n </pattern> </encoder> </appender> <!--RollingFileAppender:滾動記錄檔案,先將日志記錄到指定檔案--> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!-- 每產生一個日志檔案,該日志檔案的保存期限為7天 --> <maxHistory>7</maxHistory> </rollingPolicy> <encoder> <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n </pattern> </encoder> </appender> <!--將上述name名稱:console:標簽名稱為debug--> <root level="debug"> <appender-ref ref="console"/> </root> <!--將上述name名稱:file:標簽名稱為info--> <root level="info"> <appender-ref ref="file"/> </root> <logger name="org.springframework.scheduling" level="error"/> <Logger name="org.apache.catalina.util.LifecycleBase" level="error"/> <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn"/> <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn"/> <Logger name="org.springframework" level="info"/> <Logger name="org.freeswitch.esl" level="warn"/> <logger name="java.sql" level="error"/> <logger name="org.mybatis" level="info"/><!--mybatis的日志級別為info--> <logger name="com.example" level="debug"/><!--com.hy包下的日志級別為debug--> </configuration>
3.6.2 修改application.yml檔案,配置日志屬性
spring:
profiles:
#include: pro,dev-db #指定組態檔及激活標記檔案
#active: pro-db #選定標記檔案
active: pro #指定組態檔
#日志
logging:
config: classpath:logback-${spring.profiles.active}.xml #本地IDEA啟動配置
#config: config/logback-${spring.profiles.active}.xml # java -jar 包啟動配置
專案啟動訪問介面,控制臺列印日志

友情提示:jar運行指定配置
java -jar xxx.jar --spring.profiles.active=dev #指定dev配置 java -jar xxx.jar --server.port=9090 #指定啟動埠
參考鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/451987.html
標籤:其他
上一篇:Condition
下一篇:【JavaWeb-jQuery】筆記(1)--- jQuery概述;dom物件和jquery物件;jQuery選擇器;jQuery過濾器
