【问题标题】:Spring Data Redis with multi tenancy具有多租户的 Spring Data Redis
【发布时间】: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


    【解决方案1】:

    为了实现多租户结构,您可以进行如下操作:

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

    你可以使用自动装配工厂地图

    @Autowired
       private Map<String, JedisConnectionFactory> JedisConnectionFactories;
    
       @Autowire
       protected DataSource selectJedisConnectionFactory(String tenantId) {
         return this.JedisConnectionFactories.get(tenantId);
       }
    

    我希望这会有所帮助!

    【讨论】:

    • 没关系,但是如何将相关的连接工厂设置为spring使用的redis模板。也可以根据要求进行更改。
    • 这是一个很好的例子,说明如何使用 spring tech.asimio.net/2017/01/17/…实现多租户结构
    • @sam 你找到解决办法了吗?
    猜你喜欢
    • 2016-05-03
    • 2015-01-10
    • 1970-01-01
    • 2022-11-18
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    相关资源
    最近更新 更多