How dongxl123/butterfly works
Overall Project Type and Purpose
• The project is a library/framework intended for generating globally unique IDs in high-concurrency environments. It provides multiple implementations that cater to various deployment scenarios:
• Single machine id allocation using either database or ZooKeeper.
• Distributed mode with both Dubbo and Restful service implementations.
• The README outlines a few “发号器” issues (time rollback, machine id allocation/reclaim, and worker id limits) and describes innovative solutions, which are key to understanding the architecture.
File Structure
• The project is split into distinct modules:
• butterfly-allocator-db: Handles the DB-based worker id allocation mechanism.
• butterfly-allocator-zookeeper: Implements the ZooKeeper based solution.
• butterfly-allocator-distribute: Provides the distributed mode logic.
• butterfly-distribute: Contains server components using both Dubbo and Restful interfaces as well as the butterfly-api definitions for distributed access.
• butterfly-sample: Provides sample usages and tests for various allocation modes.
• butterfly-sequence: Contains the core ID generation logic (improved snowflake algorithm and sequence logic).
• The directories clearly separate implementations along the lines of allocation mechanisms and service interfacing. Notice the use of sub-packages like “worker”, “sequence”, “allocator”, “entity” and “config” that reveal an organized layered structure.