120: openim deployment 自动化、模板化设计

案例

来一个案例快速了解一下模块化设计思路,用一个案例快速学习和了解模板化设计过程,下面是一个 zookeeper 的基础模板配置:

###################### Zookeeper ######################
# Zookeeper configuration
# It's not recommended to modify the schema
# 
# Zookeeper address
# Zookeeper username
# Zookeeper password
zookeeper:
  schema: ${ZOOKEEPER_SCHEMA}
  address: [ ${ZOOKEEPER_ADDRESS}:${ZOOKEEPER_PORT} ]
  username: ${ZOOKEEPER_USERNAME}
  password: ${ZOOKEEPER_PASSWORD}

下面是定义的 zookeeper 的基本配置:

有意思的是,这个配置不仅仅可以通过修改 env 脚本文件来改配置,也可以通过设置环境变量来修改配置,例如:

export ZOOKEEPER_PORT="12182"

下面是配置文件的一个基础模板:

###################### Zookeeper 配置信息 ######################
def "ZOOKEEPER_SCHEMA" "openim"          # Zookeeper的模式
def "ZOOKEEPER_PORT" "12181"              # Zookeeper的端口
def "ZOOKEEPER_ADDRESS" "${DOCKER_BRIDGE_GATEWAY}"      # Zookeeper的地址
def "ZOOKEEPER_USERNAME" ""              # Zookeeper的用户名
def "ZOOKEEPER_PASSWORD" ""              # Zookeeper的密码

上面的配置,可以通过 Makefile 或者脚本一键生成如下的配置,其中命令是 make init

###################### Zookeeper ######################
# Zookeeper configuration
# It's not recommended to modify the schema
# 
# Zookeeper address
# Zookeeper username
# Zookeeper password
zookeeper:
  schema: openim
  zkAddr: [ 172.28.0.1:12181 ]
  username: ""
  password: ""

模板化设计技巧

deployments 目录中,定义了大量的模板,包括 openim-chat 的模板,以及 openim-server 的模板,通过这些模板去渲染配置文件,这样可以让配置高度定制化以及自动化。