ignite是什么?上apache官网去学习。
记录一下简单的使用情况,集群的发现使用JDBC,二个方面:缓存和持久化
一 配置,使用了springboot的configurationProperties注解
/** * Ignite配置属性 * * @author Mr.Duan * @since JDK8 2018/9/28 14:43 */ @Data @Component @ConfigurationProperties(value = "ignite") public class IgniteProperties { private Boolean enable = false; private IgniteType type = IgniteType.NONE; private String instanceName = UUID.randomUUID().toString(); }
/** * ignite持久化属性配置 * * @author Mr.Duan * @since JDK8 2018/9/28 17:59 */ @Data @Component @ConfigurationProperties(prefix = "ignite.storage") public class IgniteStorageProperties { private Boolean enable = false; private String path = IgniteConstants.STORAGE_PATH; private WALMode walMode = WALMode.FSYNC; private String walPath = IgniteConstants.WAL_PATH; private String walArchivePath = IgniteConstants.WAL_ARCHIVE_PATH; private Boolean walCompactionEnabled = true; private String regionName = IgniteConstants.STORAGE_REGION_NAME; }
/** * ignite集群发现使用jdbc时的数据源配置 * * @author Mr.Duan * @since JDK8 2018/9/28 15:00 */ @Data @Component @ConfigurationProperties(prefix = "ignite.datasource") public class IgniteDataSourceProperties { @NonNull private String url; @NonNull private String driverClassName; @NonNull private String username; private String password; }
补充下用到的常量和默认信息
/** * ${todo} * * @author Mr.Duan * @since JDK8 2018/9/28 13:59 */ public interface IgniteConstants { String IGNITE_PATH = System.getProperty("user.dir") + File.separator + "ignite"; String STORAGE_PATH = IGNITE_PATH + File.separator + "data"; String STORAGE_REGION_NAME = "default_region_data"; String WAL_PATH = IGNITE_PATH + File.separator + "wal"; String WAL_ARCHIVE_PATH = IGNITE_PATH + File.separator + "wal"; }
/** * IgniteType * * @author Mr.Duan * @since JDK8 2018/9/28 16:09 */ public enum IgniteType { /** * 单机模式 */ NONE(0), /** * JDBC发现 */ JDBC(1); private int key; IgniteType(int key) { this.key = key; } public int getKey() { return this.key; } }
这是启动相关的配置
/** * ignite配置 * * @author Mr.Duan * @since JDK8 2018/9/28 13:59 */ @Slf4j @Configuration public class IgniteConfig { @Resource private IgniteProperties igniteProperties; @Resource private IgniteStorageProperties igniteStorageProperties; @Bean(name = "igniteDataSource") @ConditionalOnClass(HikariDataSource.class) @ConditionalOnBean(IgniteDataSourceProperties.class) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(value = "ignite.type", havingValue = "jdbc", matchIfMissing = true) public DataSource igniteDataSource(IgniteDataSourceProperties igniteDataSourceProperties){ HikariDataSource dataSource = new HikariDataSource(); dataSource.setPoolName("ignite-ds-pool"); dataSource.setJdbcUrl(igniteDataSourceProperties.getUrl()); dataSource.setDriverClassName(igniteDataSourceProperties.getDriverClassName()); dataSource.setUsername(igniteDataSourceProperties.getUsername()); dataSource.setPassword(igniteDataSourceProperties.getPassword()); log.info("init igniteDataSource success"); return dataSource; } @Bean @ConditionalOnProperty(value = "ignite.enable", havingValue = "true", matchIfMissing = true) public Ignite ignite(IgniteConfiguration igniteConfiguration){ Ignite ignite = Ignition.start(igniteConfiguration); log.info("{} ignite started with discovery type {}", ignite.name(), igniteProperties.getType()); return ignite; } @Bean @ConditionalOnProperty(value = "ignite.enable", havingValue = "true", matchIfMissing = true) public IgniteConfiguration igniteConfiguration(DataSource igniteDataSource){ IgniteConfiguration igniteConfiguration = new IgniteConfiguration(); igniteConfiguration.setGridLogger(new Slf4jLogger()); igniteConfiguration.setIgniteInstanceName(igniteProperties.getInstanceName()); //自动发现配置 log.info("discovery ip finder {}", igniteProperties.getType()); if(IgniteType.JDBC.equals(igniteProperties.getType())){ TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi(); TcpDiscoveryJdbcIpFinder ipFinder = new TcpDiscoveryJdbcIpFinder(); ipFinder.setDataSource(igniteDataSource); ipFinder.setInitSchema(true); tcpDiscoverySpi.setIpFinder(ipFinder); igniteConfiguration.setDiscoverySpi(tcpDiscoverySpi); } //持久化 log.info("native storage {}", igniteStorageProperties.getEnable()); if(igniteStorageProperties.getEnable()){ DataStorageConfiguration storageConfiguration = new DataStorageConfiguration(); storageConfiguration.setStoragePath(igniteStorageProperties.getPath()); storageConfiguration.getDefaultDataRegionConfiguration() .setName(igniteStorageProperties.getRegionName()) .setPersistenceEnabled(true); storageConfiguration.setWalMode(igniteStorageProperties.getWalMode()); //walPath与walArchivePath一致时表示禁止使用walArchive storageConfiguration.setWalPath(igniteStorageProperties.getWalPath()); storageConfiguration.setWalArchivePath(igniteStorageProperties.getWalArchivePath()); storageConfiguration.setWalCompactionEnabled(igniteStorageProperties.getWalCompactionEnabled()); igniteConfiguration.setDataStorageConfiguration(storageConfiguration); } return igniteConfiguration; } }
单元测试
/** * ${todo} * * @author Mr.Duan * @since JDK8 2018/9/28 17:31 */ @Slf4j @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = UpmsApplication.class) public class IgniteTest { private final static String CACHE_NAME = "MY_CACHE"; @Resource private IgniteProperties igniteProperties; @Before public void active(){ Ignite ignite = Ignition.ignite(igniteProperties.getInstanceName()); ignite.cluster().active(true); log.info("is wal enabled == {}", ignite.cluster().isWalEnabled(CACHE_NAME)); } @Test public void testSeq(){ Ignite ignite = Ignition.ignite(igniteProperties.getInstanceName()); final IgniteAtomicSequence seq = ignite.atomicSequence("seqName", 0, true); for (int i = 0; i < 20; i++) { long currentValue = seq.get(); long newValue = seq.incrementAndGet(); System.out.println(currentValue); System.out.println(newValue); } } @Test public void testCache(){ Ignite ignite = Ignition.ignite(igniteProperties.getInstanceName()); IgniteCachecache = ignite.getOrCreateCache(getCacheConfiguration()); ignite.cluster().enableWal(CACHE_NAME); IgniteTransactions transactions = ignite.transactions(); Transaction tx = transactions.txStart(); try { for (int i= 0; i<20000; i++){ cache.put("KEY"+i, "VALUE"+i); } tx.commit(); log.info("===================success"); } catch (Exception e){ tx.rollback(); log.info("===================rollback"); } finally { tx.close(); log.info("===================close"); } } @Test public void testGetCache(){ long start = System.currentTimeMillis(); Ignite ignite = Ignition.ignite(igniteProperties.getInstanceName()); IgniteCache cache = ignite.getOrCreateCache(getCacheConfiguration()); ignite.cluster().enableWal(CACHE_NAME); for (int i= 0; i<20000; i++){ //System.out.println("============" + cache.get("KEY"+i)); cache.get("KEY"+i); } long end = System.currentTimeMillis(); log.info("cost time millis {}", end - start); } private CacheConfiguration getCacheConfiguration(){ CacheConfiguration cacheConfiguration = new CacheConfiguration<>(); cacheConfiguration.setName(CACHE_NAME); cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); cacheConfiguration.setBackups(2); cacheConfiguration.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC); return cacheConfiguration; } }
使用持久化的CacheConfiguration要激活wal,否则是不会持久化的。
哪,就是这一句 ignite.cluster().enableWal(CACHE_NAME);
只是个简单的使用,其它情况再做研究。
哦,还有个配置文件
ignite: enable: true type: none instance-name: ccw-service-upms-ignite datasource: url: jdbc:mysql://localhost:3306/sms?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver storage: enable: true path: D:/ignite/data wal-mode: fsync wal-path: D:/ignite/wal wal-archive-path: D:/ignite/wal_arch region-name: Default_Region