新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
高并發(fā)下怎樣生成唯一的訂單號?
本文轉載自微信公眾號「UP技術控」,作者conan5566。轉載本文請聯(lián)系UP技術控公眾號。

為舒蘭等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及舒蘭網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設計、成都做網(wǎng)站、舒蘭網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
一個軟件系統(tǒng),肯定到處充滿著各種單據(jù),也必然需要有各種單據(jù)號與之對應。比如:電商行業(yè)的訂單號、支付流水號、退款單號等等。SCM的采購單號、進貨單號、出貨單號、盤點單號等。在一個企業(yè)內(nèi)部或者一個2C的平臺,無法避免的需要通過某個單據(jù)號來進行溝通。所以一個好的單據(jù)號必然是便于溝通的。那么如何創(chuàng)建全局唯一的單據(jù)號。
1、單系統(tǒng)使用object
- ///
- /// 單號助手
- ///
- public class BillNoHelper
- {
- ///
- /// 防止創(chuàng)建類的實例
- ///
- private BillNoHelper() { }
- private static readonly object locker = new object();
- private static int _sn = 0;
- ///
- /// 生成單據(jù)編號
- ///
- ///
- public static string GenerateId()
- {
- lock (locker) //lock 關鍵字可確保當一個線程位于代碼的臨界區(qū)時,另一個線程不會進入該臨界區(qū)。
- {
- if (_sn == 9999)
- {
- _sn = 1;
- }
- else
- {
- _sn++;
- }
- return DateTime.Now.ToString("yyMMddHHmmss") + _sn.ToString().PadLeft(4, '0');
- }
- }
- ///
- /// 唯一值
- ///
- ///
- public static string guid()
- {
- return Guid.NewGuid().ToString().Replace("-", "");
- }
- ///
- /// GuidExtension
- ///
- ///
- ///
- public static string IsNum(Guid guid)
- {
- var s = guid.ToString();
- var sb = new StringBuilder();
- for (int i = 0; i < s.Length; i++)
- {
- if (Char.IsNumber(s, i))
- {
- sb.Append(s.Substring(i, 1));
- }
- }
- return sb.ToString();
- }
- #region 生成 Coupon Key
- private static readonly object locker2 = new object();
- private static int _sn2 = 0;
- ///
- /// 生成key 12位 字母+數(shù)字
- ///
- ///
- public static string GenerateCouponKey()//
- {
- string s;
- lock (locker2)
- {
- if (_sn2 == 9999999)
- {
- _sn2 = 1;
- }
- else
- {
- _sn2++;
- }
- s = $"Qq@,.*745u787#$@,.><;'{DateTime.Now.Ticks}{_sn2}";
- }
- return MD5_16(s);
- }
- private static string MD5_16(string p_str)
- {
- System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] data = System.Text.Encoding.Default.GetBytes(p_str);
- byte[] md5Data = md5.ComputeHash(data);
- string strResult = System.BitConverter.ToString(md5Data, 4, 8);
- md5.Clear();
- strResult = strResult.Replace("-", "").ToUpper();
- return strResult;
- }
- #endregion
- }
2、數(shù)據(jù)唯一索引
利用數(shù)據(jù)庫索引的特性生成唯一id.
3、使用redis分布式鎖
在同一時間只會有一個客戶端能獲取到鎖,其它客戶端無法同時獲取,這把鎖在一段有限的時間之后,一定會被釋放(正常釋放或異常釋放)。
名稱欄目:高并發(fā)下怎樣生成唯一的訂單號?
網(wǎng)址分享:http://www.5511xx.com/article/ccdojhj.html


咨詢
建站咨詢
