How to use MongoDB+Springboot to implement distributed ID?

forward from:

http://blog.csdn.net/forezp/article/details/69056017 This article is from Fang Zhipeng's blog 

1. Background

How to implement distributed id and search for related information, generally give these solutions:

  • Use database auto-increment Id
  • Use the incr command of reids
  • Using UUIDs
  • Twitter's snowflake algorithm
  • Use zookeeper to generate unique ID
  • MongoDB的ObjectId

In addition, I found by crawling Zhihu user id, Zhihu's user id is 32-bit, and it is preliminarily concluded that Zhihu uses md5 encryption, and then all is converted to lowercase. As for how to crawl Zhihu user information, see the article I shared earlier. The technical solution adopted in this article adopts the objectId of mogoodb.

2. How does mongodb implement distributed ID

MongoDB 's ObjectId is designed to be lightweight, and different machines can easily generate it in the same way that is globally unique. MongoDB  was designed from the ground up to be a distributed database , and handling multiple nodes is a core requirement. Makes it much easier to spawn in a sharded environment.

Its format:



 

  • The first 4 bytes are the timestamp from the standard epoch in seconds. The timestamp, combined with the following 5 bytes, provides second-level uniqueness. Since the timestamp comes first, this means that the ObjectIds will be roughly in the order they were inserted. This is useful for things like using it as an index to improve efficiency. These 4 bytes also imply when the document was created. Most client-side libraries expose a method to get this information from the ObjectId.

  • The next 3 bytes are the unique identifier of the host. Usually a hash of the machine's hostname. This ensures that different hosts generate different ObjectIds without conflict. 
    To ensure that the ObjectId generated by multiple concurrent processes on the same machine is unique, the next two bytes come from the process identifier (PID) that generated the ObjectId.

  • The first 9 bytes ensure that the ObjectId generated by different processes on different machines in the same second is unique.
  • The last 3 bytes are an automatically incremented counter to ensure that the ObjectId generated by the same process in the same second is also different. A maximum of 2563 (16 777 216) different ObjectIds are allowed per process in the same second.

3. Coding

Introduce mongodb in springboot:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- open web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


       <!--mongodb -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

 Create an entity class:

public class Customer {

    @Id
    public String id;

    public String firstName;
    public String lastName;

    public Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%s, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

 Create the mongodb interface class:

/**
 * Created by fangzhipeng on 2017/4/1.
 */


public interface CustomerRepository extends MongoRepository<Customer, String> {

    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);

}

 Test class:

@Autowired
    CustomerRepository customerRepository;


@Test
public void mongodbIdTest(){
Customer customer=new Customer("lxdxil","dd");
        customer=customerRepository.save(customer);
        logger.info( "mongodbId:"+customer.getId());
}

4. References

Accessing Data with MongoDB

The ObjectId of MongoDB

MongoDB Tutorial

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327004690&siteId=291194637