SpringBoot整合Apache Ignite的實現(xiàn)
關(guān)于Ignite的介紹,這邊推薦三個鏈接進(jìn)行學(xué)習(xí)了解。
https://ignite.apache.org/,首選還是官網(wǎng),不過是英文版,如果閱讀比較吃力可以選擇下方兩個鏈接。
https://www.oschina.net/p/apache-ignite?hmsr=aladdin1e1,開源中國上對Ignite的介紹,包含特性、架構(gòu)等內(nèi)容,比較詳細(xì)。
https://www.infoq.cn/article/apache-ignite-explorer,這個鏈接中將Ignite與Redis、Hazelcast等同類型產(chǎn)品做了比較。
接下來我們開始實踐,首先需要在本地搭建一套Ignite服務(wù),從官網(wǎng)上可以進(jìn)行下載,https://ignite.apache.org/docs/latest/installation/installing-using-zip:

這里需要注意的是,我們要下載的是安裝包,別下載了源碼。下載完畢后解壓,去到apache-ignite-2.8.1\bin目錄雙擊ignite.bat即可啟動服務(wù):

打印上圖中框出內(nèi)容即為啟動成功,接下來我們創(chuàng)建一個Spring Boot項目用于整合Ignite的相關(guān)驗證,目錄結(jié)構(gòu)如下:

父工程的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這類較為穩(wěn)定的版本,從2.0.0開始的版本和Ignite會發(fā)生沖突,編譯時拋出如下錯誤:
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,啟動應(yīng)用時會拋出如下異常:
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服務(wù)保持一致:
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data</artifactId>
<version>2.8.1</version>
</dependency>
接下來貼出Ignite的配置類IgniteConfig,通過動態(tài)IP發(fā)現(xiàn)綁定到本地的Ignite服務(wù),同時創(chuàng)建實例與緩存供本地訪問:
@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);
}
}
用于數(shù)據(jù)交互的測試類TestObject,注解@QuerySqlField的作用時告訴Ignite后續(xù)可以通過id字段發(fā)起查詢,如果沒有此注解也不影響數(shù)據(jù)的插入,但是查詢時會拋出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;
}
}
通過實現(xiàn)IgniteRepository來創(chuàng)建查詢方式:
@RepositoryConfig(cacheName = "TestCache")
public interface TestRepository extends IgniteRepository<TestObject, Long> {
TestObject findById(long id);
}
再下來是用于測試的接口及其對應(yīng)實現(xiàn):
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);
}
}
倒數(shù)第二步是編寫用于測試的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,否則應(yīng)用是無法識別通過@RepositoryConfig修飾的Bean:
@SpringBootApplication
@EnableIgniteRepositories
public class IgniteApplication {
public static void main(String[] args) {
SpringApplication.run(IgniteApplication.class, args);
}
}
啟動應(yīng)用后通過postman進(jìn)行測試驗證,首先創(chuàng)建一個TestObject的實例并塞入Ignite:

接下來通過id=12032進(jìn)行查詢:

成功返回!
整個實踐過程中踩了不少坑,也是希望這篇隨筆能為后面學(xué)習(xí)這塊內(nèi)容的同學(xué)節(jié)省一些時間吧。
參考資料:
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
http://cn.voidcc.com/question/p-ofvlepsc-dr.html
到此這篇關(guān)于SpringBoot整合Apache Ignite的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合Apache Ignite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決Eclipse導(dǎo)入的項目上有個紅叉,但不影響項目運行的問題
這篇文章主要介紹了完美解決Eclipse導(dǎo)入的項目上有個紅叉,但不影響項目運行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
IDEA Ultimate2020.2版本配置Tomcat詳細(xì)教程
這篇文章主要介紹了IDEA Ultimate2020.2版本配置Tomcat教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java如何實現(xiàn)遠(yuǎn)程文件下載到本地目錄
本文介紹了如何使用Java來實現(xiàn)遠(yuǎn)程文件的下載功能,主要通過HTTPS路徑下載文件到本地目錄,詳細(xì)介紹了相關(guān)代碼和測試步驟,并提供了實際案例供參考,本文旨在幫助需要實現(xiàn)文件下載功能的開發(fā)者快速掌握核心技術(shù)2024-10-10
SpringBoot實現(xiàn)自定義配置文件提示的方法
這篇文章主要介紹了SpringBoot實現(xiàn)自定義配置文件提示的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
MapStruct實體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解
今天小編就為大家分享一篇關(guān)于MapStruct實體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03

