新聞中心
本文轉(zhuǎn)載自微信公眾號(hào)「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請(qǐng)聯(lián)系UP技術(shù)控公眾號(hào)。

為舒蘭等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及舒蘭網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、舒蘭網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
一個(gè)軟件系統(tǒng),肯定到處充滿著各種單據(jù),也必然需要有各種單據(jù)號(hào)與之對(duì)應(yīng)。比如:電商行業(yè)的訂單號(hào)、支付流水號(hào)、退款單號(hào)等等。SCM的采購單號(hào)、進(jìn)貨單號(hào)、出貨單號(hào)、盤點(diǎn)單號(hào)等。在一個(gè)企業(yè)內(nèi)部或者一個(gè)2C的平臺(tái),無法避免的需要通過某個(gè)單據(jù)號(hào)來進(jìn)行溝通。所以一個(gè)好的單據(jù)號(hào)必然是便于溝通的。那么如何創(chuàng)建全局唯一的單據(jù)號(hào)。
1、單系統(tǒng)使用object
- ///
- /// 單號(hào)助手
- ///
- public class BillNoHelper
- {
- ///
- /// 防止創(chuàng)建類的實(shí)例
- ///
- private BillNoHelper() { }
- private static readonly object locker = new object();
- private static int _sn = 0;
- ///
- /// 生成單據(jù)編號(hào)
- ///
- ///
- public static string GenerateId()
- {
- lock (locker) //lock 關(guān)鍵字可確保當(dāng)一個(gè)線程位于代碼的臨界區(qū)時(shí),另一個(gè)線程不會(huì)進(jìn)入該臨界區(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分布式鎖
在同一時(shí)間只會(huì)有一個(gè)客戶端能獲取到鎖,其它客戶端無法同時(shí)獲取,這把鎖在一段有限的時(shí)間之后,一定會(huì)被釋放(正常釋放或異常釋放)。
標(biāo)題名稱:高并發(fā)下怎樣生成唯一的訂單號(hào)?
本文路徑:http://fisionsoft.com.cn/article/ccdojhj.html


咨詢
建站咨詢
