SpringBoot整合Apache Ignite的實現

關於Ignite的介紹,這邊推薦三個鏈接進行學習瞭解。

https://ignite.apache.org/,首選還是官網,不過是英文版,如果閱讀比較吃力可以選擇下方兩個鏈接。

https://www.oschina.net/p/apache-ignite?hmsr=aladdin1e1,開源中國上對Ignite的介紹,包含特性、架構等內容,比較詳細。

https://www.infoq.cn/article/apache-ignite-explorer,這個鏈接中將Ignite與Redis、Hazelcast等同類型產品做瞭比較。

 接下來我們開始實踐,首先需要在本地搭建一套Ignite服務,從官網上可以進行下載,https://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進行查詢:

成功返回!

整個實踐過程中踩瞭不少坑,也是希望這篇隨筆能為後面學習這塊內容的同學節省一些時間吧。

參考資料:

https://ignite.apache.org/

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

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

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

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

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

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

到此這篇關於SpringBoot整合Apache Ignite的實現的文章就介紹到這瞭,更多相關SpringBoot整合Apache Ignite內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: