Spring Boot 整合 Apache Ignite

关于Ignite的介绍,这边推荐三个链接进行学习了解。

//ignite.apache.org/,首选还是官网,不过是英文版,如果阅读比较吃力可以选择下方两个链接。

//www.oschina.net/p/apache-ignite?hmsr=aladdin1e1,开源中国上对Ignite的介绍,包含特性、架构等内容,比较详细。

//www.infoq.cn/article/apache-ignite-explorer,这个链接中将Ignite与Redis、Hazelcast等同类型产品做了比较。

 

接下来我们开始实践,首先需要在本地搭建一套Ignite服务,从官网上可以进行下载,//ignite.apache.org/docs/latest/installation/installing-using-zip:

这里需要注意的是,我们要下载的是安装包,别下载了源码。下载完毕后解压,去到apache-ignite-2.8.1\bin目录双击ignite.bat即可启动服务:

打印上图中框出内容即为启动成功,接下来我们创建一个Spring Boot项目用于整合Ignite的相关验证,目录结构如下:

父工程的pom:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.22.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <h2.version>1.4.197</h2.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Spring Boot的版本推荐为1.5.X这类较为稳定的版本,从2.0.0开始的版本和Ignite会发生冲突,编译时抛出如下错误:

Error:(8, 8) java: 名称冲突: org.apache.ignite.springdata.repository.IgniteRepository中的deleteAll(java.lang.Iterable<ID>)和org.springframework.data.repository.CrudRepository中的deleteAll(java.lang.Iterable<? extends T>)具有相同疑符, 但两者均不覆盖对方

这里还需要注意一点,如果不指定h2的版本为1.4.197,启动应用时会抛出如下异常:

org.apache.ignite.internal.processors.query.IgniteSQLException: Failed to initialize system DB connection: jdbc:h2:mem:b9189e84-4966-4b03-8500-429a958cee4f;LOCK_MODE=3;MULTI_THREADED=1;DB_CLOSE_ON_EXIT=FALSE;DEFAULT_LOCK_TIMEOUT=10000;FUNCTIONS_IN_SCHEMA=true;OPTIMIZE_REUSE_RESULTS=0;QUERY_CACHE_SIZE=0;MAX_OPERATION_MEMORY=0;BATCH_JOINS=1;ROW_FACTORY="org.apache.ignite.internal.processors.query.h2.opt.H2PlainRowFactory";DEFAULT_TABLE_ENGINE=org.apache.ignite.internal.processors.query.h2.opt.GridH2DefaultTableEngine
	at org.apache.ignite.internal.processors.query.h2.ConnectionManager.connectionNoCache(ConnectionManager.java:213) ~[ignite-indexing-2.8.1.jar:2.8.1]
	at org.apache.ignite.internal.processors.query.h2.ConnectionManager.<init>(ConnectionManager.java:152) ~[ignite-indexing-2.8.1.jar:2.8.1]
	at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.start(IgniteH2Indexing.java:2114) ~[ignite-indexing-2.8.1.jar:2.8.1]
	at org.apache.ignite.internal.processors.query.GridQueryProcessor.start(GridQueryProcessor.java:256) ~[ignite-core-2.8.1.jar:2.8.1]
	at org.apache.ignite.internal.IgniteKernal.startProcessor(IgniteKernal.java:1978) ~[ignite-core-2.8.1.jar:2.8.1]
	at org.apache.ignite.internal.IgniteKernal.start(IgniteKernal.java:1212) ~[ignite-core-2.8.1.jar:2.8.1]

子模块spring-boot-test-ignite中需要添加Ignite的依赖,版本需要与本地搭建的Ignite服务保持一致:

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring-data</artifactId>
    <version>2.8.1</version>
</dependency>

接下来贴出Ignite的配置类IgniteConfig,通过动态IP发现绑定到本地的Ignite服务,同时创建实例与缓存供本地访问:

@Configuration
class IgniteConfig {

    @Bean
    public Ignite igniteInstance() {

        TcpDiscoverySpi spi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
        ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500"));
        spi.setIpFinder(ipFinder);

        IgniteConfiguration igniteConfig = new IgniteConfiguration();
        igniteConfig.setIgniteInstanceName("TestInstance");
        igniteConfig.setDiscoverySpi(spi);
        igniteConfig.setClientMode(true);

        CacheConfiguration cacheConfig = new CacheConfiguration("TestCache");
        cacheConfig.setIndexedTypes(Long.class, TestObject.class);
        igniteConfig.setCacheConfiguration(cacheConfig);
        return Ignition.start(igniteConfig);
    }
}

用于数据交互的测试类TestObject,注解@QuerySqlField的作用时告诉Ignite后续可以通过id字段发起查询,如果没有此注解也不影响数据的插入,但是查询时会抛出Column not found的异常:

public class TestObject {

    @QuerySqlField(index = true)
    long id;
    String name;

    public TestObject(long id, String name) {

        this.id = id;
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

通过实现IgniteRepository来创建查询方式:

@RepositoryConfig(cacheName = "TestCache")
public interface TestRepository extends IgniteRepository<TestObject, Long> {

    TestObject findById(long id);
}

再下来是用于测试的接口及其对应实现:

public interface TestService {

    TestObject save(TestObject testObject);

    TestObject findTestObjectById(long id);
}
@Service
public class TestServiceImpl implements TestService {

    @Autowired TestRepository testRepository;

    public TestObject save(TestObject testObject) {

        return testRepository.save(testObject.getId(), testObject);
    }

    public TestObject findTestObjectById(long id) {

        return testRepository.findById(id);
    }
}

倒数第二步是编写用于测试的controller层:

@RestController
public class TestController {

    @Autowired
    TestService testService;

    @RequestMapping("/test")
    public TestObject savePerson(@RequestParam(value = "id") long id, @RequestParam(value = "name") String name) {

        return testService.save(new TestObject(id, name));
    }

    @RequestMapping("/find")
    public TestObject findByCode(@RequestParam(value = "id") long id) {

        return testService.findTestObjectById(id);
    }
}

启动类中需要注意添加注解@EnableIgniteRepositories,否则应用是无法识别通过@RepositoryConfig修饰的Bean:

@SpringBootApplication
@EnableIgniteRepositories
public class IgniteApplication {

    public static void main(String[] args) {

        SpringApplication.run(IgniteApplication.class, args);
    }
}

启动应用后通过postman进行测试验证,首先创建一个TestObject的实例并塞入Ignite:

接下来通过id=12032进行查询:

成功返回!

 

整个实践过程中踩了不少坑,也是希望这篇随笔能为后面学习这块内容的同学节省一些时间吧。

 

 

参考资料:

//ignite.apache.org/

//www.oschina.net/p/apache-ignite?hmsr=aladdin1e1

//www.infoq.cn/article/apache-ignite-explorer

//blog.csdn.net/ltl112358/article/details/79399026

//blog.csdn.net/qq_35051298/article/details/81452550

//stackoverflow.com/questions/52652162/ignite-wont-start-with-spring-boot-2-0-5-h2-property-nested-joins-doesnt-exi

//cn.voidcc.com/question/p-ofvlepsc-dr.html