loraserver 源码解析 (一) 构建

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wangjunsheng/article/details/80830835

源码地址

loraserver     lorawan netserver,和 gateway通信 与app server交互

lorawan         它用于lorawan通信数据的序列化

 

安装golang

安装Golang

 

下载源码

go get -u github.com/brocaar/loraserver
go get -u github.com/brocaar/lorawan

由于我的 $GOPATH 是  ~/go/gopath

所以它们最终下载到了 ~/go/gopath/src/github.com/loraserver 和 ~/go/gopath/src/github.com/lorawan

 

安装依赖库

loraserver lorawan用到了大量的第三方库

loraserver 目录下的 Makefile的 requirements 罗列了所有需要安装的第三方库(好像还是不全哦)

dev-requirements:                                                                                                                                     
    go get -u github.com/kisielk/errcheck                                                                                                             
    go get -u golang.org/x/lint/golint                                                                                                                
    go get -u github.com/smartystreets/goconvey                                                                                                       
    go get -u golang.org/x/tools/cmd/stringer                                                                                                         
    go get -u github.com/golang/protobuf/protoc-gen-go                                                                                                
    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger                                                                               
    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway                                                                          
    go get -u github.com/elazarl/go-bindata-assetfs/...                                                                                               
    go get -u github.com/jteeuwen/go-bindata/...                                                                                                      
    go get -u github.com/golang/dep/cmd/dep                                                                                                           
    go get -u github.com/goreleaser/goreleaser                                                                                                        
    go get -u github.com/goreleaser/nfpm 

如果你可以爬墙,也许可以直接 make dev-requirements 来安装,说不定就全部装上了。

我不能爬墙,所以 golang.org  google.golang.org 等网站是无法通过go get 安装的,会提示类似下面的错误

"golang.org/x/net": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/net?go-get=1": Get http://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused

幸运的是 golang.org   google.golang.org 依赖库 在 github 上都有

都在 https://github.com/golang 这里面

我们可以通过 git clone gitbub的代码来曲线安装,具体的操作方法请参考

logrus 安装

go stringer 用法

GoConvey测试框架使用指南

go redis 客户端  这个 redigo 已经被移到 gomodule 仓库, 为了编译 loraserver,我们得下载 https://github.com/garyburd/redigo 的redigo。  

redigo

go grpc protobuf 安装

 

我的安装情况大概是这样的

