• 用python的zerorpc写一个生成雪花ID的rpc服务


    一、

    最近在研究分布式系统,将部分服务拆分出去,只会python,所以用python的zerorpc写服务
    
    • 1

    二、代码

    整个都是从pysnowflake里面复制出来的,pysnowflake可以启动一个http服务,我以前一直都用这个服务的。这里只是将http服务变成rpc服务

    import time
    from loguru import logger
    
    EPOCH_TIMESTAMP = 550281600000
    
    
    class Generator(object):
        def __init__(self):
            self.dc = 1
            self.worker = 1
            self.node_id = ((self.dc & 0x03) << 8) | (self.worker & 0xff)
            self.last_timestamp = EPOCH_TIMESTAMP
            self.sequence = 0
            self.sequence_overload = 0
            self.errors = 0
            self.generated_ids = 0
    
        def get_next_id(self) -> int:
            curr_time = int(time.time() * 1000)
    
            if curr_time < self.last_timestamp:
                # stop handling requests til we've caught back up
                self.errors += 1
                raise EnvironmentError("服务器时间不对,当前时间低于最后一次请求时间")
    
            if curr_time > self.last_timestamp:
                self.sequence = 0
                self.last_timestamp = curr_time
    
            self.sequence += 1
    
            if self.sequence > 4095:
                # the sequence is overload, just wait to next sequence
                logger.warning('The sequence has been overload')
                self.sequence_overload += 1
                time.sleep(0.001)
                return self.get_next_id()
    
            generated_id = ((curr_time - EPOCH_TIMESTAMP) << 22) | (self.node_id << 12) | self.sequence
    
            self.generated_ids += 1
            return generated_id
    
        @property
        def stats(self):
            return {
                'dc': self.dc,
                'worker': self.worker,
                'timestamp': int(time.time() * 1000),  # current timestamp for this worker
                'last_timestamp': self.last_timestamp,  # the last timestamp that generated ID on
                'sequence': self.sequence,  # the sequence number for last timestamp
                'sequence_overload': self.sequence_overload,  # the number of times that the sequence is overflow
                'errors': self.errors,  # the number of times that clock went backward
            }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    import zerorpc
    from loguru import logger
    from 雪花算法服务.算法 import Generator
    
    
    class HandleModel():
        generator = Generator()
    
        def getID(self):
            logger.info("收到一次RPC请求,返回id")
            return str(self.generator.get_next_id())
    
        def get_status(self):
            logger.info("收到一次RPC请求,服务状态")
            return self.generator.stats
    
    
    if __name__ == '__main__':
        logger.info("准备开启rpc服务")
        s = zerorpc.Server(HandleModel())
        s.bind(["tcp://0.0.0.0:2441","tcp://0.0.0.0:2442"])
        logger.info(f"开启服务,端口是{2441}")
        s.run()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    结束

  • 相关阅读:
    在分布式系统中,库存超卖怎么办?
    Linux基础命令4——Linux快捷键与帮助命令
    DiffKit -- 世上最牛且开源的表数据对比工具
    无胁科技-TVD每日漏洞情报-2022-10-26
    如何评估RPA需求?
    力扣labuladong——一刷day42
    OWASP Top 10漏洞解析(1)- A1:Broken Access Control 访问控制失效
    深入理解Java IO流(第一篇)
    [go]文件与目录操作
    卷积神经网络应用实例,卷积神经网络实际应用
  • 原文地址:https://blog.csdn.net/qq_43656607/article/details/133947752