1. 程式人生 > >SpringBoot進階教程 | 第一篇:YML多文件塊實現多環境配置

SpringBoot進階教程 | 第一篇:YML多文件塊實現多環境配置

你是否為SpringBoot一個功能多個yml和多個properties檔案區分不同執行環境配置,經常為這些配置檔案的管理而頭疼,現在通過這篇文章,將徹底解決你的煩惱,這篇文篇介紹,怎麼通過yml檔案構建多文件塊,區分不同環境配置,自由切換不同環境啟動專案,一個配置檔案搞定。

準備工作

環境:

windows
jdk 8
maven 3.0
IDEA

構建工程

<?xml version="1.0" encoding="UTF-8"?>
<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>cn.zhangbox</groupId> <artifactId>spring-boot-study</artifactId> <version>1.0-SNAPSHOT</version
>
</parent> <groupId>cn.zhangbox</groupId> <artifactId>spring-boot-02-config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-02-config</name> <description>Demo project for Spring Boot</description
>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--匯入配置檔案處理器,配置檔案進行繫結就會有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

修改YML配置

#選擇哪一個環境的配置
#這裡可以在每個環境配置redis,資料庫(mysql),訊息(kafka)等相關的元件的配置
spring:
  profiles:
    active: prod

#文件塊區分為三個---
---
server:
  port: 8081
spring:
  profiles: test

#文件塊區分為三個---
---
server:
  port: 8082
spring:
  profiles: test

#文件塊區分為三個---
---
server:
  port: 8083
spring:
  profiles: prod

建立啟動類

@SpringBootApplication
public class SpringBootConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootConfigApplication.class, args);
    }
}

控制檯列印

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2018-07-04 15:07:26.214  INFO 14812 --- [           main] c.z.s.SpringBootConfigApplication        : Starting SpringBootConfigApplication on MS-20180428GSYE with PID 14812 (C:\Users\Administrator\Desktop\spring-boot-02-config\target\classes started by Administrator in C:\Users\Administrator\Desktop\spring-boot-02-config)
2018-07-04 15:07:26.219  INFO 14812 --- [           main] c.z.s.SpringBootConfigApplication        : The following profiles are active: prod
2018-07-04 15:07:26.281  INFO 14812 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]3f57bcad: startup date [Wed Jul 04 15:07:26 GMT+08:00 2018]; root of context hierarchy
2018-07-04 15:07:28.988  INFO 14812 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8083 (http)
2018-07-04 15:07:29.029  INFO 14812 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-07-04 15:07:29.031  INFO 14812 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2018-07-04 15:07:29.184  INFO 14812 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-07-04 15:07:29.184  INFO 14812 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2909 ms
2018-07-04 15:07:29.419  INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-07-04 15:07:29.424  INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-04 15:07:29.424  INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-04 15:07:29.424  INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-04 15:07:29.424  INFO 14812 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-04 15:07:30.605  INFO 14812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]3f57bcad: startup date [Wed Jul 04 15:07:26 GMT+08:00 2018]; root of context hierarchy
2018-07-04 15:07:30.731  INFO 14812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-04 15:07:30.732  INFO 14812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-04 15:07:30.777  INFO 14812 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-04 15:07:30.777  INFO 14812 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-04 15:07:30.833  INFO 14812 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-04 15:07:31.166  INFO 14812 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-04 15:07:31.470  INFO 14812 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8083 (http)
2018-07-04 15:07:31.475  INFO 14812 --- [           main] c.z.s.SpringBootConfigApplication        : Started SpringBootConfigApplication in 5.897 seconds (JVM running for 7.324)

至此YML多文件塊多環境配置是不是非常簡單,切換環境只需要修改

spring:
  profiles:
    active: prod

中active對應的環境的值即可。

原始碼地址

寫在最後

歡迎關注喜歡、和點贊後續將推出更多的spring cloud教程,敬請期待。
歡迎關注我的微信公眾號獲取更多更全的學習資源,視訊資料,技術乾貨!
歡迎掃碼關注

