如何在Spring Boot中实现Redis多租户

问题描述 投票:0回答:1

我正在使我的整个应用程序成为多租户,但仍停留在redis上。到目前为止,我已经创建了JedisConnectionFactory的地图,并试图将其传递给RedisTemplate,但是它抛出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模板,如下所示:

@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提供了其他方法?

spring spring-boot redis multi-tenant spring-data-redis
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.