本作者從下載地址:https://download.csdn.net/download/LongtengGensSupreme/12157626下載redis包解壓到本地文件目錄:E:\Source\redisfile\Redis-x64-3.2.100,如下圖所示

為了方便啟動,我們在該目錄下新建一個 startredis.bat 的文件,
然后將以下內容寫入文件:redis-server redis.windows.conf
這個命令其實就是在調用 redis-server.exe 命令來讀取 redis.window.conf 的內容,
雙擊剛才創建好的 startredis.bat 文件,就能成功的看到 Redis 啟動,如下圖所示:
redis-server啟動之后放着就可以了。可以使用客戶端自帶工具來測試redis服務器是否可用,
Redis 自帶的一個客戶端工具,它可以用來連接到我們當前的 Redis 服務器,點擊同一個文件夾下的 redis-cli.exe 文件,啟動客戶端,如下圖

redis-cli客戶端啟動成功,如下圖所示

我們做以下測試:在客戶端dos界面輸入 set key1 value1,回車,可以看到客戶端顯示:
在輸入 get key1 回車,客戶端顯示:
通過上述工作,我們便在 Windows 的環境下安裝好了Redis,我們的准備工作已完成,下面添加簡單的使用Redis用作Redis用作消息隊列MQ的案例,
以商品購買為案例,下單--》增加積分 如下:
2、新建項目控制台項目RedisMessageQueue,使用Nuget包管理器添加ServiceStack.Redis,
using System; using ServiceStack.Redis; namespace RedisMessageQueue { class Program { static void Main(string[] args) { Console.WriteLine("消息隊列演示"); RedisClient redisClient = new RedisClient("localhost:6379"); string ss = "這是redisMQ"; Console.WriteLine($"消息隊列輸入內容:{ss}"); //生產者,LPush左端插入數據,RPush右端插入數據 redisClient.LPush("mq", System.Text.Encoding.UTF8.GetBytes(ss)); System.Diagnostics.Process.Start(@"F:\Person\linjie\Logteng\ConsoleApp1\bin\Debug\netcoreapp3.1\redisClientJiFen.exe"); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); int i = 0; string sn = null; while (true) { sn = $"WG{DateTime.Now.ToString("yyyMMddHHmmssfffffff")}"; Console.WriteLine($"生成第 {i++} 個訂單,訂單號:{sn}"); Console.WriteLine("...........處理訂單............"); Console.WriteLine($"訂單 {sn} 處理完成"); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3)); redisClient.LPush("mq", System.Text.Encoding.UTF8.GetBytes(ss + i++)); } //消費者,RPop右端取出數據,LPop左端取出數據 //byte[] bytes1 = redisClient.RPop("mq"); //string mr1 = System.Text.Encoding.UTF8.GetString(bytes1); //Console.WriteLine($"消息隊列獲取內容:{mr1}"); //LPush -----》RPop //RPush -----》LPop //消費者 RPop LPop BRPop BLPop //推模型 BRPop BLPop,被動的去接受數據 //拉模型 RPop LPop, 主動的去獲收數據 //總結,一對一通訊 //byte[][] bytes = redisClient.BRPop("mq", 60); //byte[] bytes = redisClient.BRPopValue("mq", 60); //string mr = System.Text.Encoding.UTF8.GetString(bytes); //Console.WriteLine($"消息隊列獲取內容:{mr}"); //while (true) //{ // byte[] bytes = redisClient.RPop("mq"); // if (bytes != null) // { // string mr = System.Text.Encoding.UTF8.GetString(bytes); // Console.WriteLine($"{mr}"); // } // else // { // Console.WriteLine($"消息隊列沒有數據"); // } // System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); //} Console.ReadKey(); } } }
3、添加積分處理控制台項目redisClientJiFen
using ServiceStack.Redis; using System; namespace redisClientJiFen { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); RedisClient redisClient = new RedisClient("localhost:6379"); Console.WriteLine($"RedisClient連接成功"); while (true) { byte[] bytes = redisClient.RPop("mq"); if (bytes != null) { string mr = System.Text.Encoding.UTF8.GetString(bytes); Console.WriteLine($"訂單 {mr} 處理積分完成"); } else { Console.WriteLine($"消息隊列沒有數據"); } System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } } }
4、運行效果

