Nephele 学习
1 环境准备
1.1 安装 golang
本文中选择使用 go1.11
。
注意:
要求
Go
版本>=1.10.2
。
获取 golang
的安装包到本地:
[root@localhost ~]# wget https://dl.google.com/go/go1.11.linux-amd64.tar.gz
解压 tar
包到 /usr/local/
目录中:
[root@localhost ~]# tar -C /usr/local -zxvf go1.11.linux-amd64.tar.gz
把 /usr/local/go/bin
添加到环境变量 PATH
。为此你可以将下面这行加入到 /etc/profile
或者$HOME/.profile
中去:
[root@localhost ~]# export PATH=$PATH:/usr/local/go/bin
[root@localhost ~]# export GOPATH=~/golang_workspace
验证是否安装成功:
[root@localhost ~]# go version
go version go1.11 linux/amd64
1.2 安装 Govendor
通过 go get
命令进行安装:
[root@localhost ~]# go get -u github.com/kardianos/govendor
切换到 govendor
目录中,编译 govendor
,并将编译完成的 govendor
拷贝到 /usr/bin
目录:
[root@localhost ~]# cd ~/golang_workspace/src/github.com/kardianos/govendor/
[root@localhost govendor]# go build -o govendor
[root@localhost govendor]# cp ./govendor /usr/bin/
验证是否安装成功:
[root@localhost ~]# govendor -version
v1.0.9
1.3 安装 GraphicsMaigck
当前版本 Nephele
默认调用 GraphicsMagick
处理图片。
执行 ctripcorp/nephele/thirdparty/graphicsmagick/setup_unix.sh
该脚本的主要操作如下:
# 对于 centos7 系统
sudo wget http://mirrors.aliyun.com/repo/epel-7.repo
sudo mv epel-7.repo /etc/yum.repos.d/epel-aliyun.repo
sudo chmod +x /etc/yum.repos.d/epel-aliyun.repo
sudo yum clean all
sudo yum makecache
sudo yum install -y GraphicsMagick-devel --enablerepo=epel
sudo rm /etc/yum.repos.d/epel-aliyun.repo
1.4 下载 ctripcorp/nephele
使用 go get
命令下载:
[root@localhost ~]# go get github.com/ctripcorp/nephele
1.5 下载 nephele 的依赖
通过 govendor sync
命令下载依赖包:
[root@localhost ~]# cd ~/golang_workspace/src/github.com/ctripcorp/nephele
[root@localhost nephele]# govendor sync
- 发现错误:
[root@localhost nephele]# govendor sync
Username for 'https://github.com': ** your Username **
Password for 'https://destroyer399@github.com': ** your Password **
# cd .; git clone https://github.com/ctrip-nephele/gmagick /root/golang_workspace/.cache/govendor/github.com/ctrip-nephele/gmagick
Cloning into '/root/golang_workspace/.cache/govendor/github.com/ctrip-nephele/gmagick'...
remote: Repository not found.
fatal: repository 'https://github.com/ctrip-nephele/gmagick/' not found
通过错误提示,可知找不到页面 https://github.com/ctrip-nephele/gmagick/
,因为 nephele
使用的 github.com/gographics/gmagick
,所以我们将 nephele
的 verdor.json
修改一下,删掉下面两块的配置:
{
"checksumSHA1": "88ntCdB+fP/2EYKx+foTZ3Ja+rc=",
"path": "github.com/ctrip-nephele/gmagick",
"revision": "67ca701d3abae744bb5038485e42fa5e708002f0",
"revisionTime": "2018-05-28T14:47:05Z"
},
{
"checksumSHA1": "53b5mH+hdqwDzlzTMGAOx4MLROc=",
"path": "github.com/ctrip-nephele/gmagick/types",
"revision": "67ca701d3abae744bb5038485e42fa5e708002f0",
"revisionTime": "2018-05-28T14:47:05Z"
},
然后,手动下载 gographics/gmagick
并放入 vendor/github.com/
目录中,并改名为 ctrip-nephele/gmagick
。
[root@localhost nephele]# git clone https://github.com/gographics/gmagick.git ./vendor/github.com/ctrip-nephele/gmagick
Cloning into './vendor/github.com/ctrip-nephele/gmagick'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 61 (delta 0), reused 0 (delta 0), pack-reused 60
Unpacking objects: 100% (61/61), done.
[root@localhost nephele]#
- 发现第二个问题:
govendor sync
迟迟不完成,也没有打印。
[root@localhost nephele]# govendor sync
经过排查发现是 verdor.json
中存在一些墙外包,需要手动下载。
[root@localhost nephele]# git clone https://github.com/golang/text.git ./vendor/golang.org/x/text
Cloning into './vendor/golang.org/x/text'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5585 (delta 0), reused 2 (delta 0), pack-reused 5580
Receiving objects: 100% (5585/5585), 19.64 MiB | 382.00 KiB/s, done.
Resolving deltas: 100% (3721/3721), done.
[root@localhost nephele]#
[root@localhost nephele]# go get gopkg.in/yaml.v2
[root@localhost nephele]# go get gopkg.in/go-playground/validator.v8
[root@localhost nephele]#
[root@localhost nephele]# cp -rf ~/golang_workspace/src/gopkg.in/ ./vendor/
2 测试验证
2.1 编译示例
切换到 example/minimal/
目录,并执行 go build
命令。
[root@localhost nephele]# cd example/minimal/
[root@localhost minimal]# go build
发现问题:
[root@localhost minimal]# go build
# github.com/ctripcorp/nephele/command/autoorient
../../command/autoorient/command.go:35:21: wand.GetImageOrientation undefined (type *gmagick.MagickWand has no field or method GetImageOrientation)
../../command/autoorient/command.go:36:13: wand.AutoOrientImage undefined (type *gmagick.MagickWand has no field or method AutoOrientImage)
# github.com/ctripcorp/nephele/command/quality
../../command/quality/command.go:51:13: wand.SetImageOption undefined (type *gmagick.MagickWand has no field or method SetImageOption)
# github.com/ctripcorp/nephele/command/watermark
../../command/watermark/command.go:197:10: logoWand.Dissolve undefined (type *gmagick.MagickWand has no field or method Dissolve)
先在 main.go
代码中注释掉出错的引用,然后重新编译通过,并生成可执行文件 minimal
:
[root@localhost minimal]# go build
[root@localhost minimal]#
[root@localhost minimal]# ll
total 24776
-rw-r--r-- 1 root root 331 Dec 18 21:03 default.toml
-rw-r--r-- 1 root root 1281 Dec 19 01:52 main.go
-rwxr-xr-x 1 root root 25348548 Dec 19 01:52 minimal
-rw-r--r-- 1 root root 4222 Dec 18 21:03 README.md
-rw-r--r-- 1 root root 313 Dec 18 21:03 server.toml
[root@localhost minimal]#
2.2 修改配置
修改 [storage]
的 root
字段为本地任意目录,本例中使用 /root/nephele/image
为根目录,并在文件系统中创建这个目录。
[root@tensorflow minimal]# vi default.toml
...
[storage]
type = "inline"
root = "/root/nephele/image"
...
2.3 修改配置
执行前面生成的 minimal
文件,出现如下显示,则表示启动成功。
[root@localhost minimal]# ./minimal
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> github.com/ctripcorp/nephele/server/ping.configurater.func1 (4 handlers)
[GIN-debug] GET /image/:key --> github.com/ctripcorp/nephele/server.get (4 handlers)
[GIN-debug] Listening and serving HTTP on :8080
2.4 验证
拷贝一个测试样图,到前面配置的根目录 /root/nephele/image
中。
[root@tensorflow nephele]# ll
total 8
-rw-r--r-- 1 root root 8063 Dec 19 04:32 dog.jpg
在浏览器中输入服务器 ip
和监听的端口,访问 dog.jpg
。
使用 resize
操作,设置图片为 100X100
, 再次访问图片,显示已 resize
成功。