Apache Ignite具有非常先进的集群能力,包括逻辑集群组和自动发现。Apache Ignite节点之间会自动发现对方,这有助于必要时扩展集群,而不需要重启整个集群。开发者可以利用Ignite的混合云支持,允许公有云(比如AWS)和私有云之间建立连接,向他们提供两者的好处。
在启动时,节点被分配以下两种角色之一:服务器节点或客户端节点。服务器节点是集群的主力;它们缓存数据、执行计算任务等。客户端节点作为常规节点加入拓扑,但它们不存储数据。客户端节点用于将数据流式传输到集群中并执行用户查询。
要形成集群,每个节点必须能够连接到所有其他节点。为确保这一点,必须配置适当的发现机制。
发现机制
节点可以自动发现彼此并形成集群。这允许您在需要时进行横向扩展,而无需重新启动整个集群。开发人员还可以利用 Ignite 的混合云支持,允许在私有云和公共云(如 Amazon Web Services)之间建立连接,为他们提供两全其美的体验。
Ignite 提供了两种用于不同使用场景的发现机制实现:
-
TCP/IP Discovery专为数百个节点而设计和优化。
-
ZooKeeper Discovery允许将 Ignite 集群扩展到 100 和 1000 个节点,保持线性可扩展性和性能。
本文使用版本为 ignite v2.13.1
默认安装 jdk 1.8 版本即可,解压安装包,在集群每台主机上的 $IGNITE_HOME/config 目录下增加 default.xml 配置文件如下:
193,1 底端
192.168.165.3:47500..47509 192.168.165.4:47500..47509 192.168.165.5:47500..47509
通过bin/ignite.sh config/default.xml启动服务,一般Ignite有服务发现机制,无论有多少个节点,都可以只配置如上面配置的集群中的3个IP就可以,这个三个节点需要提前启动,其他新加入的节点,会通过这三个节点,路由到集群中的其他机器。
客户端访问通过集群API查看集群的状态。
public class ClusterDemo {
public static void main(String[] args) {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
cfg.setClientMode(true);
// Setting up an IP Finder to ensure the client can locate the servers.
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("192.168.165.3:47500..47509"));
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
// Starting the node
Ignite ignite = Ignition.start(cfg);
IgniteCluster cluster = ignite.cluster();
Collection baselineNodes = cluster.currentBaselineTopology();
System.out.println(baselineNodes);
ClusterGroup remoteGroup = cluster.forRemotes();
System.out.println(remoteGroup.hostNames());
// 所有的缓存
System.out.println(ignite.cacheNames());
ignite.close();
}
}
缓存读写
通过客户端API创建缓存,读写数据
public class HelloWorld {
public static void main(String[] args) throws IgniteException {
// Preparing IgniteConfiguration using Java APIs
IgniteConfiguration cfg = new IgniteConfiguration();
// The node will be started as a client node.
cfg.setClientMode(true);
// Classes of custom Java logic will be transferred over the wire from this app.
cfg.setPeerClassLoadingEnabled(true);
// Setting up an IP Finder to ensure the client can locate the servers.
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("192.168.165.2:47500..47509"));
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
// Starting the node
Ignite ignite = Ignition.start(cfg);
// Create an IgniteCache and put some values in it.
IgniteCache cache = ignite.getOrCreateCache("myCache");
cache.put(1, "Hello");
cache.put(2, "World!");
System.out.println(">> Created the cache and add the values.");
// Executing custom Java compute task on server nodes.
ignite.compute(ignite.cluster().forServers()).broadcast(new RemoteTask());
System.out.println(">> Compute task is executed, check for output on the server nodes.");
// Disconnect from the cluster.
ignite.close();
}
基线拓扑
基线拓扑是一组用于保存数据的节点。引入基线拓扑的概念是为了让您能够控制何时要 重新平衡集群中的数据。例如,如果您有一个由 3 个节点组成的集群,其中数据在节点之间分布,并且您再添加 2 个节点,则重新平衡过程会在所有 5 个节点之间重新分布数据。当基线拓扑发生变化时,就会发生重新平衡过程,这可以自动发生,也可以手动触发。
基线拓扑仅包括服务器节点;客户端节点从不包括在内,因为它们不存储数据。
基线拓扑的目的是:
-
当服务器节点在短时间内离开集群时,避免不必要的数据传输,例如,由于偶尔的网络故障或计划的服务器维护。
-
让您能够控制何时要重新平衡数据。
启用基线拓扑自动调整后,基线拓扑会自动更改。这是纯内存集群的默认行为。对于持久集群,必须手动启用基线拓扑自动调整功能。默认情况下,它被禁用,您必须手动更改基线拓扑。您可以使用控制脚本更改基线拓扑。



