Spring Data Redis多租户 [英] Spring Data Redis with multi tenancy
问题描述
我正在使用Spring Data Redis,Spring数据抽象不直接使用RedisTemplate.
I am using Spring Data Redis, with spring data abstraction not using directly 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数据抽象的存储库:
My repositories with spring data abstraction:
@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;
}
}
我对单个租户申请感到满意.
I am happy with these for a single tenant application.
现在我想实现一个多租户结构.
And now I want to implement a Multi-tenant structure.
我认为为每个租户创建一个Redis实例是一个很好的解决方案.
I thought that create a Redis instance for each tenant is a good solution.
我有一个包含租户ID和该租户专用的Redis端点的地图.
I have a map that contains tenant id and Redis endpoint which is dedicated to this tenant.
地图数据如下所示:
(Key : tenantId1, value: host1:port1)
(Key : tenantId2, value: host2:port2)
(Key : tenantId3, value: host3:port3)
我想到的情况:
租户以其租户ID进入应用程序,并将请求传递给带有租户ID的redisRepository.
保存新产品的示例:productRepository.save(product,tenantId)
.
The scenario in my mind:
a tenant comes to the application with its tenant id and pass the request to redisRepository with tenant id.
Example for save new product : productRepository.save(product,tenantId)
.
但无法想象如何实现此路由.
But can't imagine how to implement this routing.
我认为为每个租户创建一个RedisConnectionFactory.
I thought that create a RedisConnectionFactory for each tenant.
但是我不知道如何在Spring数据抽象上选择相关的connectionFactory.
But I don’t know how to select related connectionFactory on Spring data abstraction.
有人可以帮我吗?
推荐答案
要实现多租户结构,您可以按以下步骤操作:
in order to implement a multitenant structure you can proceed as below :
@Bean(name = "JedisConnectionFactor" )
public Map<String, JedisConnectionFactory> JedisConnectionFactor(){
Map<String, DataSource> factories = new HashMap<>();
//for (JedisProperties properties : // better to import jedis config from for each single tenant
//multiTenantJedisProperties.getListOfProperties()) {
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
jedisConFactory.setHostName("localhost");
jedisConFactory.setPort(6379);
result.put(jeditProperties.getTenantId(), jedisConFactory)
// iterate ....
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
jedisConFactory.setHostName("localhost");
jedisConFactory.setPort(6379);
result.put(jeditProperties.getTenantId(), jedisConFactory);
// n times
}
return factories;
}
您可以使用自动装配工厂地图
and you can use autowire the factories map
@Autowired
private Map<String, JedisConnectionFactory> JedisConnectionFactories;
@Autowire
protected DataSource selectJedisConnectionFactory(String tenantId) {
return this.JedisConnectionFactories.get(tenantId);
}
我希望这会有所帮助!
这篇关于Spring Data Redis多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!