Disconf入门指南(1)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
简介: Disconf简介 参考: https://github.com/knightliao/disconf/wiki/TutorialSummary在一个分布式环境中,同类型的服务往往会部署很多实例。这些实例使用了一些配置,为了更好地维护这些配置就产生了配置管理服务。通过这个服务可以轻松地管理成千上百个服务实例的配置问题。王阿晶提出了基于zooKeeper的配

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.git
cd disconf-master
mkdir build
mkdir -p build/online-resources
mkdir -p build/war

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

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

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

cp application-demo.properties application.properties 

构建

ONLINE_CONFIG_PATH=/home/dev1/Downloads/disconf/build/online-resources
WAR_ROOT_PATH=/home/dev1/Downloads/disconf/build/war
export ONLINE_CONFIG_PATH
export WAR_ROOT_PATH
cd disconf-web
sh deploy/deploy.sh

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

-disconf-web.war  
-html  
-META-INF  
-WEB-INF

部署

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

初始化数据库:

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

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

用户数据:可以参考 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>

并在对应的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;
    }
}

注意:访问的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>

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

disconf.properties

disconf.enable.remote.conf=true

disconf.conf_server_host=192.168.1.169:8080

disconf.version=V2

disconf.app=FreeLink

disconf.env=local

disconf.ignore=

disconf.conf_server_url_retry_sleep_seconds=1

disconf.user_define_download_dir=./disconf/download

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>

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;

// 代表连接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;
}

public void reload() throws Exception {

LOGGER.info("host: " + host);
}
}

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

Q&A:

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
9月前
|
并行计算 Ubuntu Docker
apollo快速入门之安装指南
apollo快速入门之安装指南
140 3
|
9月前
|
IDE Java Unix
Java语言开发环境配置详解
Java语言开发环境配置详解
168 1
|
9月前
|
负载均衡 监控 Java
新手入门gateway基本配置详解与深入分析
欢迎关注 `威哥爱编程` 一起交流学习,人生海海,相遇就是缘分,让我们以技术为信物,成为相互惦记的人。
420 1
|
9月前
|
关系型数据库 MySQL 中间件
KubeSphere 核心实战之四【在kubesphere平台上部署Ruoyi-cloud项目】(实操篇 4/4)
KubeSphere 核心实战之四【在kubesphere平台上部署Ruoyi-cloud项目】(实操篇 4/4)
508 1
|
存储 监控 前端开发
手把手教你搭建SpringCloud项目(十四 )集成Config分布式配置中心
手把手教你搭建SpringCloud项目(十四 )集成Config分布式配置中心
180 1
|
SQL XML 编解码
手把手教你搭建SpringCloud项目(一)图文详解
手把手教你搭建SpringCloud项目(一)图文详解
4702 1
|
消息中间件 自然语言处理 Java
ElasticSearch 学习笔记(四)-----ES在SpringBoot中的集成以及项目应用开发指南
接上一篇ElasticSearch 学习笔记(三)-----ES的设计原理以及分词器说明。今天我们主要介绍ES 与SpringBoot 的集成以及项目应用开发指南。
969 0
ElasticSearch 学习笔记(四)-----ES在SpringBoot中的集成以及项目应用开发指南
|
存储 监控 Java
Disconf源码解析(第一章)
用过Disconf的小伙伴都知道,这两个Bean配置是必不可少的,那么这两个Bean的作用是什么呢?其实很简单,Disconf有一个Web端,第一个DisconfMgrBean的作用就是从Web端下载配置文件,然后解析配置文件将配置信息存到Disconf配置仓库中,而DisconfMgrBeanSecond的作用就是给带有@DisconfFile注解的bean或者带有@DisconfItem注解的配置项注值。
Disconf源码解析(第一章)
|
监控 数据可视化 Java
SpringCloud学习(十五):Hystrix图形化Dashboard搭建与实战
SpringCloud学习(十五):Hystrix图形化Dashboard搭建与实战
221 0
SpringCloud学习(十五):Hystrix图形化Dashboard搭建与实战
|
存储 移动开发 小程序
uniapp环境搭建以及基础配置详解
安装编辑器 HbuilderX(HbuilderX 是通用的前端开发工具,但为 uni-app 做了特别强化)。 下载 APP 开发板,可开箱即用。 安装微信开发者工具。
831 0
uniapp环境搭建以及基础配置详解