百度DisConf-分布式配置管理平台使用

百度DisConf-分布式配置管理平台使用

1.1 场景介绍

模拟数据库配置文件db.properties从disconf 获取:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shiro?characterEncoding=utf-8
jdbc.username=k2mwfNyZL/M=
jdbc.password=k2mwfNyZL/M=

1.1 导入所需的jar包

<dependency>
    <groupId>com.baidu.disconf</groupId>
    <artifactId>disconf-client</artifactId>
    <version>2.6.36</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.9</version>
</dependency>

1.2 在disconf-web端上传db.properties内容

1、新建APP
新建APP
这里写图片描述
2、新建配置文件
这里写图片描述
这里写图片描述

这里写图片描述

1.3 项目的配置

1、在src/main/resources下创建disconf.properties文件

    # 是否使用远程配置文件
    # true(默认)会从远程获取配置 false则直接获取本地配置
    enable.remote.conf=true

    #
    # 配置服务器的 HOST,用逗号分隔  127.0.0.1:8000,127.0.0.1:8000
    #
    conf_server_host=http://localhost

    # 版本, 请采用 X_X_X_X 格式
     version=1

    # APP 请采用 产品线_服务名 格式
    app=ssm-web

    # 环境
    env=local

    # debug
    debug=true

    # 忽略哪些分布式配置,用逗号分隔
    ignore=

    # 获取远程配置 重试次数,默认是3次
    conf_server_url_retry_times=1
    # 获取远程配置 重试时休眠时间,默认是5秒
    conf_server_url_retry_sleep_seconds=1

2、创建applicationContext-disconf.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
        <property name="scanPackage" value="com.yaspeed.*"/>
    </bean>

    <bean id="configproperties_no_reloadable_disconf"
          class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        <property name="locations">
            <list>
                <!-- disconf-web上上传的文件名 -->
                <value>db.properties</value>
            </list>
        </property>
    </bean>

    <bean id="propertyConfigurerForProject1"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="propertiesArray">
            <list>
                <ref bean="configproperties_no_reloadable_disconf"/>
            </list>
        </property>
    </bean>
</beans>

appincationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
    <!-- 加载配置文件 -->
    <!-- 自定义EncryptPropertyPlaceholderConfigurer进行加解密处理 -->
    <bean class="com.yaspeed.core.encryptPro.EncryptPropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:db.properties</value>
                </list>
            </property>
    </bean>  

        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="${jdbc.driver}" />
                <property name="jdbcUrl" value="${jdbc.url}" />
                <property name="user" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
        </bean>


    <!-- SqlSessionFactory -->
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
    <!-- Mapper映射文件的包扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yaspeed.web.mapper" />
    </bean>

</beans>

这里写图片描述

猜你喜欢

转载自blog.csdn.net/fd2025/article/details/80607676