【IPFS】IPFS部署分布式项目到开发机的基础实践

来源:互联网 发布:公交大数据应用 编辑:程序博客网 时间:2024/05/16 18:20

什么是IPFS

IPFS 是一个点对点的分布式文件系统,它希望将所有的计算设备都连接到同一个文件系统中。

IPFS能做到去中心化的分布式部署

IPFS旨在替代HTTP和为我们所有人建立更好的网络。

安装

以下地址进行下载
https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_darwin-amd64.tar.gz

wget https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_darwin-amd64.tar.gz
tar xvfz go-ipfs_v0.4.10_darwin-amd64.tar.gz
mv go-ipfs/ipfs /usr/local/bin/ipfs

GO环境
https://storage.googleapis.com/golang/go1.8.3.darwin-amd64.pkg

go version
go version go1.8.3 darwin/amd64

实践

初始化

ipfs initinitializing IPFS node at /Users/xiaoyu/.ipfsgenerating 2048-bit RSA keypair...donepeer identity: QmSTt7gDRf5RhWDQRAp516d9hDf5ZTeyi5mcjqnL61n9okto get started, enter:ipfs cat /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/readmeipfs cat /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/readmeHello and Welcome to IPFS!██╗██████╗ ███████╗███████╗██║██╔══██╗██╔════╝██╔════╝██║██████╔╝█████╗  ███████╗██║██╔═══╝ ██╔══╝  ╚════██║██║██║     ██║     ███████║╚═╝╚═╝     ╚═╝     ╚══════╝If you're seeing this, you have successfully installedIPFS and are now interfacing with the ipfs merkledag! -------------------------------------------------------| Warning:                                              ||   This is alpha software. Use at your own discretion! ||   Much is missing or lacking polish. There are bugs.  ||   Not yet secure. Read the security notes for more.   | -------------------------------------------------------Check out some of the other files in this directory:  ./about  ./help  ./quick-start     <-- usage examples  ./readme          <-- this file  ./security-notes

也就是这个文档,我们可以访问下面的文件进行访问

  • ./about
  • ./help
  • ./quick-start
  • ./readme
  • ./security-notes

quick-start

ipfs cat /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/quick-start 
# 0.1 - Quick Start //快速开始This is a set of short examples with minimal explanation. It is meant asa "quick start". Soon, we'll write a longer tour :-)Add a file to ipfs://添加文件  echo "hello world" >hello  ipfs add helloView it: //查看文件  ipfs cat <the-hash-you-got-here>Try a directory: //添加文件夹  mkdir foo  mkdir foo/bar  echo "baz" > foo/baz  echo "baz" > foo/bar/baz  ipfs add -r fooView things: //列出查看文件和文件夹  ipfs ls <the-hash-here>  ipfs ls <the-hash-here>/bar  ipfs cat <the-hash-here>/baz  ipfs cat <the-hash-here>/bar/baz  ipfs cat <the-hash-here>/bar  ipfs ls <the-hash-here>/bazReferences: //重定向  ipfs refs <the-hash-here>  ipfs refs -r <the-hash-here>  ipfs refs --helpGet://对比文件  ipfs get <the-hash-here> -o foo2  diff foo foo2Objects://列出对象  ipfs object get <the-hash-here>  ipfs object get <the-hash-here>/foo2  ipfs object --helpPin + GC:  ipfs pin add <the-hash-here>  ipfs repo gc  ipfs ls <the-hash-here>  ipfs pin rm <the-hash-here>  ipfs repo gcDaemon:  ipfs daemon  (in another terminal)  ipfs idNetwork:  (must be online)  ipfs swarm peers  ipfs id  ipfs cat <hash-of-remote-object>Mount:  (warning: fuse is finicky!)  ipfs mount  cd /ipfs/<the-hash-here>  lsTool: //工具  ipfs version  ipfs update  ipfs commands  ipfs config --help  open http://localhost:5001/webuiBrowse:  webui:    http://localhost:5001/webui  video:    http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXse  images:    http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs  markdown renderer app:    http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown

尝试运行

ipfs daemonInitializing daemon...Adjusting current ulimit to 2048...Successfully raised file descriptor limit to 2048.Swarm listening on /ip4/127.0.0.1/tcp/4001Swarm listening on /ip4/192.168.0.106/tcp/4001Swarm listening on /ip4/192.168.1.5/tcp/50092Swarm listening on /ip6/::1/tcp/4001API server listening on /ip4/127.0.0.1/tcp/5001Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080Daemon is ready

注意此时终端的命令没有结束,也就是说目前的启动是不能关闭终端的

此时访问之前的图片网址http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs,展示效果如下,文件访问成功。

这里写图片描述

同理,访问视频文件和markdown文件也能正常访问。

在demo中给的webui实际上是一个管理后台,可以查看这个项目被放到了全球多少个节点上。

出现的报错

  • ERROR core/serve: ipfs resolve -r /ipfs/QmPhnvn747LqwPYMJmQVorMaGbMSgA7mRRoyyZYz3DoZRQ/locale/webui-zh.json: no link named "webui-zh.json" under Qmc6bAsXTFFdvV3RoLuf6jjV3jzsRLntfsivWQZ75s8QaL gateway_handler.go:584
  • ERROR flatfs: too many open files, retrying in 100ms flatfs.go:180
  • ERROR mdns: mdns lookup error: failed to bind to any unicast udp port mdns.go:135

总结

  • 了解了IPFS的安装
  • 本机成功启动了demo
  • 遇到了一些小问题

参考资料

  • https://ipfs.io/