Disconf入门指南(1)

来源:互联网 发布:天才帽子淘宝网 编辑:程序博客网 时间:2024/05/29 04:53

Disconf简介

参考: 
https://github.com/knightliao/disconf/wiki/TutorialSummary

在一个分布式环境中,同类型的服务往往会部署很多实例。这些实例使用了一些配置,为了更好地维护这些配置就产生了配置管理服务。通过这个服务可以轻松地管理成千上百个服务实例的配置问题。

王阿晶提出了基于zooKeeper的配置信息存储方案的设计与实现[1], 它将所有配置存储在zookeeper上,这会导致配置的管理不那么方便,而且他们没有相关的源码实现。淘宝的diamond[2]是淘宝内部使用的一个管理持久配置的系统,它具有完整的开源源码实现,它的特点是简单、可靠、易用,淘宝内部绝大多数系统的配置都采用diamond来进行统一管理。他将所有配置文件里的配置打散化进行存储,只支持KV结构,并且配置更新的推送是非实时的。百度内部的BJF配置中心服务[3]采用了类似淘宝diamond的实现,也是配置打散化、只支持KV和非实时推送。

同构系统是市场的主流,特别地,在业界大量使用部署虚拟化(如JPAAS系统,SAE,BAE)的情况下,同一个系统使用同一个部署包的情景会越来越多。但是,异构系统也有一定的存在意义,譬如,对于“拉模式”的多个下游实例,同一时间点只能只有一个下游实例在运行。在这种情景下,就存在多台实例机器有“主备机”模式的问题。目前国内并没有很明显的解决方案来统一解决此问题。

Disconf安装

参考 
- Git:https://github.com/knightliao/disconf/tree/master/disconf-web 
- OS China: http://www.oschina.net/p/disconf/similar_projects?fromerr=vfYXkqGn 
- 个人网站 http://www.liaoqiqi.com/post/105

安装依赖软件

  • 安装 Mysql
  • 安装 Tomcat(apache-tomcat-7.0.50)
  • 安装 Nginx(nginx/1.5.3)
  • 安装 zookeeeper (zookeeper-3.3.0)
  • 安装 Redis (2.4.5)

准备配置文件

git clone https://github.com/knightliao/disconf.gitcd disconf-mastermkdir buildmkdir -p build/online-resourcesmkdir -p build/war
  • 1
  • 2
  • 3
  • 4
  • 5

下载好对应的代码,准备对应的配置文件:将disconf-web/profile/rd目录下的文件copy到build/online-resources目录下

- jdbc-mysql.properties (数据库配置)- redis-config.properties (Redis配置)- zoo.properties (Zookeeper配置)- application.properties (应用配置)
  • 1
  • 2
  • 3
  • 4

注意,记得执行将application-demo.properties复制成application.properties:

cp application-demo.properties application.properties 
  • 1

构建

ONLINE_CONFIG_PATH=/home/dev1/Downloads/disconf/build/online-resourcesWAR_ROOT_PATH=/home/dev1/Downloads/disconf/build/warexport ONLINE_CONFIG_PATHexport WAR_ROOT_PATHcd disconf-websh deploy/deploy.sh
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

编译完成后,会在 /home/dev1/Downloads/disconf/build/war 生成以下结果:

-disconf-web.war  -html  -META-INF  -WEB-INF
  • 1
  • 2
  • 3
  • 4

部署

Disconf代码设计采用了静动分离设计,整个的代码的前后端完全分离。

初始化数据库:

对应的数据库脚本都在disconf-web/sql目录下,依次执行对应的sql语句就可以了

mysql -u username -p password < 0-init_table.sqlmysql -u username -p password -Ddisconf  < 1-init_data.sqlmysql -u username -p password -Ddisconf < 201512/20151225.sql
  • 1
  • 2
  • 3

用户数据:可以参考 sql/readme.md 来进行数据库的初始化。里面默认有6个用户

如果想自己设置初始化的用户名信息,可以参考代码来自己生成用户: 
src/main/java/com/baidu/disconf/web/tools/UserCreateTools.java

动静部署

Disconf代码采用了动静分离设计,

部署War(tomcat)

vim /etc/tomcat7/server.xml 
修改server.xml文件,在Host结点下添加Context:

<Context path="" docBase="/usr/local/disconf/build/war"></Context>
  • 1

并在对应的war目录下创建tmp目录,启动tomcat服务器。

部署前端(nginx)

修改 nginx.conf,在HTTP标签中添加以下内容:

upstream disconf {    server 127.0.0.1:8080;}server {    listen   8081;    server_name localhost;    access_log /home/work/var/logs/disconf/access.log;    error_log /home/work/var/logs/disconf/error.log;    location / {        root /home/work/dsp/disconf-rd/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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

注意:访问的html路径应该具有访问权限,可以在在nginx.conf文件第一行中更改用户,或者更改对应的HTML文件的权限。静态配置成功后,可以通过查看tomcat的日志和nginx日志文件来查看访问的记录,静态文件由nginx直接处理,而动态文件则是tomcat来处理的。

业务功能说明

支持用户登录/登出浏览配置    按 APP/版本/环境 选择修改配置    修改配置    修改配置项    修改配置文件新建配置    新建配置项    新建配置文件    新建APP

Client使用

在spring boot中使用 
1. 创建maven工程,添加对应的Maven库

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.egoo</groupId>    <artifactId>demo</artifactId>    <version>1.0-SNAPSHOT</version>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.7</java.version>    </properties>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.2.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>com.baidu.disconf</groupId>            <artifactId>disconf-client</artifactId>            <version>2.6.31</version>        </dependency>        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>            <version>2.1.0</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>            <exclusions>                <exclusion>                    <groupId>org.springframework.boot</groupId>                    <artifactId>spring-boot-starter-logging</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-log4j</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

2.添加对应的属性文件/resources

disconf.properties

disconf.enable.remote.conf=truedisconf.conf_server_host=192.168.1.169:8080disconf.version=V2disconf.app=FreeLinkdisconf.env=localdisconf.ignore=disconf.conf_server_url_retry_sleep_seconds=1disconf.user_define_download_dir=./disconf/download
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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-3.1.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <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.egoo.config"/>    </bean>    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"init-method="init" destroy-method="destroy">    </bean></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Disconf启动需要加载的类,以及需要扫描的包文件目录 
3.添加对应的代码 redis示例 
RedisConfig.java

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.Configuration;import com.baidu.disconf.client.common.annotations.DisconfFile;import com.baidu.disconf.client.common.annotations.DisconfFileItem;import com.baidu.disconf.client.common.update.IDisconfUpdate;/** * Created by fiboliu on 16-3-16. */@Configuration@DisconfFile(filename = "redis.properties")public class RedisConfig implements IDisconfUpdate {protected static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class);// 代表连接地址private String host;// 代表连接portprivate 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;}public void reload() throws Exception {LOGGER.info("host: " + host);}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

示例代码上传至csdn: 
http://pan.baidu.com/s/1nuoFNvj 
http://download.csdn.net/detail/fiboliu/9464185

Q&A:

原创粉丝点击