1.安装配置golang开发环境
Go是Google开发的一种编译型,可平行化,并具有垃圾回收功能的编程语言,可到http://www.golangtc.com/download去下载go1.7rc6.linux-amd64.tar.gz,下载后将压缩包上传并解压到指定的目录下(例:/usr/local/)。
tar -zxvf /usr/local/src/go1.7rc6.linux-amd64.tar.gz -C /usr/local/
配置环境变量,编辑/etc/profile文件,添加如下内容:
GOHOME=/usr/local
export GOROOT=$GOHOME/go
export GOPATH=/usr/webserver/go/
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
保存退出,执行source /etc/profile重新加载环境变量后,执行go version,若能正常显示go的版本信息,则配置成功。
2.下载并安装weed-fs
a. 存储海量文件
b. 能快速访问存储的文件
参考weed-fs的官网https://github.com/chrislusf/seaweedfs的installation guide安装weed-fs,见下图:
执行go get github.com/chrislusf/seaweedfs/go/weed命令后,将下载并编译weed-fs,生成的可执行文件weed放在$GOPATH/bin目录,源码存放在$GOPATH/src目录,由于在国内一些网站被墙,可能会出现一些包下载不了错误,直接网上下载放到相相应的目录就好;
以下错误:
root@ubuntu:/tmp/chengxf# go get github.com/chrislusf/seaweedfs/go/weed
package github.com/chrislusf/seaweedfs/go/weed
imports bazil.org/fuse
imports bazil.org/fuse/fs
imports golang.org/x/net/context: unrecognized import path "golang.org/x/net/context"
# cd .;Git clone https://code.google.com/p/go-decimal-inf.exp /tmp/chengxf/gopath/src/speter.NET/go/exp/math/dec/inf
Cloning into '/tmp/chengxf/gopath/src/speter.Net/go/exp/math/dec/inf'...
error: Failed to connect to 2404:6800:400a:805::200e: Network is unreachable while accessing https://code.google.com/p/go-decimal-inf.exp/info/refs
fatal: HTTP request failed
package github.com/chrislusf/seaweedfs/go/weed
imports bazil.org/fuse
imports bazil.org/fuse/fs
imports github.com/boltdb/bolt
imports github.com/chrislusf/raft
imports github.com/gogo/protobuf/proto
imports github.com/dgrijalva/jwt-go
imports github.com/gocql/gocql
imports github.com/golang/groupcache/lru
imports github.com/golang/snappy/snappy
imports speter.net/go/exp/math/dec/inf: exit status 128
package github.com/chrislusf/seaweedfs/go/weed
imports bazil.org/fuse
imports bazil.org/fuse/fs
imports github.com/boltdb/bolt
imports github.com/chrislusf/raft
imports github.com/gogo/protobuf/proto
imports github.com/dgrijalva/jwt-go
imports github.com/gocql/gocql
imports github.com/golang/groupcache/lru
imports github.com/golang/snappy/snappy
imports github.com/golang/protobuf/proto
imports github.com/syndtr/goleveldb/leveldb
imports github.com/syndtr/goleveldb/leveldb/util
imports github.com/syndtr/gosnappy/snappy
imports gopkg.in/Redis.v2
imports gopkg.in/bufio.v1
imports github.com/disintegration/imaging
imports golang.org/x/image/bmp: unrecognized import path "golang.org/x/image/bmp"
package github.com/chrislusf/seaweedfs/go/weed
imports bazil.org/fuse
imports bazil.org/fuse/fs
imports github.com/boltdb/bolt
imports github.com/chrislusf/raft
imports github.com/gogo/protobuf/proto
imports github.com/dgrijalva/jwt-go
imports github.com/gocql/gocql
imports github.com/golang/groupcache/lru
imports github.com/golang/snappy/snappy
imports github.com/golang/protobuf/proto
imports github.com/syndtr/goleveldb/leveldb
imports github.com/syndtr/goleveldb/leveldb/util
imports github.com/syndtr/gosnappy/snappy
imports gopkg.in/redis.v2
imports gopkg.in/bufio.v1
imports github.com/disintegration/imaging
imports golang.org/x/image/tiff: unrecognized import path "golang.org/x/image/tiff"
root@ubuntu:/tmp/chengxf#
首先上面的unrecognized import path “imports golang.org/x/xxx/xxx”,可以手动去https://github.com/golang上下载相应的包,放置与/tmp/chengxf/gopath/src的相应子目录下,再次执行后,发现只剩如下错误:
root@ubuntu:/tmp/chengxf# go get github.com/chrislusf/seaweedfs/go/weed
# cd .; git clone https://code.google.com/p/go-decimal-inf.exp /tmp/chengxf/gopath/src/speter.net/go/exp/math/dec/inf
Cloning into '/tmp/chengxf/gopath/src/speter.net/go/exp/math/dec/inf'...
error: Failed to connect to 2404:6800:4005:80b::200e: Network is unreachable while accessing https://code.google.com/p/go-decimal-inf.exp/info/refs
fatal: HTTP request failed
package speter.net/go/exp/math/dec/inf: exit status 128
root@ubuntu:/tmp/chengxf#
上面是获取code.google.com上的资源出现错误,可以手动去https://github.com/go-inf/inf/tree/master下载源码包,并解压至/tmp/chengxf/gopath/src/speter.net/go/exp/math/dec/inf目录下,再次执行go get github.com/chrislusf/seaweedfs/go/weed又报错:
root@ubuntu:/tmp/chengxf/gopath/src/speter.net/go/exp/math/dec/inf# go get github.com/chrislusf/seaweedfs/go/weed
../../../../../../github.com/gocql/gocql/helpers.go:13:2: code in directory /tmp/chengxf/gopath/src/speter.net/go/exp/math/dec/inf expects import "gopkg.in/inf.v0"
好吧,我也不知道什么原因,总之我将/tmp/chengxf/gopath/src/speter.net/go/exp/math/dec/inf/dec.go文件中的”// import "gopkg.in/inf.v0"“删除掉,然后再用go get github.com/chrislusf/seaweedfs/go/weed重新编译就好了。/tmp/chengxf/gopath/bin目录下生成了可执行文件weed。
2. 方式二:下载编译好的进行运行安装
下载地址:https://github.com/chrislusf/seaweedfs/releases/tag/0.74
可以直接下载它的运行包,直接解压,就可以运行了(我就是这样做的,如果这样,etc/profile里面的gopath这一项就不需要了)
export GOPATH=/usr/webserver/go/
export PATH=$PATH:$GOPATH/bin
linux 下命令下载:
wgethttps://bintray.com/artifact/download/chrislusf/seaweedfs/weed_0.70beta_linux_amd64.tar.gz
tar zxvf weed_0.70beta_linux_amd64.tar.gz
服务启动:
1 进入weed的解压目录,我的是这样的。
root@ubuntu:~/webserver/weed_0.70beta_linux_amd64# ./weed master
注意看,它是启动了一个服务,端口是9333,这是它的默认端口。
2 启动文件存储服务
root@ubuntu:~/webserver/weed_0.70beta_linux_amd64# ./weed volume -dir="/root/webserver/weed_0.70beta_linux_amd64/data" -max=5 -mserver="localhost:9333" -port=9080 &
3 提交一个存储请求,这个时候weed先要分配一个全局的文件ID
curl -X POST http://localhost:9333/dir/assign
4 存储一张图片
curl -X PUT -F file=@/root/webserver/weed_0.70beta_linux_amd64/error.jpg http://127.0.0.1:9080/5,019d90e98a
5 访问测试
因为我的服务器地址是192.168.1.47,http://192.168.1.47:9080/5,019d90e98a.jpg
甚至,你还可以加上尺寸,动态获取相应图片的大小
确实不错。看了以上的截图,你会发现它的文件ID,至始至终都存在。这就是文件的唯一标识。其实这个ID是每3部分存在的。
其中"fid":"5,019d90e98a"就是 Fid,Fid 由三个部分组成 【VolumeId, NeedleId, Cookie】 组成。
VolumeId: 1 32bit 存储的物理卷的Id
NeedleId: 01 64bit 全局唯一NeedleId,每个存储的文件都不一样(除了互为备份的)。
Cookie: 9d90e98a 32bit Cookie值,为了安全起见,防止恶意攻击。
以上这些,只能说是用过curl的方式,作为JAVA开发人员。还是需要封装一个这些API,方便访问。
以上是常规启动:在启动时我们还可以进行参数配置
例:
准备:在/tmp/chengxf目录下新建如下文件夹:mdata、vol1、vol2、input和output,并在input文件夹中新建测试文件a.txt和b.xml。
启动master:weed master -mdir="/tmp/chengxf/mdata" -defaultReplication="001" -ip="10.124.69.235" -port=9334 &
启动volume:weed volume -dir=/tmp/chengxf/vol1/ -mserver="10.124.69.235:9334" -ip="117.78.1.100" -ip.bind="10.124.69.235" -port=8081 -max=5 &
weed volume -dir=/tmp/chengxf/vol2/ -mserver="10.124.69.235:9334" -ip="117.78.1.100" -ip.bind="10.124.69.235" -port=8082 -max=5 &
存储文件:weed upload -server="117.78.1.100:9334" -dir="/tmp/chengxf/input" -include=*.txt
执行后返回文件fid:[{"fileName":"/tmp/chengxf/input/a.txt","fileUrl":"117.78.1.100:8081/1,01eb8f1e42","fid":"1,01eb8f1e42","size":44}]
根据fid访问文件:weed download -server="117.78.1.100:9334" -dir="/tmp/chengxf/output" 1,01eb8f1e42
执行后在/tmp/chengxf/output目录下发现a.txt文件
117.78.1.100是公网IP,通过防火墙映射到内网机器10.124.69.235上的,不能直接绑定公网地址,所以采用ip.bind绑定内网地址来实现。
参考文章:http://blog.csdn.net/qq286210662/article/details/46328153?ref=myread
参考文章:http://sanwen.net/a/tabgsbo.html
有疑问加站长微信联系(非本文作者)