分布式自增序列id的實現(xiàn)(二) ---分布式序號生成器

本文在分布式自增序列的實現(xiàn)(一) ---分布式序號生成器基礎(chǔ)上成文集歇,因此直接上解決辦法蝌数,省去問題的討論打毛。請先閱讀分布式自增序列的實現(xiàn)(一) ---分布式序號生成器

上一篇我們提到使用zookeeper的持久化序列node來自動生成分布式序列id嫂丙,本文將討論使用redis的INCR功能實現(xiàn)分布式自增序列的實現(xiàn)。redis是單線程的互例,它能保證生成的序列是不重復(fù)的奢入。

Redis incr功能介紹

官方文檔https://redis.io/commands/incr
Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

Note: this is a string operation because Redis does not have a dedicated integer type. The string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation.

Redis stores integers in their integer representation, so for string values that actually hold an integer, there is no overhead for storing the string representation of the integer.

The counter pattern is the most obvious thing you can do with Redis atomic increment operations.
可以看到最大的序列是64bit的有符號的整型, 最大值是2的63次方-1媳叨, 也就是9,223,372,036,854,775,807腥光,基本上絕大部分項目夠用了。從0開始糊秆。特別是文檔中的這句武福,"incr可以用作計數(shù)器模式,它是原子自增操作痘番。"

具體代碼實現(xiàn)

我直接使用的spring-data-redis捉片,程序基于springboot。完整代碼在這里汞舱,歡迎加星伍纫,fork。 本程序啟動時會自動連接redis, 請先配置好自己的redis服務(wù)器ip和端口等信息.

本示例代碼使用了RedisAtomicLong 昂芜,請參考官方文檔https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/support/atomic/RedisAtomicLong.html 我們主要使用addAndGet方法莹规。

服務(wù)層代碼

package com.yq.service.impl;

import com.yq.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Simple to Introduction
 * className: RedisServiceImpl
 *
 * @author EricYang
 * @version 2018/8/4 23:00
 */
@Service
@Slf4j
public class RedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    RedisAtomicLong redisAtomicLong;

    private static final String LONG_KEY = "yqLong";

    @Bean
    public RedisAtomicLong getRedisAtomicLong() {
        RedisAtomicLong counter = new RedisAtomicLong(LONG_KEY, template.getConnectionFactory());
        return counter;
    }

    @Override
    public String get(String key) {
        ValueOperations<String, String> ops = this.template.opsForValue();
        return ops.get(key);
    }

    @Override
    public void set(String key, String value) {
        ValueOperations<String, String> ops = this.template.opsForValue();
        ops.set(key, value);
    }

    @Override
    public String getHash(String key, String hashKey) {
        HashOperations<String, String, String> hashOps = this.template.opsForHash();
        return hashOps.get(key, hashKey);
    }

    @Override
    public void setHash(String key, String hashKey, String value) {
        HashOperations<String, String, String> hashOps = this.template.opsForHash();
        hashOps.put(key, hashKey, value);
    }

    @Override
    public long getRedisSequence() {
        long sequence = 0L;
        try {
            if (redisAtomicLong.get() == 0) {
                redisAtomicLong.getAndSet(0L);
            }
            sequence = redisAtomicLong.incrementAndGet();
        } catch (Exception ex) {
            log.error("Failed to get sequence.", ex);
        }
        return sequence;
    }
}

controller層代碼, 供其他服務(wù)調(diào)用

@RestController
@RequestMapping("/cache")
public class RedisController {
    private Logger logger = LoggerFactory.getLogger(RedisController.class);

   @Autowired
   RedisService redisService;

    @ApiOperation(value = "獲取sequence")
    @GetMapping(value = "/sequence", produces = "application/json;charset=UTF-8")
    public long getSequence() {
        long value = redisService.getRedisSequence();
        return value;
    }
    。泌神。良漱。

效果截圖

getSequence.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市欢际,隨后出現(xiàn)的幾起案子母市,更是在濱河造成了極大的恐慌,老刑警劉巖损趋,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件患久,死亡現(xiàn)場離奇詭異,居然都是意外死亡浑槽,警方通過查閱死者的電腦和手機蒋失,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來括荡,“玉大人,你說我怎么就攤上這事溉旋』澹” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長邑闲。 經(jīng)常有香客問我算行,道長,這世上最難降的妖魔是什么苫耸? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任州邢,我火速辦了婚禮,結(jié)果婚禮上褪子,老公的妹妹穿的比我還像新娘量淌。我一直安慰自己,他們只是感情好嫌褪,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布呀枢。 她就那樣靜靜地躺著,像睡著了一般笼痛。 火紅的嫁衣襯著肌膚如雪裙秋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天缨伊,我揣著相機與錄音摘刑,去河邊找鬼。 笑死刻坊,一個胖子當(dāng)著我的面吹牛枷恕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播紧唱,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼活尊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了漏益?” 一聲冷哼從身側(cè)響起蛹锰,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绰疤,沒想到半個月后铜犬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡轻庆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年癣猾,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片余爆。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡纷宇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蛾方,到底是詐尸還是另有隱情像捶,我是刑警寧澤上陕,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站拓春,受9級特大地震影響释簿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜硼莽,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一庶溶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧懂鸵,春花似錦偏螺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至殴穴,卻和暖如春凉夯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背采幌。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工劲够, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人休傍。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓征绎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親磨取。 傳聞我的和親對象是個殘疾皇子人柿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,451評論 0 13
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)忙厌,斷路器凫岖,智...
    卡卡羅2017閱讀 134,654評論 18 139
  • 1 Redis介紹1.1 什么是NoSql為了解決高并發(fā)哥放、高可擴展、高可用爹土、大數(shù)據(jù)存儲問題而產(chǎn)生的數(shù)據(jù)庫解決方...
    克魯?shù)吕?/span>閱讀 5,291評論 0 36
  • 文/賴佳秀 冬姐姐穿著雪白的衣服悄悄地走了甥雕,春妹妹來了,她給故鄉(xiāng)穿上了五顏六色的新衣胀茵,大地美起來了社露,香起來...
    我愛無花果閱讀 622評論 0 0
  • 無意間看到張小嫻寫的:女人需要三樣?xùn)|西 1.男人 2.愛情 3.安全感 我終于知道所有的痛苦的癥結(jié)...
    竹筠益閱讀 335評論 0 1