分享
  1. 首页
  2. 文章

使用Redis做任务队列(Golang)

xcltapestry · · 9891 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

上篇用纯go在本机模拟了下分布式队列的东西。这里补上Redis队列部份。
用Redis做队列有下面三个问题需解决:

1. 队列构建
使用Redis的RPUSH/LPOP来解决

2. 参数传递/解析
客户端将JOSN参数存入Redis,Server端取出后解析还原。

3. 连接池

redigo支持Redis连接池

下面代码就是具体解决实现:

//Redis做后台任务队列
//author: Xiong Chuan Liang
//date: 2015年3月25日
package main
import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"time"
	"github.com/garyburd/redigo/redis"
)
func main() {
	r, err := newRedisPool("", "")
	if err != nil {
		fmt.Println(err)
		return
	}
	//将job放入队列
	r.Enqueue()
	//依次取出两个Job
	r.GetJob()
	r.GetJob()
}
type RedisPool struct {
	pool *redis.Pool
}
func newRedisPool(server, password string) (*RedisPool, error) {
	if server == "" {
		server = ":6379"
	}
	pool := &redis.Pool{
		MaxIdle: 3,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", server)
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := c.Do("AUTH", password); err != nil {
					c.Close()
					return nil, err
				}
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
	return &RedisPool{pool}, nil
}
type Job struct {
	Class string `json:"Class"`
	Args []interface{} `json:"Args"`
}
//模拟客户端
func (r *RedisPool) Enqueue() error {
	c := r.pool.Get()
	defer c.Close()
	j := &Job{}
	j.Class = "mail"
	j.Args = append(j.Args, "xcl_168@aliyun.com", "", "body", 2, true)
	j2 := &Job{}
	j2.Class = "Log"
	j2.Args = append(j2.Args, "ccc.log", "ddd.log", []int{222, 333})
	for _, v := range []*Job{j, j2} {
		b, err := json.Marshal(v)
		if err != nil {
			return err
		}
		_, err = c.Do("rpush", "queue", b)
		if err != nil {
			return err
		}
	}
	fmt.Println("[Enqueue()] succeed!")
	return nil
}
//模拟Job Server
func (r *RedisPool) GetJob() error {
	count, err := r.QueuedJobCount()
	if err != nil || count == 0 {
		return errors.New("暂无Job.")
	}
	fmt.Println("[GetJob()] Jobs count:", count)
	c := r.pool.Get()
	defer c.Close()
	for i := 0; i < int(count); i++ {
		reply, err := c.Do("LPOP", "queue")
		if err != nil {
			return err
		}
		var j Job
		decoder := json.NewDecoder(bytes.NewReader(reply.([]byte)))
		if err := decoder.Decode(&j); err != nil {
			return err
		}
		fmt.Println("[GetJob()] ", j.Class, " : ", j.Args)
	}
	return nil
}
func (r *RedisPool) QueuedJobCount() (int, error) {
	c := r.pool.Get()
	defer c.Close()
	lenqueue, err := c.Do("llen", "queue")
	if err != nil {
		return 0, err
	}
	count, ok := lenqueue.(int64)
	if !ok {
		return 0, errors.New("类型转换错误!")
	}
	return int(count), nil
}
/*
运行结果:
[Enqueue()] succeed!
[GetJob()] Jobs count: 2
[GetJob()] mail : [xcl_168@aliyun.com body 2 true]
[GetJob()] Log : [ccc.log ddd.log [222 333]]
[root@xclos src]#
*/
可以看到Go已能取得参数。都是些最基础的东西。

MAIL: xcl_168@aliyun.com

BLOG: http://blog.csdn.net






有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:xcltapestry

查看原文:使用Redis做任务队列(Golang)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
9891 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