How to implement multitenancy for redis in spring boot

馋奶兔 提交于 2021-01-01 10:04:32

问题


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.

Below are code snippets:

@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());
    }
}

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;
    }
}

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


回答1:


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



来源:https://stackoverflow.com/questions/59661914/how-to-implement-multitenancy-for-redis-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!