如何在spring boot中实现redis的多租户 [英] How to implement multitenancy for redis in spring boot

查看:397
本文介绍了如何在spring boot中实现redis的多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使我的整个应用程序成为多租户的,但仍然停留在 redis 上.到目前为止,我创建了 JedisConnectionFactory 的地图并尝试将其传递给 RedisTemplate 但它抛出 java.lang.IllegalArgumentException: template not initialized;在使用之前调用 afterPropertiesSet().

I am working on making my whole application multi-tenanted but stuck on redis. So far I created a map of JedisConnectionFactory and tried to pass it to RedisTemplate but it throwing java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using it.

以下是代码片段:

@Component
public class RedisConfiguration {

    @Autowired
    private DSConfig dsConfig;

    private Map<String,JedisConnectionFactory> jedisConnectionFactoryMap = new HashMap<>();

    private static Logger LOGGER = LoggerFactory.getLogger(RedisConfiguration.class);

    @PostConstruct
    public void initializeJedisConnectionFactories() {

        for(DatasourceDetail datasourceDetail : dsConfig.getDatasources()) {
            RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
            redisConfig.setHostName(datasourceDetail.getRedisHost());
            redisConfig.setPassword(RedisPassword.of(datasourceDetail.getRedisPassword()));
            redisConfig.setPort(Integer.parseInt(datasourceDetail.getRedisPort()));
            JedisClientConfiguration configuration = JedisClientConfiguration.builder().usePooling().
                    poolConfig(new JedisPoolConfig()).build();
            jedisConnectionFactoryMap.put(datasourceDetail.getTenantId()
                    ,new JedisConnectionFactory(redisConfig,configuration));
        }
        LOGGER.info("Connection factory count " + jedisConnectionFactoryMap.size());
    }

    public RedisTemplate< String, Object > redisTemplate() throws Exception {
        final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();
        template.setConnectionFactory( jedisConnectionFactory() );
        template.setKeySerializer( new StringRedisSerializer() );
        template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        return template;
    }

    JedisConnectionFactory jedisConnectionFactory() throws Exception {
        if(TenantContext.getCurrentTenant()==null) {
            throw new Exception("No tenant context found");
        }
        LOGGER.info("Returning redis connection for tenant" + TenantContext.getCurrentTenant());
        return jedisConnectionFactoryMap.get(TenantContext.getCurrentTenant());
    }
}

我正在使用 redis 模板,如下所示:

And I am using redis template as below:

@CrossOrigin("*")
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisConfiguration redisConfiguration;


    @GetMapping("/set")
    @ResponseBody
    public Object set(@RequestParam(value = "key", required = true) String key,
                      @RequestParam(value = "value", required = true) String value) throws Exception {
        redisConfiguration.redisTemplate().opsForValue().set( key,value );
        return true;
    }

    @GetMapping("/get")
    @ResponseBody
    public Object get(@RequestParam(value = "key", required = true) String key) throws Exception {
        redisConfiguration.redisTemplate().opsForValue().get(key);
        return true;
    }
}

无论如何我可以以更好的方式实现这一点,或者spring redis提供了其他任何方式吗?

Is there anyway I can implement this in a better way or is there any other way which spring redis provides?

推荐答案

最后我能够通过显式调用 afterPropertiesSet() 来解决.

Finally I was able to resolve by calling afterPropertiesSet() explicitly.

这篇关于如何在spring boot中实现redis的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