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

net.jrouter.id.spring.boot.autoconfigure.IdServiceAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
/*
 * Copyright (C) 2010-2111 [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package net.jrouter.id.spring.boot.autoconfigure;

import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import net.jrouter.id.IdGenerator;
import net.jrouter.id.impl.IdGenerator2018;
import net.jrouter.id.impl.CuratorIdService;
import net.jrouter.id.impl.LocalFileIdService;
import net.jrouter.id.impl.RedisIdService;
import static net.jrouter.id.spring.boot.autoconfigure.GeneratorType.*;
import net.jrouter.id.support.CompositeIdGenerator;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;

/**
 * IdServiceAutoConfiguration.
 */
@Configuration
@EnableConfigurationProperties(IdServiceProperties.class)
@Slf4j
public class IdServiceAutoConfiguration {

    private final IdServiceProperties idServiceProperties;

    public IdServiceAutoConfiguration(IdServiceProperties idServiceProperties) {
        this.idServiceProperties = idServiceProperties;
    }

    @Configuration
    @ConditionalOnProperty(prefix = IdServiceProperties.DISTRIBUTED_ID, name = "generatorType", havingValue = "redis",
            matchIfMissing = true)
    static class RedisKeyConfiguration {

        @Autowired
        private IdServiceProperties idServiceProperties;

        /**
         * Build a RedisTemplate with {@code String} type hash key and {@code Long} type hash value.
         *
         * @param redisConnectionFactory {@code RedisConnectionFactory}
         *
         * @return the RedisTemplate
         *
         * @see StringRedisTemplate
         */
        @Bean
        @ConditionalOnMissingBean(name = "idServiceRedisTemplate")
        public RedisTemplate idServiceRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate template = new RedisTemplate<>();
            RedisSerializer stringSerializer = new StringRedisSerializer();
            template.setDefaultSerializer(stringSerializer);
            template.setKeySerializer(stringSerializer);
            template.setValueSerializer(stringSerializer);
            template.setHashKeySerializer(stringSerializer);
            template.setHashValueSerializer(new GenericToStringSerializer(Long.class));
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }

        @Bean
        @ConditionalOnMissingBean(name = "redisKeyGenerator")
        IdGenerator keyGenerator(RedisTemplate idServiceRedisTemplate) {
            return new RedisIdService(idServiceRedisTemplate, idServiceProperties);
        }
    }

    @Configuration
    @ConditionalOnProperty(prefix = IdServiceProperties.DISTRIBUTED_ID, name = "generatorType",
            havingValue = "zookeeper")
    static class ZkKeyConfiguration {

        @Autowired
        private IdServiceProperties idServiceProperties;

        @Autowired
        private CuratorFramework curatorFramework;

        @Bean
        @ConditionalOnMissingBean(name = "zkKeyGenerator")
        IdGenerator keyGenerator() {
            return new CuratorIdService(curatorFramework, idServiceProperties);
        }
    }

    @Configuration
    @ConditionalOnProperty(prefix = IdServiceProperties.DISTRIBUTED_ID, name = "generatorType",
            havingValue = "local")
    static class LocalKeyConfiguration {

        @Autowired
        private IdServiceProperties idServiceProperties;

        @Bean
        @ConditionalOnMissingBean(name = "localKeyGenerator")
        IdGenerator keyGenerator() {
            return new LocalFileIdService(idServiceProperties);
        }
    }

    @Bean
    IdGenerator idGenerator(IdGenerator keyGenerator) {
        Long workerId = null;
        if (idServiceProperties.getGeneratorType() == LOCAL) {
            workerId = keyGenerator.generateId();
        } else {
            List> keyGenerators = new ArrayList<>(2);
            if (idServiceProperties.isEnableLocalFileStorager()) {
                //add local file file
                keyGenerators.add(new LocalFileIdService(idServiceProperties));
            }
            keyGenerators.add(keyGenerator);
            workerId = new CompositeIdGenerator<>(keyGenerators).generateId();
        }
        Assert.notNull(workerId, String.format("Can't generate key from [%s]", keyGenerator.toString()));
        if (log.isDebugEnabled()) {
            log.debug("Use key [{}] for id generator.");
        }
        return new IdGenerator2018(workerId);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy