Giter Site home page Giter Site logo

magic-starter-id-demo's Introduction

magic-starter-id-demo

演示分布式主键生成模块的使用

配置

雪花算法

引入依赖之后,在配置文件配置 worker-iddata-center-id

magic:
  id:
    snowflake:
      worker-id: 1
      data-center-id: 1

在代码中,直接注入即可

@Autowired
private Id snowflakeId;

当然,也可以通过自定义 Bean 的形式配置

@Bean
public Id snowflakeId() {
    return SnowflakeIdFactory.create().dataCenterId(1L).workerId(1L).prefix(new DefaultPrefix("2019-")).getInstance();
}

数据库步长

在配置文件配置表信息步长重试次数、以及起始 ID

magic:
  id:
    database:
      step: 1
      retry-times: 3
      table-name: id_test

在配置类中配置

@Bean
public Id databaseId2(DataSource dataSource, IdDatabaseProperties databaseProperties) {
    return DatabaseIdFactory
      .create()
      .businessName(() -> String.format("test_db_%s", DateUtil.today()))
      .prefix(() -> "2019-")
      .dataSource(dataSource)
      .step(databaseProperties.getStep())
      .stepStart(databaseProperties.getStepStart())
      .retryTimes(databaseProperties.getRetryTimes())
      .tableName(databaseProperties.getTableName())
      .getInstance();
}

Redis 步长

在配置文件配置 步长起始 ID

magic:
  id:
    redis:
      step: 1
      step-start: 0

在配置类中配置

@Bean
public Id redisId(IdRedisProperties redisProperties) {
    return RedisIdFactory
            .create()
            .businessName(() -> String.format("test_redis_%s", DateUtil.today()))
            .prefix(() -> "2019-")
            .ip("localhost")
            .port(6379)
            .step(redisProperties.getStep())
            .stepStart(redisProperties.getStepStart())
            .getInstance();
}

使用

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class IdTests {

    @Autowired
    private Id snowflakeId;
    @Autowired
    private Id databaseId1;
    @Autowired
    private Id databaseId2;
    @Autowired
    private Id redisId;

    @Test
    public void testSnowflake() {
        log.info("【snowflakeId】= {}", snowflakeId.nextId());
        log.info("【snowflakeId】= {}", snowflakeId.nextIdStr());

        log.info("【snowflakeId】= {}", snowflakeId.nextId(new DateBusinessName()));
        log.info("【snowflakeId】= {}", snowflakeId.nextIdStr(new DateBusinessName(), () -> "自定义"));
    }

    @Test
    public void testDatabase() {
        log.info("【databaseId1】= {}", databaseId1.nextId());
        log.info("【databaseId1】= {}", databaseId1.nextIdStr());

        log.info("【databaseId1】= {}", databaseId1.nextId(() -> "自定义模块-1"));
        log.info("【databaseId1】= {}", databaseId1.nextIdStr(() -> "自定义模块-1", () -> "自定义前缀-1-"));

        log.info("【databaseId2】= {}", databaseId2.nextId());
        log.info("【databaseId2】= {}", databaseId2.nextIdStr());
    }

    @Test
    public void testRedis() {
        log.info("【redisId】= {}", redisId.nextId());
        log.info("【redisId】= {}", redisId.nextIdStr());

        log.info("【redisId】= {}", redisId.nextId(() -> "自定义模块"));
        log.info("【redisId】= {}", redisId.nextIdStr(() -> "自定义模块", () -> "自定义前缀-"));
    }
}

magic-starter-id-demo's People

Contributors

xkcoding avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.