Ignite緩存管理初體驗:ignite服務端配置,大家可以用參考官方進行配置(或者使用默認配置也可以)。
本文中的ignite使用版本是1.7,與spring結合使用。
maven依賴配置
ignite 客戶端配置
<property name="clientMode" value="true"><!--聲明為客戶端--> <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. --> <property name="discoverySpi"> <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> <property name="ipFinder"> <!-- Ignite provides several options for automatic discovery that can be used instead os static IP based discovery. For information on all options refer to our documentation: https://apacheignite.readme.io/docs/cluster-config --> <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. --> <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">--> <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder"> <property name="addresses"> <list> <!-- In distributed environment, replace with actual host IP address. --> <value>${ignite.serverIp}</value><!--服務端IP地址--> </list> </property> </bean> </property> </bean> </property> <property name="communicationSpi"> <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi"> <property name="localPort" value="${ignite.communication.localport}"><!--對接服務端端口--> <property name="localAddress" value="192.168.1.93"><!--ignite客戶端鏈接地址(適用多網卡情況)--> </property></property></bean> </property> </property>
igniteService代碼(interface實現緩存存儲,供應用調用)
@原碼 public interface IgniteCacheApplication { /** *設置系統對象 */ public boolean putIgniteCache(String parentName,String name,Object value,Long expiry); /** *獲取系統對象 */ public Object getIgniteCache(String parentName,String name); /** *移除系統對象 */ public boolean removeIgniteCache(String parentName,String name); /** *清空系統對象 */ public boolean clearIgniteCacheByName(String parentName,String name); /** *清空系統緩存 */ public void clearIgniteCache(); /** *銷毀緩存對象 */ public void destroyCache(String parentName);
@實現類原碼
@Service(“cacheServiceImpl”)
public class IgniteCacheServiceImpl implements CacheApplication,InitializingBean {
private static final Logger logger = Logger.getLogger(IgniteCacheServiceImpl.class); public static Ignite ignite ; @Autowired private IgniteConfiguration cfg; @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public boolean putIgniteCache(String parentName, String name, Object value, Long expiry) { boolean flag=true; try { CacheConfiguration cacheCfg = new CacheConfiguration(); if(StringUtils.isEmpty(name)){ return false; } if(!StringUtils.isEmpty(expiry)){ cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, expiry))); } cacheCfg.setName(parentName); cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg); cache.put(name, value); CacheConstant.igniteStatus="normal"; } catch (CacheException e) { if (e.getCause() instanceof IgniteClientDisconnectedException) { IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException)e.getCause(); cause.reconnectFuture().get(); // Wait for reconnect. logger.error("ignite Wait for reconnect",e); CacheConstant.igniteStatus="death"; flag=false; }else{ e.printStackTrace(); logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; flag=false; } } catch (Exception e) { e.printStackTrace(); logger.error("添加ignite對象緩存異常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; flag=false; } return flag; } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Object getIgniteCache(String parentName, String name) { try { CacheConfiguration cacheCfg = new CacheConfiguration(); if(StringUtils.isEmpty(name)){ return false; } cacheCfg.setName(parentName); cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg); CacheConstant.igniteStatus="normal"; return cache.get(name); } catch (CacheException e) { if (e.getCause() instanceof IgniteClientDisconnectedException) { IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException)e.getCause(); cause.reconnectFuture().get(); // Wait for reconnect. logger.error("ignite Wait for reconnect",e); CacheConstant.igniteStatus="death"; return null; }else{ e.printStackTrace(); logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; return null; } } catch (Exception e) { e.printStackTrace(); logger.error("獲取ignite對象緩存異常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; return null; } } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public boolean removeIgniteCache(String parentName, String name) { try { CacheConfiguration cacheCfg = new CacheConfiguration(); if(StringUtils.isEmpty(name)){ return false; } cacheCfg.setName(parentName); cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg); boolean flag = cache.remove(name); CacheConstant.igniteStatus="normal"; return flag; } catch (CacheException e) { if (e.getCause() instanceof IgniteClientDisconnectedException) { IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException)e.getCause(); cause.reconnectFuture().get(); // Wait for reconnect. logger.error("ignite Wait for reconnect",e); return false; }else{ e.printStackTrace(); logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; return false; } } catch (Exception e) { e.printStackTrace(); logger.error("移除ignite對象緩存異常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; return false; } } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public boolean clearIgniteCacheByName(String parentName, String name) { boolean flag=true; try { CacheConfiguration cacheCfg = new CacheConfiguration(); cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); cacheCfg.setName(parentName); IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg); if(StringUtils.isEmpty(name)){ return false; } cache.clear(); CacheConstant.igniteStatus="normal"; } catch (CacheException e) { if (e.getCause() instanceof IgniteClientDisconnectedException) { IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException)e.getCause(); cause.reconnectFuture().get(); // Wait for reconnect. logger.error("ignite Wait for reconnect",e); CacheConstant.igniteStatus="death"; return false; }else{ e.printStackTrace(); logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; return false; } } catch (Exception e) { e.printStackTrace(); logger.error("清除ignite對象中所有緩存異常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; flag=false; } return flag; } @Override public void clearIgniteCache() { try { @SuppressWarnings("rawtypes") CacheConfiguration cacheCfg = new CacheConfiguration(); cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); @SuppressWarnings("unchecked") IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg); cache.clear(); CacheConstant.igniteStatus="normal"; } catch (CacheException e) { if (e.getCause() instanceof IgniteClientDisconnectedException) { IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException)e.getCause(); cause.reconnectFuture().get(); // Wait for reconnect. logger.error("ignite Wait for reconnect",e); CacheConstant.igniteStatus="death"; }else{ e.printStackTrace(); logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; } } catch (Exception e) { e.printStackTrace(); logger.error("清除ignite緩存異常,errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; } } /** *系統啟動時,掃描ignite配置,並啟動 */ @Override public void afterPropertiesSet() throws Exception { startIgnite(); } public void setCfg(IgniteConfiguration cfg) { this.cfg = cfg; } public void startIgnite(){ logger.info("starting ignite ..."); ignite = Ignition.start(cfg); logger.info("started ignite ..."); } @Override public void destroyCache(String parentName) { try { ignite.destroyCache(parentName); CacheConstant.igniteStatus="normal"; } catch (CacheException e) { if (e.getCause() instanceof IgniteClientDisconnectedException) { IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException)e.getCause(); cause.reconnectFuture().get(); // Wait for reconnect. logger.error("ignite Wait for reconnect",e); CacheConstant.igniteStatus="death"; }else{ e.printStackTrace(); logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; } } catch (Exception e) { e.printStackTrace(); logger.error("清除ignite緩存異常,errorInfo:"+e.getMessage(),e); CacheConstant.igniteStatus="death"; } } </string,></string,></string,></string,></string,>