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

org.zodiac.autoconfigure.redis.RedissonAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.redis;

import static java.util.Collections.emptyMap;

import java.util.Map;

import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;
import org.zodiac.autoconfigure.redis.condition.ConditionalOnRedisEnabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.redis.redisson.RedissonClientFactory;

@SpringBootApplication
@ConditionalOnRedisEnabled
@EnableConfigurationProperties(value = {RedissonClientsInfoProperties.class})
@Import(value = {RedissonAutoConfiguration.RedisClientRegistrar.class})
@ConditionalOnProperty(name = SystemPropertiesConstants.Zodiac.SPRING_REDISSON_ENABLED, havingValue = "true")
public class RedissonAutoConfiguration {

    private static Logger log = LoggerFactory.getLogger(RedissonAutoConfiguration.class);

    private static final String DEFAULT_REDIS_NAME = "default";
    private static final String REDISSON_NAME = "redissonClient";
    private static final String REDISSON_NAME_SUFFIX = "RedissonClient";

    /**
     * 规格化数据源名称,数据源名称一定是以DataSource为后缀的。
     *
     * @param name 数据源名称
     * @return 数据源名称
     */
    private static String normalizeDataSourceName(String name) {
        if (StringUtils.isEmpty(name) || REDISSON_NAME.equals(name)) {
            return REDISSON_NAME;
        }

        if (name.endsWith(REDISSON_NAME_SUFFIX)) {
            return name;
        }

        return name + REDISSON_NAME_SUFFIX;
    }

    /**
     * 注册客户端。
     *
     */
    static class RedisClientRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {

        private Map clients;

        @Override
        public void setEnvironment(Environment environment) {
            this.clients = Binder.get(environment)
                .bind(SystemPropertiesConstants.Zodiac.SPRING_REDISSON_CLIENTS, Bindable.mapOf(String.class, Object.class))
                .orElse(emptyMap());
        }

        @Override
        public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
            clients.keySet().forEach(key -> {
                // 注册客户端工厂Bean
                String clientBeanName = normalizeDataSourceName(key);
                String factoryBeanName = clientBeanName + "Factory";
                BeanDefinitionBuilder factoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(RedissonClientFactory.class)
                    .addConstructorArgValue(key);
                registry.registerBeanDefinition(factoryBeanName, factoryBuilder.getBeanDefinition());

                // 选择合适的客户端类型
                Class clientClass = RedissonClient.class;
                String factoryMethodName = "createClient";
//                if (clients.get(key) instanceof Map) {
//                    Map props = (Map) clients.get(key);
//                    if (props.containsKey("type") && "REACTIVE".equalsIgnoreCase(props.get("type").toString())) {
//                        clientClass = RedissonReactiveClient.class;
//                        factoryMethodName = "createRedissonReactiveClient";
//                    } else if (props.containsKey("type") && "RX".equalsIgnoreCase(props.get("type").toString())) {
//                        clientClass = RedissonRxClient.class;
//                        factoryMethodName = "RX";
//                    }
//                }

                // 注册客户端Bean
                BeanDefinitionBuilder clientBuilder = BeanDefinitionBuilder.genericBeanDefinition(clientClass)
                    .setFactoryMethodOnBean(factoryMethodName, factoryBeanName)
                    .setDestroyMethodName("shutdown");
                registry.registerBeanDefinition(clientBeanName, clientBuilder.getBeanDefinition());
                if (DEFAULT_REDIS_NAME.equals(key)) {
                    registry.registerAlias(clientBeanName, REDISSON_NAME);
                }

                log.info("Load redisson client: {}", clientBeanName);
            });
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy