Linux上 diconf 中心配置文件 使用

DisConf 是一款用于分布式系统配置管理的工具,能够帮助开发者轻松管理和更新多个服务器上的配置文件。本文详细介绍 DisConf 的环境搭建过程及两种集成方式:注解式和配置文件模式。
使用背景

        随着业务需求的复杂,分布式服务的流行,配置文件的管理也成为一个问题?

        今天介绍的disconf工具就能解决这个问题,如果你还为修改很多服务器上的配置文件而烦恼,那么你可以看看该工具的使用介绍.

disconf 环境搭建

            从github上下载源代码,都知道这个是开源的,所以都可以下载源码,然后自己编译.

            代码地址 : github上地址: https://github.com/knightliao/disconf

            文档地址: https://github.com/knightliao/disconf/wiki

            该项目运行需要依赖nginx,redis、zookeper、mysql,所以要安装之类的组件,在这里不一一介绍了,默认安装好,直接进行安装disconf项目

 管理端disconf-web

                首先需要java 和maven的编译环境.默认环境都安装好了.

mkdir /usr/local/disconf/source     #创建一个存放配置文件的目录

mkdir /usr/local/disconf/war     #创建一个存放编译后文件的目录

        cp ./disconf/disconf-web/profile/rd/* /usr/local/disconf/source    #将git上检下的项目配置文件拷贝到指定的目录上,同时把application-demo.properties修改成application.properties(mv命令)

修改配置文件

    将/usr/local/disconf/source下的这4个配置文件修改成自己环境相关的配置

       application.properties    该文件中主要是配置了监控邮件发送和接受的服务器和邮箱地址

zoo.properties    主要修改里面的hosts,指定zookeeper集群的host的端口

jdbc-mysql.properties    主要修改数据库的host和mysql的用户名和密码

redis-config.properties    主要修改2个实例的host和端口 (不需要集群,如果只有一个,那么填写一样的即可)

log4j.properties    主要修改日志存放的路径
    log4j.appender.dailyRolling.File=/home/xxx/xxx/tomcat/log/disconf-log4j.log

logback.xml    主要修改web和监控的log存放位置
    <property name="log.base" value="/home/xxx/tomcat/log/disconf-web"/>
    <property name="log.monitor" value="/home/xxx/tomcat/log/monitor"/>

编译生成war包

将存放路径配置成环境变量,等会脚本需要使用

ONLINE_CONFIG_PATH= /usr/local/disconf/source

WAR_ROOT_PATH= /usr/local/disconf/war

export ONLINE_CONFIG_PATH

export WAR_ROOT_PATH

cd disconf/disconf-web    进入web项目文件夹

sh deploy/deploy.sh    执行脚本(注意最好这样执行)

初始化数据库
        
根据disconf-web下的sql文件夹下的README.md来初始化

tomcat配置

            在tomcat的server.xml中加入    <Context path="" docBase="/usr/local/disconf/war"></Context>

nginx配置

        在http这个标记对里面加上如下配置(/usr/local/nginx/conf/nginx.conf):
include /etc/nginx/mime.types; #指定浏览器对css加载的类型
(特别注意https://blog.csdn.net/m0_37904728/article/details/78745243 )
default_type application/octet-stream;#指定浏览器对css加载的类型
upstream disconf {
server 127.0.0.1:8080;
}
server {
listen 8991;
server_name localhost;
access_log /home/xxx/nginx/log/disconf/access.log;
error_log /home/xxx//nginx/log/disconf/error.log;
location / {
root /usr/local/xxx/disconf/war/html;
if ($query_string) {
expires max;
}
}
location ~ ^/(api|export) {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://disconf;
}
}

以上环境就配置好了,启动后访问:http://IP:8991/    用户名:admin 密码:admin 出现以下的界面,这算是环境弄好了.

 

环境弄好了,那如何使用到项目中呢?

接下来就是将项目整合disconf,将配置文件交给disconf管理,使用maven作为例子.

导包:

 <dependency>

            <groupId>com.baidu.disconf</groupId>

            <artifactId>disconf-client</artifactId>

            <version>2.6.31</version>

       </dependency>
disconf支持两种开发模式,一种注解式,配置文件->配置文件可以无侵入模式
    先来注解模式:

    将disconf.properties文件放到resources目录下,内容如下:

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

#
# 配置服务器的 HOST,用逗号分隔  127.0.0.1:8000,127.0.0.1:8000
#
disconf.conf_server_host=172.16.7.217:8991

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

# APP 请采用 产品线_服务名 格式
disconf.app=disconf_demo

# 环境
disconf.env=rd

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

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

# 自定义的下载路径
disconf.user_define_download_dir=./config

    在applicationContext.xml里面添加如下内容:

<aop:aspectj-autoproxy proxy-target-class="true"/>  
  
    <!-- 使用disconf必须添加以下配置 -->  
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"  
          destroy-method="destroy">  
        <property name="scanPackage" value="cn.disconf.test"/>
    </bean>  
    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"  
          init-method="init" destroy-method="destroy">  
    </bean>

    重点解释:  <property name="scanPackage" value="cn.disconf.test"/> value填写的是discnf注解扫描的类路径

然后将之前的配置文件映射成java bean:

@Configuration
@DisconfFile(filename = "redis.properties")
public class JedisConfig{  
  
    protected static final Logger LOGGER = LoggerFactory  
            .getLogger(JedisConfig.class);  
  
    // 代表连接地址  
    private String host;
  
    // 代表连接port
    private int port;  
  
    /** 
     * 地址, 分布式文件配置 
     *  
     * @return 
     */  
    @DisconfFileItem(name = "redis.host", associateField = "host")  
    public String getHost() {  
  
        return host;  
    }  
  
    public void setHost(String host) {  
  
        this.host = host;  
    }  
  
    /** 
     * 端口, 分布式文件配置 
     *  
     * @return 
     */  
    @DisconfFileItem(name = "redis.port", associateField = "port")  
    public int getPort() {  
  
        return port;  
    }  
  
    public void setPort(int port) {  
  
        this.port = port;  
    }
}

    重点解释注解:

        @DisconfFile 映射配置文件名称

      @DisconfFileItem 映射配置文件内容 name :配置文件的key  ,associateField : java bean的属性. 该注解写在bean属性的get方法上.

