SpringBoot初级开发--多环境配置的集成(9)


  在Springboot的开发中,我们经常要切换各种各样的环境配置,比如现在是开发环境,然后又切换到生产环境,这个时候用多环境配置就是一个明智的选择。接下来我们沿用上一章的工程来配置多环境配置工程。

1.准备多环境配置文件

这里我们准备三个配置文件
在这里插入图片描述
application.properties是通用统一的配置文件,内容如下

server.port=8088

[email protected]@

application-dev.properties是开发用的配置文件,内容如下

server.port=8088

first.web.welcome.name="now is dev"

spring.datasource.dynamic.hikari.pool-name=example-cp
spring.datasource.dynamic.hikari.max-pool-size=20
spring.datasource.dynamic.hikari.max-lifetime=17170000
spring.datasource.dynamic.hikari.connectionInitSql=set names utf8mb4

spring.datasource.dynamic.primary=test
#test???
spring.datasource.dynamic.datasource.test.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.test.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.dynamic.datasource.test.username=root
spring.datasource.dynamic.datasource.test.password=root

spring.datasource.dynamic.datasource.test.hikari.max-pool-size=12
spring.datasource.dynamic.datasource.test.hikari.max-lifetime=15000000



info.application.name="@project.name@"
info.application.description="@project.description@"
info.application.version="@project.version@"

management.server.port=52001

management.endpoints.web.exposure.include=*

management.info.env.enabled=true

#spring.elasticsearch.rest.uris=http://10.10.52.155:9200
#spring.elasticsearch.rest.username=
#spring.elasticsearch.rest.password=
#spring.elasticsearch.rest.connection-timeout=10000
#spring.elasticsearch.rest.read-timeout=30000


spring.elasticsearch.uris=http://10.10.52.155:9200
spring.data.elasticsearch.repositories.cluster-name=elasticsearch
spring.data.elasticsearch.repositories.cluster-nodes=10.10.52.155:9300


spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER



application-pro.properties作为生产配置,和上面的一样的。

2.修改POM.xml文件配置

1.增加profiles和resources的多配置属性的修改,整个pom文件的内容如下。profiles增加了dev,pro两个配置。resoucces包含了编译要包含的文件,配置文件按照,你选择的哪一版本配置加入配置文件。


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.14version>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>firstwebartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>firstwebname>
    <description>firstwebdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.0version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>dynamic-datasource-spring-boot-starterartifactId>
            <version>3.1.1version>
        dependency>
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>2.1.5version>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>dynamic-datasource-spring-boot-starterartifactId>
            <version>2.5.4version>
        dependency>

        
        <dependency>
		        <groupId>org.springframework.bootgroupId>
		        <artifactId>spring-boot-starter-thymeleafartifactId>
         dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-boot-starterartifactId>
            <version>3.0.0version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4jartifactId>
            <version>1.3.8.RELEASEversion>
        dependency>

        <dependency>
            <groupId>com.mysqlgroupId>
            <artifactId>mysql-connector-jartifactId>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-elasticsearchartifactId>
        dependency>

    dependencies>
    <profiles>
        <profile>
            <id>devid>
            <properties>
                <profile.active>devprofile.active>
            properties>
            <activation>
                <activeByDefault>trueactiveByDefault>		
            activation>

        profile>
        <profile>
            <id>proid>
            <properties>
                <profile.active>proprofile.active>
            properties>
        profile>
    profiles>
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>application.propertiesinclude>
                    <include>application-${profile.active}.propertiesinclude>
                    <include>log4j.propertiesinclude>
                    <include>**/*.htmlinclude>
                    <include>**/*.txtinclude>
                    <include>/static/include>
                    <include>/templates/include>
                includes>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

2.修改完配置记得更新idea的maven配置,这样就出现了profiles,你可以选择是dev安装发包,还是pro安装发包。
SpringBoot初级开发--多环境配置的集成(9)_第1张图片
选择好pro版本后,我们直接install。然后maven就把与pro相关的配置全部打包成一个包。
SpringBoot初级开发--多环境配置的集成(9)_第2张图片
这样按照多版本配置去打包就实现了。如果springboot要按版本运行,可以直接把配置加入这里
SpringBoot初级开发--多环境配置的集成(9)_第3张图片
SpringBoot初级开发--多环境配置的集成(9)_第4张图片
程序的源码在这里可以下载到链接: https://pan.baidu.com/s/1e3QSc9COQ-ioJiAxvDlXMg 提取码: cqnw

你可能感兴趣的:(JAVA,springboot,spring,boot,后端,java,spring,多配置)