Geth多台电脑搭建集群网络

上篇博客介绍了在一台电脑上配置多个geth节点,同时再最后也有个问题,多台电脑之间连接没有成功,后来多方研究之后,搭建成功了。防火墙没关!!!
Ubuntu系统关闭防火墙命令:sudo ufw disable

现在三台电脑:电脑一 219.216.65.127(Ubuntu)、电脑二 219.216.65.139(Windows)、电脑三 219.216.64.144(Ubuntu)

三台电脑上的genesis.json文件要相同,创建一样的创世区块;然后networkid要相同,属于同一个网络。genesis.json文件如下:

{
  "nonce": "0x0000000000000042",
  "timestamp": "0x00",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData": "0x00",
  "gasLimit": "0x80000000",
  "difficulty": "0x01",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x3333333333333333333333333333333333333333",
  "alloc": {     }
}

好了,现在来在三台电脑上各自创建一个节点

电脑一

//创建创世区块
$ geth --datadir ~/ether/n1 init genesis.json

# 启动console
$ geth --identity "hello" --datadir ~/ether/n1 --port 30301 --rpcport 8101 --networkid 12345 console 2>> ~/ether/n1/geth.log

启动成功,获取节点信息,方便后面连接。

> admin.nodeInfo
{
  enode: "enode://2c196b02b23598426f3915dbd7a1e8e178b067b3e9921af6849e42027ec41f73f6febc27b52da7e77c0edf69412b0231b886904c750167de335ead2798de12ff@[::]:30301",
  id: "2c196b02b23598426f3915dbd7a1e8e178b067b3e9921af6849e42027ec41f73f6febc27b52da7e77c0edf69412b0231b886904c750167de335ead2798de12ff",
  ip: "::",
  listenAddr: "[::]:30301",
  name: "Geth/v1.4.5-stable-a269a713/linux/go1.4.2/hello",
  ports: {
    discovery: 30301,
    listener: 30301
  },
  protocols: {
    eth: {
      difficulty: 5290384,
      genesis: "0x6099b65e564bd511f49e8f39ba27b6a68b6b78fd1481592257f06bbf93abe624",
      head: "0x337f2059a1a6d82aee1afba91828d86a8a3b6f85d83b8cb2f8a9210af3b29c1c",
      network: 12345
    }
  }
}

同样地,在电脑二、三上创建创世区块,启动console,获取它们的节点信息,然后在电脑一上连接另外两台电脑上的节点。

电脑二的enode:

"enode://54b7b1dccbe37148cda6e0c9889d23ffb8e2efc905f3c2d001cd66b3b513210a4a426706bab795d83d0c236b9b1647f46947d86c48ac8faf7f802aff318c4aa1@[::]:30301"

电脑三的enode:

"enode://07954219f1a52acfa7562ac841e77860505bb6f6cc948bc540b94326606679d33e805a21b21cb105eaf58f449b0acc84893560919afee00bbc01b8a3fc0213af@[::]:30301"

电脑一连接电脑二和电脑三:

>admin.addPeer("enode://54b7b1dccbe37148cda6e0c9889d23ffb8e2efc905f3c2d001cd66b3b513210a4a426706bab795d83d0c236b9b1647f46947d86c48ac8faf7f802aff318c4aa1@219.216.65.139:30301")

>admin.addPeer("enode://07954219f1a52acfa7562ac841e77860505bb6f6cc948bc540b94326606679d33e805a21b21cb105eaf58f449b0acc84893560919afee00bbc01b8a3fc0213af@219.216.64.144:30301")

现在可以看见连接的数量:

> net.peerCount
2
> admin.peers
[{
    caps: ["eth/61", "eth/62", "eth/63"],
    id: "07954219f1a52acfa7562ac841e77860505bb6f6cc948bc540b94326606679d33e805a21b21cb105eaf58f449b0acc84893560919afee00bbc01b8a3fc0213af",
    name: "Geth/v1.4.5-stable-a269a713/linux/go1.6.2/hello",
    network: {
      localAddress: "219.216.65.127:30301",
      remoteAddress: "219.216.64.144:36332"
    },
    protocols: {
      eth: {
        difficulty: 5290384,
        head: "337f2059a1a6d82aee1afba91828d86a8a3b6f85d83b8cb2f8a9210af3b29c1c",
        version: 63
      }
    }
}, {
    caps: ["eth/61", "eth/62", "eth/63"],
    id: "54b7b1dccbe37148cda6e0c9889d23ffb8e2efc905f3c2d001cd66b3b513210a4a426706bab795d83d0c236b9b1647f46947d86c48ac8faf7f802aff318c4aa1",
    name: "Geth/v1.4.5-stable/windows/go1.6.2/hello",
    network: {
      localAddress: "219.216.65.127:30301",
      remoteAddress: "219.216.65.139:50911"
    },
    protocols: {
      eth: {
        difficulty: 5290384,
        head: "337f2059a1a6d82aee1afba91828d86a8a3b6f85d83b8cb2f8a9210af3b29c1c",
        version: 63
      }
    }
}]

现在可以看见两个节点都连接成功。主要是防火墙的问题!

你可能感兴趣的:(区块链)