zoukankan      html  css  js  c++  java
  • Spring MVC 集成Disconf

    1、Disconf:Distributed Configuration Management Platform(分布式配置管理平台),专注于各种「分布式系统配置管理」的「通用组件」和「通用平台」, 提供统一的「配置管理服务」

    2、配置步骤:

      1.maven中添加jar依赖:

    <dependency>
        <groupId>com.baidu.disconf</groupId>
        <artifactId>disconf-client</artifactId>
    </dependency>

      2.spring配置文件配置(配置在springmvc的主配置文件中):

    <!-- 使用disconf必须添加以下配置 -->
        <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean" destroy-method="destroy">
            <property name="scanPackage" value="com.sf" />
        </bean>
        <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond" init-method="init" destroy-method="destroy">
        </bean>
    
        <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload) -->
            <bean id="configproperties_disconf"
            class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
            <property name="locations">
                <list>
                    <!--多个动态配置配置在这里-->
                    <value>classpath:redis.properties</value>
                </list>
            </property>
        </bean>
    
        <bean id="propertyConfigurer"
            class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
            <property name="ignoreResourceNotFound" value="true" />
            <property name="ignoreUnresolvablePlaceholders" value="true" />
            <property name="propertiesArray">
                <list>
                    <ref bean="configproperties_disconf" />
                </list>
            </property>
        </bean>

      3.在resources文件夹下增加 disconf.properties 配置文件配置disconf:

    #
    # 配置服务器的 HOST,用逗号分隔  127.0.0.1:8000,127.0.0.1:8000
    #
    conf_server_host=XXXXXX
    
    # 版本, 请采用 X_X_X_X 格式
    version=1_0_0_0
    
    # APP 请采用 产品线_服务名 格式
    app=XXXXX
    
    # 是否使用远程配置文件
    # true(默认)会从远程获取配置 false则直接获取本地配置
    enable.remote.conf=true
    
    # 环境
    env=rd
    
    # debug
    debug=true
    
    # 忽略哪些分布式配置,用逗号分隔
    ignore=
    
    # 获取远程配置 重试次数,默认是3次
    conf_server_url_retry_times=3
    
    # 获取远程配置 重试时休眠时间,默认是5秒
    conf_server_url_retry_sleep_seconds=1

    4.添加properties配置文件:

    redis.ip=127.0.0.1
    redis1.port=8888
    redis1.password=123456

    5.使用spring的注解@Value来读取配置文件,并注入值

    @Repository
    public class RedisConfig {
    
       @Value("#{configproperties_disconf['redis.ip']}")
       public String ip;
       @Value("#{configproperties_disconf['redis.port']}")
       public int port;
       // getter & setter
     } 

    6.通过注入类的属性使用proerties配置

      通过上面配置已经将redis.properties的读取工作配置完成,在需要读取redis.properties的类中注入RedisConfig

      @Autowired
      RedisConfig redisConfig;
      读取方式:redisConfig.getIp();

    以上是使用spring集成disconf读取properties文件的配置过程,还需要将redisConfig配置文件上传到disconf的服务上,重启服务即可下载使用

     
  • 相关阅读:
    CentOS7系统基本操作
    python3安装
    nodejs基础【持续更新中】
    基于Jenkins实现持续集成【持续更新中】
    git之merge和rebase的区别
    服务器为什么这么慢?耗尽了CPU、RAM和磁盘I/O资源
    编程的四个境界
    Gunicorn独角兽
    Python 中 logging 日志模块在多进程环境下的使用
    vue+webpack怎么分环境进行打包
  • 原文地址:https://www.cnblogs.com/qinxu/p/10135769.html
Copyright © 2011-2022 走看看