美文网首页
spring非boot项目集成配置中心

spring非boot项目集成配置中心

作者: 孤月夜行舟 | 来源:发表于2019-01-08 09:58 被阅读0次

在spring非boot项目中你的配置文件位置通常是这样配置的

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:config/app.properties</value>
            </list>  
        </property>  
    </bean>

要换成使用配置中心其实非常简单,只需要把classpath:的地址改成easyconf的http地址就可以了。来看一下

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                  <!--相信这个url,你在看了其他文章以后知道是什么意思-->
                   <value>http://106.12.26.82:8083/config/getConfig/alliance-missions/dev/1.0.0</value>
            </list>  
        </property>  
    </bean>

这里你肯定会有疑问,这样配置的话,那本地开发环境怎么弄呢,难道要在配置中心里加一个本地的local配置吗?当然是不需要的,解决办法出奇的简单,就是使用环境变量。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list> 
                    <value>${config_url:classpath:config/app.properties}</value>
            </list>  
        </property>  
    </bean>

这就是环境变量+默认值的方式,默认使用classpath下的配置,本地开发的时候正好是这种场景,部署到测试或者生产环境时,只需要设置环境变量config_url就好了。而这个环境变量的设置可以在你的部署脚本里写好。

可以说配置中心 easyconf 对项目的入侵几乎到零了。怎么样,想不想撸一把试试呢。

相关文章

网友评论

      本文标题:spring非boot项目集成配置中心

      本文链接:https://www.haomeiwen.com/subject/rtkerqtx.html