springboot项目集成Consul配置中心的多环境方案

多环境部署profile方案:

整体思路是配置maven多环境的profile,打包时使用mvn -P={active-profile}指定想要激活的版本

第一步:配置项目中maven的profiles标签

	<profiles>
        <profile>
            <!-- 本地开发 -->
            <id>local</id>
            <activation>
                <!--默认开发环境-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>local</activatedProperties>
                <activatedConsulHost>localhost</activatedConsulHost>
                <activatedConsulPort>8500</activatedConsulPort>
            </properties>
        </profile>
        <profile>
            <!-- 测试服务器dev2 -->
            <id>dev2</id>
            <properties>
                <activatedProperties>dev2</activatedProperties>
				<!-- dev2 Cousl Host-->
                <activatedConsulHost>gyd-consul</activatedConsulHost>
				<!-- dev2 Cousl Port -->
                <activatedConsulPort>8500</activatedConsulPort>
            </properties>
        </profile>
    </profiles>

第二步:增加maven resources打包插件

		<plugins>	
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--一定要设置成true.这样才会用对应env目录下的配置文件覆盖原来的-->
                <filtering>true</filtering>
                <includes>
                    <include>bootstrap.yml</include>
                </includes>
            </resource>
        </resources>

第三步:用maven profile的配置项替换yml中的配置项

bootstrap.yml只需要指定开启Consul的host和port 并开启配置中心即可。
我的项目中就只有一个bootstrap.yml。没有其他bootstrap-{activeProfile}.yml之类的配置。
在这里插入图片描述

spring:
  profiles:
    active: @activatedProperties@
  application:
    name: gyd-zuul-server
  cloud:
    consul:
      host: @activatedConsulHost@
      port: @activatedConsulPort@
      enabled: true
      config:
        enabled: true
        format: YAML

第四步:修改jenkins构建的mvn命令,增加-P {需要激活的profile}

在这里插入图片描述

第五步:Idea本地开发环境profile的激活在这里插入图片描述

Tips

配置中心Consul使用的默认路径是/config/{application.name}/data
在这里插入图片描述
比如我的项目application.name: gyd-zuul-server
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值