springboot中多環(huán)境配置的示例分析

這篇文章主要介紹了springboot中多環(huán)境配置的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)是專業(yè)的焉耆網(wǎng)站建設(shè)公司,焉耆接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行焉耆網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

NO1.我們在做項目的時候是不是都會區(qū)分很多環(huán)境呢?比如開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境等,那么第一步我將先帶大家配置好各個環(huán)境;

1.首先打開我們項目的pom.xml文件加入以下內(nèi)容:

<build>
  <finalName>${project.artifactId}-${project.version}</finalName>
  <plugins>
   <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <executions>
      <execution>
        <goals>
         <goal>repackage</goal>
        </goals>
      </execution>
     </executions>
   </plugin>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.3</version>
     <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <encoding>utf8</encoding>
     </configuration>
   </plugin>
  </plugins>
  <filters>
   <filter>src/main/resources/application-${filter-resource-name}.properties</filter>
  </filters>
  <resources>
   <resource>
     <directory>src/main/resources</directory>
     <filtering>true</filtering>
     <excludes>
      <exclude>filters/*</exclude>
      <exclude>filters/*</exclude>
      <exclude>application-dev.properties</exclude>
      <exclude>application-test.properties</exclude>
      <exclude>application-alpha.properties</exclude>
      <exclude>application-prod.properties</exclude>
     </excludes>
   </resource>
   <resource>
     <directory>src/main/resources</directory>
     <filtering>true</filtering>
     <includes>
      <include>application-${filter-resource-name}.properties</include>
     </includes>
   </resource>
  </resources>
</build>
<profiles>
  <profile>
   <id>dev</id>
   <activation>
     <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
     <filter-resource-name>dev</filter-resource-name>
   </properties>
  </profile>
  <profile>
   <id>test</id>
   <properties>
     <filter-resource-name>test</filter-resource-name>
   </properties>
  </profile>
  <profile>
   <id>alpha</id>
   <properties>
     <filter-resource-name>alpha</filter-resource-name>
   </properties>
  </profile>
  <profile>
   <id>prod</id>
   <properties>
     <filter-resource-name>prod</filter-resource-name>
   </properties>
  </profile>
</profiles>

這一段相信大家都很熟悉了吧,我就不多做解釋了(有疑問的童鞋可以私信我哦);

2.然后打開application.properties文件,并在其中加入以下內(nèi)容:

#表示激活的配置文件(dev|prod)
spring.profiles.active=@filter-resource-name@

整個項目變成了如下結(jié)構(gòu):

springboot中多環(huán)境配置的示例分析

至此我們的springboot多環(huán)境配置已經(jīng)完成;

3.設(shè)置日志級別

#log level
logging.level.root=debug

4.設(shè)置自定義端口以及實例名

#端口
server.port=8888
#實例名
spring.application.name=demo-springboot

5.logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml" />
  <appender name="demo" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>demo/demo.log</file>
   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
     <!-- 按天回滾 daily -->
     <fileNamePattern>demo/demo.%d{yyyy-MM-dd}.log</fileNamePattern>
     <!-- 日志最大的歷史 10天 -->
     <maxHistory>10</maxHistory>
   </rollingPolicy>
   <encoder charset="UTF-8">
     <pattern>${FILE_LOG_PATTERN}</pattern>
   </encoder>
  </appender>
  <logger name="com.example.demo" level="INFO" additivity="false">
   <appender-ref ref="demo"/>
  </logger>
  <logger name="com.example.demo.dao" level="DEBUG" />
  <logger name="com.example.demo.service" level="INFO" />
  <logger name="druid.sql.Statement" level="DEBUG" />
  <logger name="druid.sql.ResultSet" level="DEBUG" />
  <logger name="org.apache" level="INFO" />
  <logger name="org.mybatis.spring" level="ERROR" />
  <logger name="org.springframework" level="INFO"></logger>
  <logger name="springfox" level="ERROR"></logger>
  <root level="INFO">
   <appender-ref ref="demo" />
  </root>
</configuration

感謝你能夠認真閱讀完這篇文章,希望小編分享的“springboot中多環(huán)境配置的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

文章名稱:springboot中多環(huán)境配置的示例分析
鏈接地址:http://muchs.cn/article40/ihpoeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、商城網(wǎng)站響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)搜索引擎優(yōu)化、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)