DistributedIDService.java 773 Bytes
Newer Older
1 2 3
package pwc.taxtech.atms.service.impl;

import org.springframework.beans.factory.annotation.Value;
4
import org.springframework.stereotype.Component;
5 6 7 8 9
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.common.util.SnowFlake;

import javax.annotation.PostConstruct;

10
@Component
11
public class DistributedIdService extends BaseService {
12 13 14 15 16 17 18 19 20 21 22 23 24
    @Value("${distributed_id_datacenter}")
    private Integer dataCenterId;
    @Value("${distributed_id_machine}")
    private Integer machineId;

    private SnowFlake snowFlake;

    @PostConstruct
    public void init() {
        snowFlake = new SnowFlake(dataCenterId, machineId);
    }

    /**
25
     * 获取Id
26 27 28 29 30 31 32
     *
     * @return long
     */
    public long nextId() {
        return snowFlake.nextId();
    }
}