公眾號回覆“學習”,拉你進程式設計師技術討論群乾貨資源第一時間分享。

公眾號回覆“視訊”,領取800GJava視訊學習資源。
java學習全套
820G資源

公眾號回覆“全棧”,領取1T前端Java產品經理微信小程式Python等資源合集大放送。
全棧資料
java
python
機器學習
產品經理
接近1T資源

公眾號回覆“慕課”,領取1T慕課實戰學習資源。
慕課實戰大全
php
python
測試
後端
前端
前端
微信
1061G資源

公眾號回覆“實戰”,領取750G專案實戰學習資源。
前後端實戰專案
750實戰資源

公眾號回覆“面試”,領取8G面試實戰學習資源。
JAVA面試實戰視訊

傳智面試講解
8G面試資源

相關推薦

SpringBoot教程 | 第一YML實現環境配置

你是否為SpringBoot一個功能多個yml和多個properties檔案區分不同執行環境配置,經常為這些配置檔案的管理而頭疼,現在通過這篇文章,將徹底解決你的煩惱,這篇文篇介紹,怎麼通過yml檔案構建多文件塊,區分不同環境配置,自由切換不同環境啟動專案,一個

Python第一Python簡介

代碼 簡潔 處理 ros 進一步 基礎 得到 運行速度 動態 Python簡介 1.Python的由來 Python是著名的“龜叔”Guido van Rossum在1989年聖誕節期間,為了打發無聊的聖誕節而編寫的一個編程語言。 2.C 和 Python、Java、C#等

CodeIgniter 入門教程第一信息發布

per 所有 控制 知識點 image doc ref har 提交 一、MVC CodeIgniter 采用MVC架構即:控制層、模型層和視圖層。 對應Application下面的文件夾 (圖1): 所有新建文件以.php結尾 視圖層

業余草 SpringCloud 教程 | 第一 服務的註冊與發現Eureka(Finchley版本)

只需要 版本控制 aps 選擇 framework 電腦 右鍵 org -c 一、spring cloud簡介 鑒於《史上最簡單的Spring Cloud教程》很受讀者歡迎,再次我特意升級了一下版本,目前支持的版本為Spring Boot版本2.0.3.RELEASE,S

自動駕駛模擬 PresSan教程第一課——場景建立

自動駕駛、智慧駕駛成為如今的熱門話題,Prescan作為目前應用甚廣的一款自動駕駛/智慧駕駛模擬軟體已獲得越來越多汽車工程師的青睞,目前對於PreScan的使用缺乏一套詳盡且更貼近實操的教程。本系列教程根據作者在使用過程中遇到的諸多問題總結而來,供大家學習參考。 Presc

nginx教程第一nginx安裝、配置

一、簡介 為什麼選擇Nginx? 更快 這表現在兩個方面: 一方面, 在正常情況下, 單次請求會得到更快的響應; 另一方面, 在高峰期( 如有數以萬計的併發請求) , Nginx可以比其他Web伺服器

Spring Cloud系列教程 | 第一微服務架構演進

架構的演變過程: 單體架構——>垂直架構——>soa面向服務架構——>微服務架構 我們為什麼要使用Spring Cloud? 單體架構 在網站開發的前期,專案面臨的流量相對較少,單一應用可以實現我們所需要的功能,從而減少開發、部署和維護的難度。

史上最簡單的 SpringCloud 教程 | 第一 服務的註冊與發現(Eureka)

一、spring cloud簡介 spring cloud 為開發人員提供了快速構建分散式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理

史上最簡單的 SpringCloud 教程 | 第一 服務的註冊與發現Eureka(Finchley版本)

一、spring cloud簡介 鑑於《史上最簡單的Spring Cloud教程》很受讀者歡迎,再次我特意升級了一下版本,目前支援的版本為Spring Boot版本2.0.3.RELEASE,Spring Cloud版本為Finchley.RELEASE。

