All Downloads are FREE. Search and download functionalities are using the official Maven repository.

im.shs.tick.sequence.SequenceAutoConfiguration Maven / Gradle / Ivy

package im.shs.tick.sequence;

import im.shs.tick.sequence.builder.DbSeqBuilder;
import im.shs.tick.sequence.builder.RedisSeqBuilder;
import im.shs.tick.sequence.builder.SnowflakeSeqBuilder;
import im.shs.tick.sequence.properties.SequenceDbProperties;
import im.shs.tick.sequence.properties.SequenceRedisProperties;
import im.shs.tick.sequence.properties.SequenceSnowflakeProperties;
import im.shs.tick.sequence.range.impl.name.DateBizName;
import im.shs.tick.sequence.range.impl.name.DefaultBizName;
import im.shs.tick.sequence.seq.Sequence;
import im.shs.tick.core.props.TickProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan("im.shs.tick.sequence")
public class SequenceAutoConfiguration {

	/**
	 * 数据库作为发号器的存储介质
	 *
	 * @param dataSource
	 * @param properties
	 * @return
	 */
	@Bean
	@ConditionalOnBean(SequenceDbProperties.class)
	@ConditionalOnMissingBean(name = "dbSequence")
	public Sequence dbSequence(DataSource dataSource, SequenceDbProperties properties) {
		return DbSeqBuilder
				.create()
				.bizName(new DefaultBizName(properties.getBizName()))
				.dataSource(dataSource)
				.step(properties.getStep())
				.retryTimes(properties.getRetryTimes())
				.tableName(properties.getTableName())
				.build();
	}

	/**
	 * Redis 作为发号器的存储介质
	 *
	 * @param redisProperties
	 * @param properties
	 * @return
	 */
	@Bean
	@ConditionalOnBean(SequenceRedisProperties.class)
	@ConditionalOnMissingBean(name = "redisSequence")
	public Sequence redisSequence(RedisProperties redisProperties, TickProperties tickProperties, SequenceRedisProperties properties) {
		return RedisSeqBuilder
				.create()
				.global(tickProperties)
				.bizName(new DateBizName(properties.getBizName()))
				.ip(redisProperties.getHost())
				.port(redisProperties.getPort())
				.auth(redisProperties.getPassword())
				.step(properties.getStep())
				.build();
	}

	/**
	 * snowflake 算法作为发号器实现
	 *
	 * @param properties
	 * @return
	 */
	@Bean
	@ConditionalOnBean(SequenceSnowflakeProperties.class)
	@ConditionalOnMissingBean(name = "snowflakeSequence")
	public Sequence snowflakeSequence(SequenceSnowflakeProperties properties) {
		return SnowflakeSeqBuilder
				.create()
				.datacenterId(properties.getDatacenterId())
				.workerId(properties.getWorkerId())
				.build();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy