NoSql学习之路一redis做消息队列

nosql最近很火,我也研究研究,呵呵,今天来说说redis,简单的那些增删改查 就不说了,网上太多了,我研究了一下,redis做消息队列

首先是用的redis的发布订阅做的消息队列实现消息队列,

测试是在本机做的,redis在虚拟机2003的系统,386M内存,本机是8.1的系统,6g内存

我是用了500个线程模拟并发每个线程发布500条数据,在发布消息的时候,服务器的内存占用并不高,cpu占有率挺高的

这是每个线程用的时间,都差不多3 4秒


另一端用sqlserver保存消息,结果插到24万左右的时候报错了,原来是日志文件太大了,超过1G了,而数据库文件只有50M,时间大概用了20多分钟吧,

今天又用mongodb试了一下,效率比sqlserver高很多,只用了56秒就ok,对了我保存消息的时候,只用一个线程

下面上代码

发布端

namespace publish
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int b = 0; b < 500; b++)
            {
                Thread thread = new Thread(new ThreadStart(() =>
                   {
                       RedisClient redisClient = RedisClientFactory.Instance.CreateRedisClient("192.168.189.134",6379);
                       Stopwatch stopwatch1 = new Stopwatch();
                       stopwatch1.Start();
                       for (int i = 0; i < 500; i++)
                       {
                           UserInfo userInfo = new UserInfo();
                           userInfo.Age = 1000;
                           userInfo.Name = "XX";
                           userInfo.I = i;
                           userInfo.ThreadId = Thread.CurrentThread.ManagedThreadId;
                           redisClient.Publish("aa",Encoding.Default.GetBytes("XX在广播 频道:aa线程:"+Thread.CurrentThread.ManagedThreadId.ToString()));
                       }
                       stopwatch1.Stop();
                       Console.WriteLine("aa用时:" + stopwatch1.ElapsedMilliseconds + "毫秒");
                   }));
                thread.Start();
            }
            Console.ReadKey();
        }
    }


    class UserInfo
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int ThreadId { get; set; }
        public int I { get; set; }
    }

}

接受端,
 class Program
    {
        static void Main(string[] args)
        {
            RedisClient Redis = RedisClientFactory.Instance.CreateRedisClient("192.168.189.134",6379);//redis服务IP和端口
            Redis.Subscribe(new string[] { "it","aa" });
            Thread thread = new Thread(() =>
            {
                Recode recode = new Recode();
                while (true)
                {
                    byte[][] bb =
                         Redis.ReceiveMessages();
                    Mongo.Insert(new r()
                    {
                        message1 = Encoding.Default.GetString(bb[0]),message2 = Encoding.Default.GetString(bb[1]),message3 = Encoding.Default.GetString(bb[2]),date=DateTime.Now
                    });
                }
            });
            thread.Start();
            Console.WriteLine("OK");
            Console.ReadKey();
        }

sqlserver保存方法
 public void Insert(string message1,string message2,string message3)
        {

            using (sqlConnection connection = new sqlConnection("server=localhost;database=ThreeLayer;Integrated Security=true"))
            {
                connection.open();
                using (sqlCommand command = new sqlCommand("insert into redis(Date,Message1,Message2,Message3) values(@d,@m1,@m2,@m3)",connection))
                {
                    command.Parameters.Add(new sqlParameter("@d",DateTime.Now));
                    command.Parameters.Add(new sqlParameter("@m1",message1));
                    command.Parameters.Add(new sqlParameter("@m2",message2));
                    command.Parameters.Add(new sqlParameter("@m3",message3));
                    command.ExecuteScalar();
                }
            }
        }

mongodb保存
  public static void Insert(r r)
        {
            string connStr = ConfigurationManager.AppSettings["MongoServerSettings"];//获取连接字符串

            MongoServer _server = MongoServer.Create(connStr);//创建mongodb服务对应的对象

            MongoDatabase _db = _server.GetDatabase("test2");//获取数据库,如果没有,会自动创建一个
            var collectionName = typeof(r).Name;//指定集合的名字 
            var collection = _db.GetCollection<r>(collectionName);//获取集合,如果集合不存在,那么直接创建一个
            collection.Insert(r);//将数据插入到 集合里面去
}
今天到此结束,欢迎小伙伴,提意见

相关文章

校园网上订餐系统系统主要功能模块包括公告内容(公告栏、轮...
从今天开始,正式进入项目阶段。本次的项目是跟着黑马的瑞吉...
在可预见的将来,IT不会萎缩,只会越发展越庞大,最终会渗透...
vulntarget-b靶场最详细通关记录。
MongoDB是一个开源、高性能、支持海量数据存储的。
【NoSQL数据库技术与应用】课本代码、课后答案(持续更新)