本次和大家分享的是RedisMQ隊列的用法,前兩篇文章隊列工廠之(MSMQ)和隊列工廠之RabbitMQ分別簡單介紹對應(yīng)隊列環(huán)境的搭建和常用方法的使用,加上本篇分享的RedisMQ那么就完成了咋們隊列工廠"三劍客"的目標(biāo)了哈哈;Redis的作用不僅僅局限于隊列,更多的一般都使用它的key,value的形式來存儲session或者h(yuǎn)ash的方式存儲一些常用的數(shù)據(jù),當(dāng)然這不是本章分享的內(nèi)容(之前有些文章有講過redis的使用場景和代碼分享各位可以看下),這QueueReposity-隊列工廠最后一篇結(jié)束后,筆者后面分享的可能是netcore方面的一些東西了,vs2017出來了簡單創(chuàng)建netcore項目之后發(fā)現(xiàn)與之前的版本有些變動,例如:沒有project.json,怎么配置生成跨平臺程序等問題,需要一個一個學(xué)習(xí)和嘗試,網(wǎng)上搜索的文章還很少,全靠閱讀全英文的官網(wǎng)來學(xué)習(xí)了哈哈;希望大家能夠喜歡本篇文章,也希望各位多多"掃碼支持"和"推薦"謝謝!

Redis安裝和RedisClient工具的使用
封裝RedisMQ隊列的讀和寫
隊列工廠之RedisMQ測試用例
下面一步一個腳印的來分享:
Redis安裝和RedisClient工具的使用
首先要使用redis需要下載安裝Redis,這里由于之前的文章有講解在windows下怎么搭建redis服務(wù),所以不再贅述,各位可以點擊搭建Redis服務(wù)端,并用客戶端連接,因此我這里直接分享怎么使用RedisClient工具,這工具使用起來比較簡單和方便,首先去這個地址下載:
http://dlsw.baidu.com/sw-search-sp/soft/a2/29740/RedisClient20140730.1406883096.exe
安裝-》打開軟件,能看到如圖的界面:

-》點擊“Server”-》Add-》輸入一個昵稱,你redis服務(wù)端的ip,端口-》確認(rèn)即可:

這個時候你redisclient的配置工作就完成了是不是很簡單啊,-》再來點擊剛才創(chuàng)建昵稱-》雙擊打開redis的第一個數(shù)據(jù)庫db0(這里就是在沒有指定數(shù)據(jù)庫位置時候存儲數(shù)據(jù)的地方)-》能看到你存儲的數(shù)據(jù)key:

如果想看某個name的數(shù)據(jù)直接雙擊對應(yīng)的name就行了-》這里是我redis服務(wù)存儲的一個hash數(shù)據(jù)的截圖:

是不是很方便,這個客戶端可以直接刪除你不想要的數(shù)據(jù)-》右鍵選中您想刪除的name-》Delete即可刪除:

