【问题标题】:Apache Ignite with Couchbase as Persistence使用 Couchbase 作为持久性的 Apache Ignite
【发布时间】:2022-01-15 20:57:11
【问题描述】:

我正在尝试在后端使用 Apache IgniteCouchbase 作为持久层。我将其作为数据网格进行操作,以便为启动内存缓存所做的任何更改最终都会写入 couchbase。我正在使用 spring-boot 和 spring-data 来实现这一点。 igniteConfiguration bean 看起来像这样

@Bean(name = "igniteConfiguration")
    public IgniteConfiguration igniteConfiguration() {
        IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
        CacheConfiguration cache = new CacheConfiguration("sample");
        cache.setAtomicityMode(CacheAtomicityMode.ATOMIC);
        //The below line  where I am confused
        cache.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheStoreImplementationWithCouchbaseRepositoryBean.class);
        cache.setCacheStoreSessionListenerFactories()
        cache.setReadThrough(true);
        cache.setWriteThrough(true);
        
        igniteConfiguration.setCacheConfiguration(cache);

        return igniteConfiguration;
    }

我需要提供 Ignite 的 cacheStore 接口的一种实现,以便将 couchbase 连接为后端数据存储。我配置了 Couchbase Repository 类并在 CacheStoreImplementationWithCouchbaseRepositoryBean.java 中创建了它的一个 bean。但是这个存储库 bean 没有被启动,因为 CacheStoreImplementation 类不在 spring 上下文中并且总是为 null。

当我使用弹簧数据时。现在我有

  1. Ignite 的 Spring 数据存储库
  2. couchbase 的 Spring 数据存储库
  3. ignite的cacheStore接口的一种实现

但不确定如何以某种方式将 couchbase 存储库 bean 发送到 cacheStore 实现,以便它使用此存储库类在内部执行 couchbase 中的 crud 操作。

【问题讨论】:

    标签: java spring-boot spring-data couchbase ignite


    【解决方案1】:

    bivrantoshakil -

    “我需要提供Ignite的cacheStore接口的一个实现,以便连接couchbase作为后端数据存储。我配置了Couchbase Repository类并在CacheStoreImplementationWithCouchbaseRepositoryBean.java中创建了一个bean”

    您正在关注哪些文档/教程? 请拉上拉链并发布您的项目,我会进行调查。

    我有一些不切实际的想法

    1. 你可以选择不使用 Ignite 的 Couchbase,因为键值对 API 非常快,并且持久化是异步和可配置的。

    2. https://blog.couchbase.com/couchbase-spring-cache/ 有一个使用 couchbase 进行 Spring 数据缓存的教程,您可以跳到“添加 Spring Boot 缓存功能”。

    3. 这里有 Apache Ignite 教程 - https://www.baeldung.com/apache-ignite-spring-data

    【讨论】:

    • 嗨迈克尔,感谢您的检查。我找到了一个解决方法。我将在下一个答案中分享。
    【解决方案2】:

    看看这里是如何配置存储工厂的:https://github.com/apache/ignite/tree/master/examples/src/main/java/org/apache/ignite/examples/datagrid/store

    我建议将您的项目与 CouchBase 和/或 spring-data 解耦以使其更简单,然后一一添加适当的组件,以查看故障所在。

    如果您需要在另一个 bean 之前初始化一个 bean,请考虑使用适当的 annotations

    【讨论】:

      【解决方案3】:

      我找到了解决上述问题的方法。首先我创建了一个类来获取 Spring Application Context

      import org.springframework.beans.BeansException;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.ApplicationContextAware;
      import org.springframework.stereotype.Component;
      
      @Component
      public class ApplicationContextProvider implements ApplicationContextAware {
      
      private static ApplicationContext context;
      
      /**
       * Returns the Spring managed bean instance of the given class type if it
       * exists. Returns null otherwise.
       * 
       * @param beanClass
       * @return
       */
      public static <T extends Object> T getBean(Class<T> beanClass) {
          return context.getBean(beanClass);
      }
      
      @Override
      public void setApplicationContext(ApplicationContext context) throws BeansException {
      
          // store ApplicationContext reference to access required beans later on
          ApplicationContextProvider.context = context;
      }
      }
      

      然后在 CacheStoreImplementationWithCouchbaseRepositoryBean.class 我做了这个 -

      //Repository bean
      private CouchbaseRepository repository;
      @Override
      public Employee load(Long key) {
          repository = ApplicationContextProvider.getBean(CouchbaseRepository.class);
          return repository.findById(key).orElse(null);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-05-19
        • 1970-01-01
        • 2018-07-04
        • 2020-02-05
        • 2017-03-20
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2021-04-28
        相关资源
        最近更新 更多