SpringMVC實戰教程 | 第一一個在JSP頁面輸出“HelloWorld”的Spring MVC例項

配置web.xml 作用:裝載DispatcherServlet類,讀取Spring配置檔案,設定一些初始化引數,加入標記庫,設定比如 * .do 、* .form的對映等。 <servlet> <servlet-nam

python教程 第一步-何韜-專題視訊課程

python進階教程 第一步—269人已學習 課程介紹        Python 語言是計算機工程、大資料及人工智慧等領域的基礎性語言,廣泛且深刻地影響著資訊科技各領域的發展方式及速度,從軟體開發到硬體開發、從資料科學到智慧技術、從應用實踐到科技創新,它無處不在!Pyth

SQL Server索引第十三Insert,Update,Delete語句

    索引設計是資料庫設計中比較重要的一個環節,對資料庫的效能其中至關重要的作用,但是索引的設計卻又不是那麼容易的事情,效能也不是那麼輕易就獲取到的,很多的技術人員因為不恰當的建立索引,最後使得其效果適得其反,可以說“成也索引,敗也索引”。 本系列文章來自Stairway to SQL Server I

Python第一】socket

1.socket()模組函式 要使用socket.socket()函式來建立套接字。其語法如下: socket.socket(socket_family,socket_type,protocol=0) socket_family可以是如下引數:   s

SpringCloud 教程 | 第一 服務的註冊與發現(Eureka)

一、spring cloud簡介spring cloud 為開發人員提供了快速構建分散式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件匯流排、全域性鎖、決策競選、分散式會話等等。它執行環境簡單,可以在開發人員的電腦上跑。另外說明spring cloud是基

SpringBoot教程(六十)intellij idea project下建個module搭建架構(上)

在 IntelliJ IDEA 中,沒有類似於 Eclipse 工作空間(Workspace)的概念,而是提出了Project和Module這兩個概念。多module有一個父maven工程,多個子工程。在多個子工程中,可能有一個web工程,也可能有多個web工程。這樣的好處在於大大解耦各個module

SpringBoot教程(六十一)intellij idea project下建個module搭建架構(下)

在上一篇文章《SpringBoot進階教程(六十)intellij idea project下建多個module(上)》中,我們已經介紹了在intellij idea中建立project之後再分化多個module,今天再大致介紹介紹各個module之間詳細工作的細分。 如果是不考慮細分多個module

SpringBoot教程(六十四)註解大全

在Spring1.x時代,還沒出現註解,需要大量xml配置檔案並在內部編寫大量bean標籤。Java5推出新特性annotation,為spring的更新奠定了基礎。從Spring 2.X開始spring將xml配置中的物件ioc過程轉化成了註解。Spring Boot之所以能夠輕鬆地實現應用的建立及與其

SpringBoot教程(六十八)Sentinel實現限流降級

前面兩篇文章nginx限流配置和SpringBoot進階教程(六十七)RateLimiter限流,我們介紹瞭如何使用nginx和RateLimiter限流,這篇文章介紹另外一種限流方式---Sentinel。 Sentinel 是阿里中介軟體團隊開源的,面向分散式服務架構的輕量級高可用流量控制組件,主要以

SpringBoot教程(六十九)ApplicationContextAware

在某些特殊的情況下,Bean需要實現某個功能,但該功能必須藉助於Spring容器才能實現,此時就必須讓該Bean先獲取Spring容器,然後藉助於Spring容器實現該功能。為了讓Bean獲取它所在的Spring容器,可以讓該Bean實現ApplicationContextAware介面。Applicat

SpringBoot教程(七十)SkyWalking

流行的APM(Application Performance Management工具有很多,比如Cat、Zipkin、Pinpoint、SkyWalking。優秀的監控工具還有很多,其它比如還有zabbix、prometheus、Arthas、Grafana之類的。這裡主要介紹SkyWalking,它是