tinyid

repository·master·Indexed 25 days ago

https://github.com/didi/tinyid

A high-performance ID Generator Service providing unique identifiers via a REST API or a specialized Java client. Optimized for high throughput, it supports over 10 million QPS per instance when using the Java client. Features include batch ID retrieval and customizable increment logic via bizType patterns.

Tokens
880
Snippets
3
Records
4
Agent score
31%

What's inside tinyid

  1. Integrate the Tinyid Java Client

    master

    The Java client is the recommended way to use Tinyid, capable of over 10 million QPS per instance.

    1. Add Maven Dependency: Add tinyid-client to your pom.xml.
    2. Configure Client Properties: Create a file named tinyid_client.properties and place it in your classpath. It must contain the tinyid.server address and your tinyid.token.
    3. Generate IDs in Code: Use the TinyId class to call nextId(bizType) for a single ID or nextId(bizType, batchSize) for a list of IDs.
    <!-- Maven Dependency -->
    <dependency>
        <groupId>com.xiaoju.uemc.tinyid</groupId>
        <artifactId>tinyid-client</artifactId>
        <version>${tinyid.version}</version>
    </dependency>
    
    <!-- tinyid_client.properties -->
    tinyid.server=localhost:9999
    tinyid.token=0f673adf80504e2eaa552f5d791b644c
    
    <!-- Java Usage -->
    Long id = TinyId.nextId("test");
    List<Long> ids = TinyId.nextId("test", 10);
  2. Set up and start the Tinyid server

    master

    To run the Tinyid ID Generator Service, follow these steps:

    1. Clone the repository: git clone https://github.com/didi/tinyid.git.
    2. Initialize the database: Navigate to tinyid/tinyid-server/ and execute the db.sql script in your MySQL instance to create the required tables.
    3. Configure the database: Edit tinyid-server/src/main/resources/offline/application.properties to set your datasource.tinyid.primary connection details (driver, URL, username, and password).
    4. Build and Run:
      • Navigate to tinyid-server/.
      • Run the build script: sh build.sh offline.
      • Start the server using the generated JAR file: java -jar output/tinyid-server-xxx.jar (replace xxx with the actual version/filename).
    cd tinyid-server/
    sh build.sh offline
    java -jar output/tinyid-server-xxx.jar
  3. Configure the Tinyid server database

    master

    The Tinyid server uses application.properties located in tinyid-server/src/main/resources/offline/ to manage database connections. You must define the datasource.tinyid.primary properties to connect to your MySQL instance.

    datasource.tinyid.names=primary
    
    datasource.tinyid.primary.driver-class-name=com.mysql.jdbc.Driver
    datasource.tinyid.primary.url=jdbc:mysql://ip:port/databaseName?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
    datasource.tinyid.primary.username=root
    datasource.tinyid.primary.password=123456
  4. Use the Tinyid REST API

    master

    Tinyid provides several REST endpoints to retrieve IDs. All requests require a bizType and a token.

    • nextId: Returns a JSON response containing the ID.
    • nextIdSimple: Returns the ID as a plain text/raw value.
    • nextIdSimple with batchSize: Returns a comma-separated list of IDs.
    • Custom Step/Delta: By using specific bizType patterns (e.g., test_odd), you can control the increment logic (e.g., delta of 2).