~/go/gopath/src/github.com/brocaar/loraserver $ make dev-requirements
go get -u github.com/kisielk/errcheck
package golang.org/x/tools/go/packages: unrecognized import path "golang.org/x/tools/go/packages" (https fetch: Get https://golang.org/x/tools/go/packages?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
Makefile:49: recipe for target 'dev-requirements' failed
make: *** [dev-requirements] Error 1
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u golang.org/x/lint/golint
package golang.org/x/lint/golint: unrecognized import path "golang.org/x/lint/golint" (https fetch: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/smartystreets/goconvey
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u golang.org/x/tools/cmd/stringer
package golang.org/x/tools/cmd/stringer: unrecognized import path "golang.org/x/tools/cmd/stringer" (https fetch: Get https://golang.org/x/tools/cmd/stringer?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/golang/protobuf/protoc-gen-go
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
package google.golang.org/genproto/googleapis/api/annotations: unrecognized import path "google.golang.org/genproto/googleapis/api/annotations" (https fetch: Get https://google.golang.org/genproto/googleapis/api/annotations?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
package google.golang.org/genproto/googleapis/api/annotations: unrecognized import path "google.golang.org/genproto/googleapis/api/annotations" (https fetch: Get https://google.golang.org/genproto/googleapis/api/annotations?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/jteeuwen/go-bindata/...
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/golang/dep/cmd/dep
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/goreleaser/goreleaser
package golang.org/x/sync/errgroup: unrecognized import path "golang.org/x/sync/errgroup" (https fetch: Get https://golang.org/x/sync/errgroup?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
package golang.org/x/oauth2: unrecognized import path "golang.org/x/oauth2" (https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/goreleaser/nfpm

然后开始解决一些被墙了的三方库

到 https://github.com/golang 上面找, 后面的路径是一样的

 

github.com/kisielk/errcheck 

cd $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/tools.git                                                                          
Cloning into 'tools'...                                                                                                                               
remote: Enumerating objects: 107, done.                                                                                                               
remote: Counting objects: 100% (107/107), done.                                                                                                       
remote: Compressing objects: 100% (84/84), done.                                                                                                      
remote: Total 24321 (delta 28), reused 70 (delta 17), pack-reused 24214                                                                               
Receiving objects: 100% (24321/24321), 11.77 MiB | 191.00 KiB/s, done.                                                                                
Resolving deltas: 100% (16861/16861), done.                                                                                                           
Checking connectivity... done. 

下面2个命令很耗时,耐心等待~~~~~                                                                                                                       
~/go/gopath/src/golang.org/x $ go get -u golang.org/x/tools/go/packages
~/go/gopath/src/golang.org/x $ go get -u github.com/kisielk/errcheck

 

golang.org/x/lint/golint

~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/lint.git                                                                           
Cloning into 'lint'...                                                                                                                                
remote: Enumerating objects: 749, done.                                                                                                               
remote: Total 749 (delta 0), reused 0 (delta 0), pack-reused 749                                                                                      
Receiving objects: 100% (749/749), 333.46 KiB | 187.00 KiB/s, done.                                                                                   
Resolving deltas: 100% (442/442), done.                                                                                                               
Checking connectivity... done.                                                                                                                        
~/go/gopath/src/golang.org/x $ go get -u -v golang.org/x/lint/golint                                                                                  
Fetching https://golang.org/x/lint/golint?go-get=1                                                                                                    
https fetch failed: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                             
golang.org/x/lint (download)                                                                                                                          
Fetching https://golang.org/x/lint?go-get=1                                                                                                           
https fetch failed: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                                    
Fetching https://golang.org/x/tools/go/ast/astutil?go-get=1                                                                                           
https fetch failed: Get https://golang.org/x/tools/go/ast/astutil?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                    
golang.org/x/tools (download)                                                                                                                         
Fetching https://golang.org/x/tools/go/gcexportdata?go-get=1                                                                                          
https fetch failed: Get https://golang.org/x/tools/go/gcexportdata?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                   
Fetching https://golang.org/x/tools/go/internal/gcimporter?go-get=1                                                                                   
https fetch failed: Get https://golang.org/x/tools/go/internal/gcimporter?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                            
golang.org/x/tools/go/ast/astutil                                                                                                                     
golang.org/x/lint                                                                                                                                     
golang.org/x/lint/golint    

看完日志,我明白了,go get -u 之所以那么慢,是因为总是连接不上,其实我都下载好了的啊。好吧。是不是我git clone就行了,然后如果有需要生成bin文件,我自己手动 go install 

 

golang.org/x/tools/cmd/stringer

x/tools 啊, 我前面已经 git clone 好了,吸取前面的教训,直接安装就行啦

~/go/gopath/src/golang.org/x $ cd $GOPATH/src/golang.org/x/tools/cmd/stringer/                                                                        
~/go/gopath/src/golang.org/x/tools/cmd/stringer $ go install   

搞完后 跑到 $GOPATH/bin , 是不是多了个 stringer
~/go/gopath/bin $ ls -tl                                                                                                                              
total 70665                                                                                                                                           
-rwxrwxr-x   1 zlgmcu         zlgmcu    6442218 2019-01-25 11:37 stringer  

 

google.golang.org/genproto/googleapis/api/annotations

这个是 装 grpc时 产生的错误

go grpc protobuf 安装 及 用法简单示例

 

golang.org/x/sync/errgroup

golang.org/x/oauth2

cd $GOPATH/src/golang.org/x/
~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/sync.git
Cloning into 'sync'...
remote: Enumerating objects: 4, done.        
remote: Counting objects: 100% (4/4), done.        
remote: Compressing objects: 100% (4/4), done.        
remote: Total 103 (delta 0), reused 1 (delta 0), pack-reused 99        
Receiving objects: 100% (103/103), 40.98 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Checking connectivity... done.
~/go/gopath/src/golang.org/x $ git  clone https://github.com/golang/oauth2.git
Cloning into 'oauth2'...
remote: Enumerating objects: 5, done.        
remote: Counting objects: 100% (5/5), done.        
remote: Compressing objects: 100% (2/2), done.        
remote: Total 1686 (delta 4), reused 3 (delta 3), pack-reused 1681        
Receiving objects: 100% (1686/1686), 517.60 KiB | 40.00 KiB/s, done.
Resolving deltas: 100% (1036/1036), done.
Checking connectivity... done.

cloud.google.com/go/pubsub

找了好久,总算找到了,它们全部都在 下面这个链接里

https://github.com/googleapis/google-cloud-go

下载后,拷贝到合适的目录即可

mkdir -p $GOPATH/src/cloud.google.com                                                                               
cd $GOPATH/src/cloud.google.com  
~/go/gopath/src/cloud.google.com $ git clone https://github.com/googleapis/google-cloud-go.git                                                         
Cloning into 'google-cloud-go'...                                                                                                                      
remote: Enumerating objects: 427, done.                                                                                                                
remote: Counting objects: 100% (427/427), done.                                                                                                        
remote: Compressing objects: 100% (186/186), done.                                                                                                     
remote: Total 20434 (delta 266), reused 332 (delta 221), pack-reused 20007                                                                             
Receiving objects: 100% (20434/20434), 14.46 MiB | 71.00 KiB/s, done.                                                                                  
Resolving deltas: 100% (14029/14029), done.                                                                                                            
Checking connectivity... done.   
~/go/gopath/src/cloud.google.com $ mv google-cloud-go go

 

google.golang.org/api/iterator

googleapis go 语言版本在这 https://github.com/googleapis/google-api-go-client

~/go/gopath/src/google.golang.org $ git clone https://github.com/googleapis/google-api-go-client.git                                                  
Cloning into 'google-api-go-client'...                                                                                                                
remote: Enumerating objects: 1495, done.                                                                                                              
remote: Counting objects: 100% (1495/1495), done.                                                                                                     
remote: Compressing objects: 100% (973/973), done.                                                                                                    
remote: Total 72438 (delta 687), reused 1013 (delta 363), pack-reused 70943                                                                           
Receiving objects: 100% (72438/72438), 132.27 MiB | 642.00 KiB/s, done.                                                                               
Resolving deltas: 100% (35349/35349), done.                                                                                                           
Checking connectivity... done.                                                                                                                                                                                                                                        
~/go/gopath/src/google.golang.org $ mv google-api-go-client/ api   

 

其它的方法都类似 缺啥装啥

 

redis 安装

   下载 https://redis.io/download   redis-4.0.10.tar
   
   tar -xvf redis-4.0.10.tar.gz
   make   
   make test   

   sudo make install

 

生成migrations

运行 go generate cmd/loraserver/main.go

   会借助 go-bindata 生成 internal/migrations/migrations_gen.go

 

 

安装完所有的依赖后 dep ensure -v

   在我电脑下显示如下,被墙了,会有2个失败的,忽略即可
   

The following issues were found in Gopkg.toml:


  ✗ unable to deduce repository and source type for "google.golang.org/grpc": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://google.golang.org/grpc?go-get=1": Get http://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused
  ✗ unable to deduce repository and source type for "golang.org/x/net": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/net?go-get=1": Get http://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused


  ProjectRoot name validation failed

 

装loraserver后,我多了这些第三方库

~/go/gopath/src/github.com $ ls
alecthomas  fatih       grpc-ecosystem  lib          rubenv
apex        fsnotify    hashicorp       magiconair   satori
aws         garyburd    imdario         masterminds  sirupsen
blakesmith  ghodss      jacobsa         mattn        smartystreets
brocaar     golang      jmoiron         mitchellh    spf13
caarlos0    gomodule    jteeuwen        nsf          
campoy      google      jtolds          pelletier    
eclipse     goreleaser  kardianos       pkg          
elazarl     grpc        kisielk         rogpeppe   
~/go/gopath/src $ ls
github.com  google.golang.org  
golang.org  gopkg.in   
~/go/gopath/src/golang.org/x $ ls
crypto  lint  net  sys  text  tools
~/go/gopath/src/google.golang.org $ ls
genproto  grpc
~/go/gopath/src/gopkg.in $ ls
gorp.v1  yaml.v2

多了这么多程序

~/go/gopath/bin/linux_386 $ ls
cobra               gocode    protoc-gen-go
dep                 goconvey  protoc-gen-grpc-gateway
errcheck            godef     protoc-gen-swagger
go-bindata          golint    stringer
go-bindata-assetfs  govendor  

 

尝试编译后会报个语法错误

loraserver 有4个地方用 uuid的时候 没有处理error返回值

dp.DeviceProfile.DeviceProfileID = uuid.NewV4().String()

github.com/satori/go.uuid 目录的 generator.go 改一下

/* raw 
func NewV4() (UUID, error) {
	return global.NewV4()
}
*/
func NewV4() (UUID) { // modify by wjs, fix loraserver build error
	uuid,_ := global.NewV4()
	return uuid 
}

这样改不好,正确的做法我想应该是改 loraserver 源码,判断 error返回值。暂时这么改通过编译吧:)

 

 

make build  (在loraserver目录)

   会在 build 目录下生成 loraserver 程序。

~/go/gopath/src/github.com/brocaar/loraserver $ make build

于是生成可执行文件

~/go/gopath/src/github.com/brocaar/loraserver/build $ ls

loraserver


loraserver 源码解析 (二) 运行起来

展开阅读全文

没有更多推荐了,返回首页