测试环境
- Mac Catalina
- Docker Desktop 3.6.0
- Hyperledger Fabric 2.5.4
- CouchDB 3.3.2
简单分析
当前版本的Fabric 2.5.4默认支持的LevelDB仅能够实现存储简单的键值对数据,并且LevelDB与Peer节点并存于同一个操作系统进程中。
CouchDB适用于存储JSON文件,并支持富查询和对更多数据类型的操作。但是,CouchDB进程与Peer节点进程是独立存在的,每个Peer节点进程都对应一个CouchDB进程。
因此,在本文中要使用CouchDB来替换LevelDB用作状态数据库,需要把它独立运行并进行手动配置。
幸运的是,Fabric的测试网络已经为我们作好了配置。接下来,我们把相关配置项分析一下。
重要声明:
当前下载安装的Hyperledger Fabric 2.5.4正好其生成测试配置网络时对应最新版本正式发行的CouchDB 3.3.2。我们注意到,我们独立下载安装的CouchDB 3.3.2正好与这里配置对应,不再需要修改。
1、Docker Compose中配置CouchDB服务
对应文件位于测试网络的路径位置是:test-network/compose,文件为:compose-couch.yaml。
内容如下:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '3.7'
networks:
test:
name: fabric_test
services:
couchdb0:
container_name: couchdb0
image: couchdb:3.3.2
labels:
service: hyperledger-fabric
# Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
# for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode.
environment:
- COUCHDB_USER=admin
- COUCHDB_PASSWORD=adminpw
# Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
# for example map it to utilize Fauxton User Interface in dev environments.
ports:
- "5984:5984"
networks:
- test
peer0.org1.example.com:
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984
# The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
# provide the credentials for ledger to connect to CouchDB. The username and password must
# match the username and password set for the associated CouchDB.
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=adminpw
depends_on:
- couchdb0
couchdb1:
container_name: couchdb1
image: couchdb:3.3.2
labels:
service: hyperledger-fabric
# Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
# for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode.
environment:
- COUCHDB_USER=admin
- COUCHDB_PASSWORD=adminpw
# Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
# for example map it to utilize Fauxton User Interface in dev environments.
ports:
- "7984:5984"
networks:
- test
peer0.org2.example.com:
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb1:5984
# The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
# provide the credentials for ledger to connect to CouchDB. The username and password must
# match the username and password set for the associated CouchDB.
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=adminpw
depends_on:
- couchdb1
注意上述默认配置中CouchDB用户名与密码及端口号(5984)。
2、在Peer服务中配置使用了CouchDB
另外,注意到上面配置默认定义了2个Peer服务。这两个服务分别为组织Org1和Org2定义了Peer节点运行的Docker容器。例如,peer0.org1.example.com的定义代码如下:
peer0.org1.example.com:
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984
# The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
# provide the credentials for ledger to connect to CouchDB. The username and password must
# match the username and password set for the associated CouchDB.
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=adminpw
depends_on:
- couchdb0
下面,我们将以CouchDB选项启动Fabric测试网络。当然,首先要启动Docker。
第一步:启动Docker Desktop
第二步:以CouchDB选项启动Fabric测试网络
- 使用cd命令进入到test-network目录下
- 运行下列命令:
./network.sh up -s couchdb
在文件network.sh中有一个函数networkUp(),用于启动测试网络。
# Bring up the peer and orderer nodes using docker compose.
function networkUp() {
checkPrereqs
# generate artifacts if they don't exist
if [ ! -d "organizations/peerOrganizations" ]; then
createOrgs
fi
COMPOSE_FILES="-f compose/${COMPOSE_FILE_BASE} -f compose/${CONTAINER_CLI}/${CONTAINER_CLI}-${COMPOSE_FILE_BASE}"
if [ "${DATABASE}" == "couchdb" ]; then
COMPOSE_FILES="${COMPOSE_FILES} -f compose/${COMPOSE_FILE_COUCH} -f compose/${CONTAINER_CLI}/${CONTAINER_CLI}-${COMPOSE_FILE_COUCH}"
fi
DOCKER_SOCK="${DOCKER_SOCK}" ${CONTAINER_CLI_COMPOSE} ${COMPOSE_FILES} up -d 2>&1
$CONTAINER_CLI ps -a
if [ $? -ne 0 ]; then
fatalln "Unable to start network"
fi
}
只需要注意其中的几个变量定义:COMPOSE_FILE_BASE和COMPOSE_FILE_COUCH。
接下来,我们启动Google浏览器,导航地址:http://127.0.0.1:5984/_utils/#login,显示如下:
输入用户名:admin和密码:adminpw,此二值都是配置文件中默认值。实际开发中,肯定要进行修改!
登录成功后,显示CouchDB数据库管理界面,如下:
引用
https://blog.51cto.com/zhuxianzhong/7488553