Skip to content

Commit 15f7452

Browse files
committed
<案例><NSQ><生产者和消费者案例>
1 parent 7cff26d commit 15f7452

File tree

7 files changed

+220
-110
lines changed

7 files changed

+220
-110
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
bin

README.md

+2-110
Original file line numberDiff line numberDiff line change
@@ -5,113 +5,5 @@ Go 语言学习资料与社区索引
55
## 类库搜索
66
[整理](awesome_go.md) 感谢 https://github.com/avelino/awesome-go 上面最新整理的
77

8-
9-
10-
鉴于之前有人做的 [索引资料](https://github.com/wonderfo/wonderfogo/wiki) 已经过期超过 2 年,所以特开此坑。各位同学可以通过发起 Fork + Pull Request 的方式进行补充和更新。
11-
12-
**最后更新时间**:2015 年 3 月 9 日
13-
14-
## 交流社区
15-
16-
### 中文社区
17-
18-
- [golanghome社区](http://golanghome.com/):基于 [WeTalk](https://github.com/beego/wetalk) 搭建的新型社区
19-
- [Golang 中国](http://www.golangtc.com/):国内较早的 Go 社区,汇聚各类信息与服务
20-
- [Study Golang](http://studygolang.com/):国内 Go 社区先驱,同样汇聚各类信息与服务
21-
- [Revel 交流论坛](http://gorevel.cn/)[Revel](https://github.com/revel/revel) 框架的中文社区
22-
- [SudoChina](http://www.sudochina.com/):基于 [beego](http://beego.me/) 的问答社区
23-
- QQ 群:
24-
- Go Web 编程群:259316004
25-
- Golang 中国:211938256
26-
- Go 内部实现与标准库:192706294
27-
- Golang Web 开发:231956113
28-
- Go 编程语言:102319854
29-
- Martini 交流群:371440803
30-
31-
### 英文社区
32-
33-
- [golang-nuts 邮件列表](https://groups.google.com/forum/#!forum/golang-nuts):Go 语言官方指定邮件列表讨论区
34-
- [Go+ 社区](https://plus.google.com/u/0/communities/114112804251407510571):Go 语言官方指定 G+ 社区
35-
36-
## 网址导航
37-
38-
- 官方:
39-
- [Playground](http://play.golang.org):Go 语言代码在线运行
40-
- 国内镜像:
41-
- [Go 指南国内镜像](http://tour.golangtc.com/)
42-
- [Go 语言国内下载镜像](http://www.golangtc.com/download)
43-
- [Go 官方网站国内镜像](http://docs.studygolang.com/)
44-
- Web 框架:
45-
- [beego](http://beego.me/):国人开发的轻量级 Web 框架
46-
- [Revel](https://github.com/revel/revel):较早成熟的重量级 Web 框架
47-
- [Martini](https://github.com/go-martini/martini): 一个强大为了编写模块化Web应用而生的GO语言框架
48-
- ORM 以及数据库驱动:
49-
- [xorm](https://github.com/go-xorm/xorm):支持 MySQL、PostgreSQL、SQLite3 以及 MsSQL
50-
- [mgo](http://labix.org/mgo):MongoDB 官方推荐驱动
51-
- [SSDB](http://www.ideawu.com/ssdb/zh_cn/): 高性能 kv 数据库
52-
- 辅助站点:
53-
- [Go Walker](https://gowalker.org):Go 语言在线 API 文档
54-
- [gobuild.io](http://gobuild.io/):Go 语言在线二进制编译与下载
55-
- [Rego](http://regoio.herokuapp.com/):Go 语言正则在线测试
56-
- 开发工具:
57-
- [Emacs24](http://ftp.gnu.org/gnu/emacs/)[配置脚本](https://github.com/wackonline/hack/blob/master/install-mint-dev/install-emacs.d.sh) / [(中文社区)](http://emacser.com/)
58-
- [LiteIDE](https://github.com/visualfc/liteide)
59-
- [Sublime Text 2/3](http://sublimetext.com):[配置教程](http://my.oschina.net/Obahua/blog/110767)
60-
- [IntelliJ IDEA](http://www.jetbrains.com/idea/)
61-
- [Atom](https://atom.io):[配置插件](https://atom.io/packages/go-plus)(感觉还不错,类似sublime,配置比较简单)
62-
- 学习站点:
63-
- [Go China 网络课堂](http://edu.go-china.org/)
64-
- [Go by Example](https://gobyexample.com/)
65-
- [GopherCasts](https://gophercasts.io/)
66-
- [Go database/sql tutorial](http://go-database-sql.org/)
67-
- 支持 Go 的云平台:
68-
- [Koding](https://koding.com/)
69-
- [Nitrous.IO](https://www.nitrous.io/)
70-
- 其它站点:
71-
- [Golang 杂志](https://flipboard.com/section/the-golang-magazine-bJ1GqB):[订阅说明](http://bbs.go-china.org/post/476)
72-
- [Reddit](http://www.reddit.com/r/golang/)
73-
- [Newspaper. IO](http://www.newspaper.io/golang):Golang 新闻订阅
74-
- [Go Newsletter](http://www.golangweekly.com/):Golang 新闻订阅
75-
76-
## 资料汇总
77-
78-
### 中文资料
79-
80-
- 书籍:
81-
- [《深入解析Go》](https://github.com/tiancaiamao/go-internals)
82-
- [《Go实战开发》](https://github.com/astaxie/Go-in-Action)
83-
- [《Go入门指南》](https://github.com/Unknwon/the-way-to-go_ZH_CN)
84-
- [《Go语言标准库》](https://github.com/polaris1119/The-Golang-Standard-Library-by-Example)
85-
- [《Go Web 编程》](https://github.com/astaxie/build-web-application-with-golang)
86-
- [《Go语言博客实践》](https://github.com/achun/Go-Blog-In-Action)
87-
- [《Go语言学习笔记》](https://github.com/qyuhen/book)
88-
- 翻译:
89-
- [Effective Go](http://www.hellogcc.org/effective_go.html)
90-
- 教程:
91-
- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
92-
- [《Go Web基础》](https://github.com/Unknwon/go-web-foundation)
93-
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)
94-
95-
### 英文资料
96-
97-
- 文档:
98-
- [Go Code Review Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments):Go 语言代码风格指导
99-
- [GopherCon 2014](https://github.com/gophercon/2014-talks)
100-
- 书籍:
101-
- [Network programming with Go](http://jan.newmarch.name/go/):[中文版](https://github.com/astaxie/NPWG_zh)
102-
- [Practical Cryptography With Go](https://leanpub.com/gocrypto/read#leanpub-auto-select-bibliography)
103-
- [An Introduction to Programming in Go](http://www.golang-book.com/)
104-
- [Go Bootcamp](http://www.golangbootcamp.com/book)
105-
106-
### 视频资料
107-
108-
- 基础:
109-
- Go Slices and Bytes - Shakeel Mahate:[优酷视频](http://v.youku.com/v_show/id_XNjkzMjM1Mjg4.html) - [Youtube](http://www.youtube.com/watch?v=dKlNSIUSfz0)
110-
- COSCUP 2013:
111-
- Golang & ORM - 林佑安:[优酷视频](http://v.youku.com/v_show/id_XNjkzMTQ1MjYw.html) - [Youtube](http://www.youtube.com/watch?v=VwAtYGyjTks)
112-
- GopherCon 2014:[Youtube](http://confreaks.com/events/gophercon2014)
113-
- GopherCon India 2015: [Youtube](https://www.youtube.com/playlist?list=PLxFC1MYuNgJTY3uQ5Ja4F5Sz305nnrBOq)
114-
115-
## 声明感谢
116-
[https://github.com/Unknwon/go-study-index](https://github.com/Unknwon/go-study-index) 信息整理
117-
>>>>>>> dee930577ffbe85a95af0103823aed84f67b18e1
8+
## demo
9+
[学习过程中的编写](demo/README.md)

demo/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
Golang Demo
3+
==============
4+
5+
## 案例
6+
[nsq-demo](nsq-demo) NSQ 使用案例

demo/nsq-demo/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 监控HTTP数据
2+
3+
## 依赖
4+
- 必须:https://github.com/shirou/gopsutil
5+
- window必须:https://github.com/go-ole/go-ole
6+
- window必须:https://github.com/StackExchange/wmi
7+
8+
## 编译方式
9+
- go 1.5 一下使用:gox 多平台编译
10+
- go 1.5 以后使用:GOOS=linux GOARCH=amd64 go build -o ./bin/nsqProducer nsq_producer.go 进行编译
11+
12+
## 编译平台参考
13+
14+
可用的OS和ARCH的值如下:
15+
16+
```
17+
$GOOS $GOARCH
18+
darwin 386
19+
darwin amd64
20+
darwin arm
21+
darwin arm64
22+
dragonfly amd64
23+
freebsd 386
24+
freebsd amd64
25+
freebsd arm
26+
linux 386
27+
linux amd64
28+
linux arm
29+
linux arm64
30+
linux ppc64
31+
linux ppc64le
32+
netbsd 386
33+
netbsd amd64
34+
netbsd arm
35+
openbsd 386
36+
openbsd amd64
37+
openbsd arm
38+
plan9 386
39+
plan9 amd64
40+
solaris amd64
41+
windows 386
42+
windows amd64
43+
```
44+
## 部署方案:
45+
- 1.防火墙开发TCP:8899 端口
46+
- 2.Supervisord 进行部署

demo/nsq-demo/main.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"time"
6+
"github.com/nsqio/go-nsq"
7+
"strconv"
8+
)
9+
10+
func main() {
11+
go startConsumer()
12+
startProducer()
13+
}
14+
15+
// 生产者
16+
func startProducer() {
17+
cfg := nsq.NewConfig()
18+
producer, err := nsq.NewProducer("127.0.0.1:4150", cfg)
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
// 发布消息
23+
for {
24+
i := time.Now().Unix()
25+
if err := producer.Publish("test", []byte("test message - "+ strconv.FormatInt(i, 10))); err != nil {
26+
log.Fatal("publish error: " + err.Error())
27+
}
28+
time.Sleep(100 * time.Millisecond)
29+
}
30+
}
31+
32+
// 消费者
33+
func startConsumer() {
34+
cfg := nsq.NewConfig()
35+
consumer, err := nsq.NewConsumer("test", "sensor01", cfg)
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
// 设置消息处理函数
40+
consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
41+
log.Println(string(message.Body))
42+
return nil
43+
}))
44+
// 连接到单例nsqd
45+
if err := consumer.ConnectToNSQD("127.0.0.1:4150"); err != nil {
46+
log.Fatal(err)
47+
}
48+
<-consumer.StopChan
49+
}

demo/nsq-demo/nsq_consumer.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
_ "github.com/go-sql-driver/mysql" // import your used driver
5+
"github.com/nsqio/go-nsq"
6+
"log"
7+
"github.com/astaxie/beego/orm"
8+
"fmt"
9+
"time"
10+
"strconv"
11+
)
12+
13+
func init() {
14+
orm.RegisterDataBase("default", "mysql", "root:@tcp(172.16.50.143:4000)/shtelecom?charset=utf8", 30)
15+
orm.Debug = true
16+
17+
}
18+
19+
func main() {
20+
o := orm.NewOrm()
21+
o.Using("default") // 默认使用 default,你可以指定为其他数据库
22+
cfg := nsq.NewConfig()
23+
consumer, err := nsq.NewConsumer("shtelecom-active", "mohoo", cfg)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
// 设置消息处理函数
28+
consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
29+
log.Println(string(message.Body))
30+
i := time.Now().Unix()
31+
res, err := o.Raw(" INSERT INTO openid_active (openid, acttime, type, note) VALUES(?, ?, ?, ?); ", string(message.Body), strconv.FormatInt(i, 10), "BIND", "WX").Exec()
32+
if err == nil {
33+
num, _ := res.RowsAffected()
34+
fmt.Println("mysql row affected nums: ", num)
35+
}
36+
time.Sleep(3 * time.Millisecond)
37+
return nil
38+
}))
39+
// 连接到单例nsqd
40+
if err := consumer.ConnectToNSQD("172.16.50.143:4150"); err != nil {
41+
log.Fatal(err)
42+
}
43+
<-consumer.StopChan
44+
}

demo/nsq-demo/nsq_producer.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"github.com/labstack/echo"
5+
"net/http"
6+
"github.com/nsqio/go-nsq"
7+
"log"
8+
"fmt"
9+
"encoding/json"
10+
)
11+
12+
type ProBody struct {
13+
Body string `json:"body"` // 主体内容
14+
Uuid string `json:"uuid"` // 时间戳
15+
Acttime string `json:"acttime"` // 活动时间
16+
Type string `json:"type"` // 类型
17+
Note string `json:"note"` // 备注
18+
}
19+
20+
type ActBody struct {
21+
Openid string `json:"openid"` // 主体内容
22+
Acttime string `json:"acttime"` // 活动时间
23+
Type string `json:"type"` // 类型
24+
Note string `json:"note"` // 备注
25+
Uuid string `json:"uuid"` // UUID
26+
}
27+
28+
var producer *nsq.Producer
29+
30+
func init() {
31+
cfg := nsq.NewConfig()
32+
var err error
33+
producer, err = nsq.NewProducer("172.16.50.143:4150", cfg)
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
}
38+
39+
func main() {
40+
e := echo.New()
41+
e.POST("/producer", func(c echo.Context) error {
42+
body := new(ProBody)
43+
var err error
44+
if err = c.Bind(body); err != nil {
45+
return c.JSON(http.StatusMethodNotAllowed, "内容体不对")
46+
}
47+
if body.Body == "" {
48+
return c.JSON(http.StatusMethodNotAllowed, "body不能为空")
49+
}
50+
51+
// 微信活跃数据
52+
var actBody ActBody
53+
actBody.Openid = body.Body
54+
actBody.Acttime = body.Acttime
55+
actBody.Uuid = body.Uuid
56+
actBody.Type = body.Type
57+
actBody.Note = body.Note
58+
59+
jsons, errs := json.Marshal(actBody) //转换成JSON返回的是byte[]
60+
if errs != nil {
61+
fmt.Println(errs.Error())
62+
}
63+
if err := producer.Publish("shtelecom-active", jsons); err != nil {
64+
return c.JSON(http.StatusBadGateway, "publish error: "+err.Error())
65+
} else {
66+
e.Logger.Info("SendOk: " + body.Uuid)
67+
return c.JSON(http.StatusOK, "ok")
68+
}
69+
})
70+
e.Logger.Fatal(e.Start(":8899"))
71+
}

0 commit comments

Comments
 (0)