以上就是注解式开发,接下来就是写disconf的配置.



        直接运行项目,在项目启动的时候,会到disconf上拉取配置文件到指定的本地目录下,同时会放一份到classes下一份.

    再来说下配置文件模式:

            配置文件用起来很简单,还是要用到disconf的核心disconf.properties文件.

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

#
# 配置服务器的 HOST,用逗号分隔  127.0.0.1:8000,127.0.0.1:8000
#
disconf.conf_server_host=172.16.7.217:8991

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

# APP 请采用 产品线_服务名 格式
disconf.app=disconf_demo

# 环境
disconf.env=rd

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

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

# 自定义的下载路径
disconf.user_define_download_dir=./config

           在applicationContext.xml文件中添加disconf需要的启动配置

        

   <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 使用disconf必须添加以下配置 -->
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
        <property name="scanPackage" value="com.example.disconf.demo"/>
    </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>

        这里使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload),还有种配置更改不会自动reload,有兴趣可以看看官方的文档,这里就不在描述了.

    这两种方式用起来是不是觉得都很简单啊!如果服务器很多,也不用去一台一台的去修改配置文件了.这个工具使用介绍就到这里了,如果有什么疑问或者问题可以联系,初次写博客,如果有什么不到位的地方,请大家多多包涵.同时大家有什么关于(java)新的技术要讨论的可以留言评论,我会去尝试研究然后一样写博客.如果没有时间限制,每个星期都要坚持也一篇关于技术使用的博客,希望大家多多给建议.奋斗奋斗奋斗奋斗奋斗奋斗留下下次想了解的技术啊!谢谢......













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值