如果运行过其他的示例,可以执行
docker rm -f $(docker ps -aq)
docker rmi -f $(docker images | grep fabcar | awk '{print $3}')
#mac os 使用以下命令下载
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/darwin-amd64-1.4.0/hyperledger-fabric-darwin-amd64-1.4.0.tar.gz
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/darwin-amd64-1.4.0/hyperledger-fabric-ca-darwin-amd64-1.4.0.tar.gz
tar -xvf
#拉取镜像
docker pull hyperledger/fabric-javaenv:x86_64-1.4.0
docker pull hyperledger/fabric-ccenv:x86_64-1.4.0
docker pull hyperledger/fabric-baseos:x86_64-0.4.6
对应blockchain-explorer版本为release-3.9
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone http://gerrit.hyperledger.org/r/fabric
切换到1.4.3
go get github.com/golang/protobuf/protoc-gen-go
mkdir -p $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin
cp $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin
cd $GOPATH/src/github.com/hyperledger/fabric
make release
#下载包含Fabric模块的镜像
make docker
对于 MacOS 系统,在编译之前需要打开文件 $GOPATH/src/github.com/hyperledger/fabric/Makefile,在其中的第一个GO_LDFLAGS字符串所在行的尾加上字符串 -s。
或直接执行curl -sSL https://goo.gl/6wtTN5 | bash (需要),会自动执行相关操作
cp /Users/qifei/go/src/github.com/hyperledger/fabric/release/darwin-amd64/bin/* /usr/local/bin
sudo chmod -R 775 /usr/local/bin/configtxgen
sudo chmod -R 775 /usr/local/bin/configtxlator
sudo chmod -R 775 /usr/local/bin/cryptogen
sudo chmod -R 775 /usr/local/bin/peer
sudo chmod -R 775 /usr/local/bin/orderer
git clone https://github.com/hyperledger/fabric-samples.git
cryptogen提供了一个命令用于获取cryptogen所需配置,我们直接使用这个命令生成一个文件修改即可:
mkdir ~/syswin/fabric-deploy/crypto-config
cryptogen showtemplate >> crypto-config.yaml
vi crypto-config.yaml
将配置修改为:
OrdererOrgs:
- Name: Orderer
Domain: zhengtoon.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.zhengtoon.com
EnableNodeOUs: false
Template:
Count: 2
Users:
Count: 1
- Name: Org2
Domain: org2.zhengtoon.com
EnableNodeOUs: false
Template:
Count: 2
Users:
Count: 1
保存后执行
cryptogen generate --config=./crypto-config.yaml
之后可以看到在当前文件夹生成了一个crypto-config文件夹,存放证书等信息
将生成的内容中域名映射本机,HOST增加
vi /etc/hosts
127.0.0.1 orderer.zhengtoon.com
127.0.0.1 peer0.org1.zhengtoon.com
127.0.0.1 peer1.org1.zhengtoon.com
127.0.0.1 peer0.org2.zhengtoon.com
127.0.0.1 peer1.org2.zhengtoon.com
cp -r $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml ~/syswin/fabric-deploy/
对configtx.yaml进行修改,如下
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: ./crypto-config/ordererOrganizations/zhengtoon.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: ./crypto-config/peerOrganizations/org1.zhengtoon.com/msp
AnchorPeers:
- Host: peer0.org1.zhengtoon.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: ./crypto-config/peerOrganizations/org2.zhengtoon.com/msp
AnchorPeers:
- Host: peer0.org2.zhengtoon.com
Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.zhengtoon.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
Profiles:
TwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
mkdir channel-artifacts
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
可以直接从first-network中拷贝docker-compose-cli.yaml 及base文件夹进行修改
包含orderer,peer,cli等的配置,注意容器path与物理机的映射
orderer.zhengtoon.com
- ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ../crypto-config/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp:/var/hyperledger/orderer/msp
- ../crypto-config/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/tls/:/var/hyperledger/orderer/tls
- orderer.zhengtoon.com:/var/hyperledger/production/orderer
peer0.org1.zhengtoon.com
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls:/etc/hyperledger/fabric/tls
- peer1.org1.zhengtoon.com:/var/hyperledger/production
cli
- /var/run/:/host/var/run/
- ./../chaincode/:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
创建好之后执行命令启动
export IMAGE_TAG=1.4.0
export COMPOSE_PROJECT_NAME=fabric-deploy
docker-compose -f docker-compose-cli.yaml up -d
进入cli
环境变量
: ${CHANNEL_NAME:="mychannel"}
: ${DELAY:="3"}
: ${LANGUAGE:="golang"}
: ${TIMEOUT:="10"}
: ${VERBOSE:="false"}
export set CHANNEL_NAME=mychannel
peer channel create -o orderer.zhengtoon.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
//TLS
peer channel create -o orderer.zhengtoon.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem >&log.txt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org1.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/ca.crt
peer channel join -b mychannel.block
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer1.org1.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls/ca.crt
peer channel join -b mychannel.block
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org2.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/ca.crt
peer channel join -b mychannel.block
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer1.org2.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer1.org2.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer1.org2.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer1.org2.zhengtoon.com/tls/ca.crt
peer channel join -b mychannel.block
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org1.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/ca.crt
peer channel update -o orderer.zhengtoon.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org2.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/ca.crt
peer channel update -o orderer.zhengtoon.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem
peer channel update -o orderer.zhengtoon.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx
#TLS
peer channel update -o orderer.zhengtoon.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org1.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/ca.crt
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer1.org1.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer1.org1.zhengtoon.com/tls/ca.crt
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org2.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/ca.crt
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer1.org2.zhengtoon.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer1.org2.zhengtoon.com/tls/server.key
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer1.org2.zhengtoon.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer1.org2.zhengtoon.com/tls/ca.crt
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
peer chaincode instantiate -o orderer.zhengtoon.com:7050 -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
peer chaincode instantiate -o orderer.zhengtoon.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.member'\'','\''Org2MSP.member'\'')'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
peer chaincode invoke -o orderer.zhengtoon.com:7050 -C mychannel -n mycc --peerAddresses peer0.org1.zhengtoon.com:7051 -c '{"Args":["invoke","a","b","10"]}'
peer chaincode invoke -o orderer.zhengtoon.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.zhengtoon.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.zhengtoon.com/peers/peer0.org1.zhengtoon.com/tls/ca.crt --peerAddresses peer0.org2.zhengtoon.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.zhengtoon.com/peers/peer0.org2.zhengtoon.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
#合约打包与签署
peer chaincode package mycc.out -n mycc -v 0.0.2 -s -S -p github.com/chaincode/chaincode_example02/go
peer chaincode signpackage mycc.out signed-mycc.out
#安装合约,新的合约也需要在每个peer上单独安装。所以需要切换不同的环境
peer chaincode install ./signed-mycc.out
#更新的合约不需要初始化,需要进行更新操作:
peer chaincode upgrade -o orderer.zhengtoon.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zhengtoon.com/orderers/orderer.zhengtoon.com/msp/tlscacerts/tlsca.zhengtoon.com-cert.pem -C mychannel -n mycc -v 0.0.2 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.member'\'','\''Org2MSP.member'\'')'
docker logs -f cli
docker logs dev-peer0.org2.zhengtoon.com-mycc-1.0