redis + messagepack 消息队列

0x01缘由:

  当需要在不同的服务之间传递状态信息或通知,以及订阅者和发布者的模式时,要使用消息队列。

  典型的场景有:

   WEB和C后台/Python后台/Java后台传递一些消息时,如配置文件、指令等。

0x02开源程序版本:

   msgpack(消息序列化):git clone https://github.com/msgpack/msgpack-c.git

   hredis(redis c/c++开发接口):https://github.com/gilala/hredis

   消息序列化工具还有protobuf,两个各有利弊,msgpack用python语言相当好用,为了尝试使用C++决定尝试用一下。

  

0x03动手造轮子(木)的目的:

  • 锻炼动手能力;

  • 了解一些新工具,开阔思路;

  • 复习makefile的编写;

0x04阅读你能获得什么:

  • makefile的编写,如何检测.h的变化,不用make clean;
  • messagepack c++接口的使用;
  • redis c结构的使用;
  • 公司产品中得到利用;

0x05 messagepack例子:

/*
 * msgpack.cpp
 *
 *  Created on: 2017年5月24日
 *      Author: Administrator
 */
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#include <msgpack.hpp>

typedef struct item
{
		int age;
		std::string name;
		MSGPACK_DEFINE(age, name);
}Item;


int main()
{
	Item data;
	data.age = 28;
	data.name = "pym";

    std::stringstream buffer;
    msgpack::pack(buffer, data);

    // send the buffer ...
    buffer.seekg(0);

    // deserialize the buffer into msgpack::object instance.
	std::string str(buffer.str());

	msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

	// deserialized object is valid during the msgpack::object_handle instance alive.
	msgpack::object deserialized = oh.get();

	// msgpack::object supports ostream.
	std::cout << deserialized << std::endl;

	// convert msgpack::object instance into the original type.
	// if the type is mismatched, it throws msgpack::type_error exception.
	Item dst;
	deserialized.convert(dst);

	printf("name: %s\nage : %d\n", dst.name.c_str(), dst.age);
	return 0;
}

0x05 其他源码:

      https://github.com/pangyemeng/redis_message_queue.git

0x06 存在几个问题未解决:

  rpush key string 为字符串,所以序列化后不能存0字符串结尾的标识;
  消息类型中不能存在为0的证书,因为序列化后会为0,导致存入redis后截断;


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值