怎么樣,這個RedisClient工具學(xué)會了么,是不是挺簡單的呢;
封裝RedisMQ隊列的讀和寫
到這里終于來到我們代碼分享的時刻了,盡管QueueReposity-隊列工廠已經(jīng)開源了源碼,這里還是單獨分享一次只有RedisMQ的代碼;首先創(chuàng)建一個名稱為:QRedisMQ的class-》繼承 PublicClass.ConfClass<T>-》再實現(xiàn)接口IQueue,最后就有了我們實現(xiàn)接口方法體代碼:
/// <summary>
/// RedisMQ
/// </summary>
public class QRedisMQ : PublicClass.ConfClass<QRedisMQ>, IQueue
{
private IRedisClient redis = null;
public void Create()
{
if (string.IsNullOrWhiteSpace(this.ApiUrl) ||
string.IsNullOrWhiteSpace(this.UserPwd)) { throw new Exception("創(chuàng)建QRedisMQ隊列需要指定隊列:ApiUrl,UserPwd"); }
this.ApiKey = string.IsNullOrWhiteSpace(this.ApiKey) ? "6379" : this.ApiKey;
redis = redis ?? new RedisClient(this.ApiUrl, Convert.ToInt32(this.ApiKey), this.UserPwd);
}
public long Total(string name = "Redis_01")
{
if (redis == null) { throw new Exception("請先創(chuàng)建隊列連接"); }
if (string.IsNullOrWhiteSpace(name)) { throw new Exception("name不能為空"); }
return redis.GetListCount(name);
}
public Message Read(string name = "Redis_01")
{
if (redis == null) { throw new Exception("請先創(chuàng)建隊列連接"); }
if (string.IsNullOrWhiteSpace(name)) { throw new Exception("name不能為空"); }
var message = new Message();
try
{
message.Label = name;
var result = redis.DequeueItemFromList(name);
if (string.IsNullOrWhiteSpace(result)) { return message; }
message.Body = result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return message;
}
public bool Write(string content, string name = "Redis_01")
{
if (redis == null) { throw new Exception("請先創(chuàng)建隊列連接"); }
if (string.IsNullOrWhiteSpace(content) || string.IsNullOrWhiteSpace(name)) { throw new Exception("content和name不能為空"); }
redis.EnqueueItemOnList(name, content);
return true;
}
public void Dispose()
{
if (redis != null)
{
redis.Dispose();
redis = null;
}
}
//public List<Message> ReadAll()
//{
// throw new NotImplementedException();
//}
}這里用到的Redis的dll是引用了相關(guān)的nuget包:

封裝的隊列Redis工廠流程同樣是:創(chuàng)建(Create)-》讀(Read)|寫(Write)-》釋放(Dispose);有了具體的RedisMQ實現(xiàn)類,然后還需利用工廠模式提供的方法來創(chuàng)建這個類的實例:
/// <summary>
/// ==================
/// author:神牛步行3
/// des:該列工廠開源,包括隊列有MSMQ,RedisMQ,RabbitMQ
/// blogs:http://www.cnblogs.com/wangrudong003/
/// ==================
/// 隊列工廠
/// </summary>
public class QueueReposity<T> where T : class,IQueue, new()
{
public static IQueue Current
{
get
{
return PublicClass.ConfClass<T>.Current;
}
}
}到這兒RedisMQ工廠代碼就完成了,下面開始分享我們的測試用例;
隊列工廠之RedisMQ測試用例
通過上面配置環(huán)境和封裝自己的方法,這里寫了一個簡單的測試用例,分為Server(加入消息隊列)和Client(獲取消息隊列),首先來看Server端的代碼:
/// <summary>
/// 隊列服務(wù)端測試用例
/// </summary>
class Program
{
static void Main(string[] args)
{
Redis_Server();
// RabbitMQ_Server();
//MSMQ_Server();
}
private static void Redis_Server()
{
//實例化QRedisMQ對象
var mq = QueueReposity<QRedisMQ>.Current;
try
{
Console.WriteLine("Server端創(chuàng)建:RedisMQ實例");
mq.Create();
var num = 0;
do
{
Console.WriteLine("輸入循環(huán)數(shù)量(數(shù)字,0表示結(jié)束):");
var readStr = Console.ReadLine();
num = string.IsNullOrWhiteSpace(readStr) ? 0 : Convert.ToInt32(readStr);
Console.WriteLine("插入數(shù)據(jù):");
for (int i = 0; i < num; i++)
{
var str = "我的編號是:" + i;
mq.Write(str);
Console.WriteLine(str);
}
} while (num > 0);
}
catch (Exception ex)
{
}
finally
{
Console.WriteLine("釋放。");
mq.Dispose();
}
Console.ReadLine();
}通過:創(chuàng)建(Create)-》讀(Read)|寫(Write)-》釋放(Dispose) 的流程來使用我們的隊列工廠,此時我們運行下這個Server端,然后分別錄入4次參數(shù):

能看到截圖的文字描述,這些測試數(shù)據(jù)插入到了redis的隊列中,下面我們通過第一節(jié)說的RedisClient工具查看數(shù)據(jù),點擊隊列名稱如:

通過工具能看到我們剛才插入的數(shù)據(jù),然后我們來通過測試用例的client端讀取隊列,具體代碼:
/// <summary>
/// 隊列客戶端測試用例
/// </summary>
class Program
{
static void Main(string[] args)
{
RedisMQ_Client();
// RabbitMQ_Client();
//MSMQ_Client();
}
private static void RedisMQ_Client()
{
//實例化QRedisMQ對象
var mq = QueueReposity<QRedisMQ>.Current;
try
{
Console.WriteLine("Client端創(chuàng)建:RedisMQ實例");
mq.Create();
while (true)
{
try
{
var total = mq.Total();
if (total > 0) { Console.WriteLine("隊列條數(shù):" + total); }
var result = mq.Read();
if (result.Body == null) { continue; }
Console.WriteLine(string.Format("接受隊列{0}:{1}", result.Label, result.Body));
}
catch (Exception ex)
{ Console.WriteLine("異常信息:" + ex.Message); }
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
Console.WriteLine("釋放。");
mq.Dispose();
}
}運行生成的exe,看效果:

通過圖形能看出讀取隊列的數(shù)據(jù)正如我們想的那樣依次讀取,測試用例測試RedisMQ的代碼沒問題;以上對封裝RedisMQ的代碼分享和環(huán)境搭建講解,到這里隊列工廠(MSMQ,RabbitMQ,RedisMQ)就分享完了,希望能給您帶來好的幫助,謝謝閱讀;
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:隊列工廠之RedisMQ-創(chuàng)新互聯(lián)
地址分享:http://www.chinadenli.net/article32/dpcjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、品牌網(wǎng)站制作、虛擬主機(jī)、App開發(fā)、App設(shè)計、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容