【发布时间】:2019-01-03 20:20:25
【问题描述】:
我使用的是 Spring Data Redis,Spring 数据抽象不直接使用 RedisTemplate。
我的数据模型如下:
@RedisHash(value = “products")
public class Product {
@Id
@Indexed
private String id;
private String description;
private BigDecimal price;
private String imageUrl;
//Getter and Setter
}
我的带有 Spring 数据抽象的存储库:
@Repository
public interface ProductRepository extends CrudRepository<Product,String> {
}
这是我的配置:
@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
jedisConFactory.setHostName("localhost");
jedisConFactory.setPort(6379);
return jedisConFactory;
}
}
对于单租户应用程序,我对这些感到满意。
现在我想实现一个多租户结构。
我认为为每个租户创建一个 Redis 实例是一个很好的解决方案。
我有一个包含租户 ID 和专用于该租户的 Redis 端点的地图。
地图数据如下:
(Key : tenantId1, value: host1:port1)
(Key : tenantId2, value: host2:port2)
(Key : tenantId3, value: host3:port3)
我心中的情景:
一个租户带着它的租户 id 来到应用程序,并用租户 id 将请求传递给 redisRepository。
保存新产品示例:productRepository.save(product,tenantId).
但无法想象如何实现这个路由。
我想为每个租户创建一个 RedisConnectionFactory。
但是不知道Spring数据抽象上如何选择相关的connectionFactory。
有人可以帮帮我吗?
【问题讨论】:
标签: spring spring-boot redis multi-tenant spring-data-redis