命令行的形式扩展集群,是不断的增加Server,在实际的很多业务场景中,需要我们用Ignite实现CS模式,本文将提供详细的代码。
主要maven依赖:
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-indexing</artifactId>
<version>${ignite.version}</version>
</dependency>
<properties>
<ignite.version>2.3.0</ignite.version>
</properties>
代码实现
Server端实现:
import com.jc.searchengine.po.Person;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteCompute;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import java.util.Set;
/**
* @Author: wangjie
* @Description:
* @Date: Created in 10:20 2018/3/23
*/
public class Application {
public static void main(String[] args) {
Ignite ignite = Ignition.start();
CacheConfiguration cacheCfg = new CacheConfiguration();
cacheCfg.setName("serverCache");
cacheCfg.setCacheMode(CacheMode.PARTITIONED);
IgniteCache<Long,Person> cache = ignite.getOrCreateCache(cacheCfg);
ClusterGroup clusterGroup = ignite.cluster().forServers();
cache.put(1L,new Person(1,"Sofiya",2));
cache.put(2L,new Person(1,"Sofiya",2));
cache.put(3L,new Person(1,"Sofiya",666666666));
}
}
Server端的代码实质上主要做了2个操作:
- 创建一个名叫“serverCache”的缓存区
- 在缓存区插入数据
Client 端实现:
import com.jc.searchengine.po.Person;
import org.apache.ignite.*;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.query.ContinuousQuery;
import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.util.lang.IgnitePredicate2X;
import org.apache.ignite.lang.IgniteBiPredicate;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import javax.cache.Cache;
import java.time.Period;
import java.util.Arrays;
/**
* @Author: wangjie
* @Description:
* @Date: Created in 10:55 2018/3/23
*/
public class Application{
public static void main(String[] args) {
//此节点配置为客户端
Ignition.setClientMode(true);
IgniteConfiguration cfg = new IgniteConfiguration();
TcpCommunicationSpi commSpi = new TcpCommunicationSpi();
commSpi.setSlowClientQueueLimit(1000);
TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi();
TcpDiscoveryVmIpFinder ipfinder = new TcpDiscoveryVmIpFinder();
ipfinder.setAddresses(Arrays.asList("localhost"));
discoverySpi.setIpFinder(ipfinder);
cfg.setCommunicationSpi(commSpi);
cfg.setDiscoverySpi(discoverySpi);
Ignite ignite = Ignition.start(cfg);
ClusterGroup clusterGroup = ignite.cluster().forClients();
IgniteCache<Long,Person> cache = ignite.getOrCreateCache("serverCache");
IgniteCompute compute = ignite.compute(clusterGroup);
compute.broadcast(() -> {
Person s1 = cache.get(1L);
Person s2 = cache.get(2L);
Person s3 = cache.get(3L);
System.out.println(s1.toString() + " " + s2.toString() + " " + s3.toString());
});
}
}
Client端的代码做了以下工作:
- 将此节点配置为客户端模式
- 获取到刚刚Server段创建的“serverCache”缓存区
- 将Server端的数据展示出来
运行程序
分别启动Server端和Client端的程序,查看控制台:
Server端:
Server.pngClient端:
Client.png项目代码github地址-Server
项目代码github地址-Client
程序媛小白一枚,如有错误,烦请批评指正!(#.#)
网友评论