前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >apache Ignite 安装和helloworld

apache Ignite 安装和helloworld

作者头像
lovelife110
发布2021-12-08 17:18:48
8050
发布2021-12-08 17:18:48
举报
文章被收录于专栏:爱生活爱编程爱生活爱编程

代码位置

https://github.com/lilihongjava/ignite_examples/tree/main/ignite-01

安装

下载ignite安装包,apache-ignite-2.11.0-bin.zip

选2台服务器,解压文件,在bin目录下(如/root/ignite/apache-ignite-2.11.0-bin/bin)执行

代码语言:javascript
复制
./ignite.sh ../examples/config/example-ignite.xml

日志如下:

代码语言:javascript
复制
[15:56:53] Ignite node started OK (id=235e6170)
[15:56:53] Topology snapshot [ver=1, locNode=235e6170, servers=1, clients=0, state=ACTIVE, CPUs=8, offheap=3.9GB, heap=4.3GB]
[15:56:53]   ^-- Baseline [id=0, size=1, online=1, offline=0]
[15:56:58] Joining node doesn't have stored group keys [node=acd7e430-d4b3-443c-816f-d3a3f6c8fa1f]
[15:56:58] Topology snapshot [ver=2, locNode=235e6170, servers=2, clients=0, state=ACTIVE, CPUs=16, offheap=7.8GB, heap=8.6GB]
[15:56:58]   ^-- Baseline [id=0, size=2, online=2, offline=0]

Ignite node started OK,则表示节点启动成功,此例中在2台服务器启动,所以 online=2。

helloworld程序

创建maven项目,添加包依赖

代码语言:javascript
复制
    <properties>
        <ignite.version>2.11.0</ignite.version>
    </properties>
    <dependencies>
        <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>
    </dependencies>

创建HelloWorld.java 文件

cfg.setClientMode(true),作为客户端节点启动。

采用tcp发现方式(TcpDiscoveryMulticastIpFinder)查找服务节点

Ignition.start(cfg), 启动客户端节点

代码语言:javascript
复制
        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("10.1.12.215:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
        // Starting the node
        Ignite ignite = Ignition.start(cfg);

客户端创建cache(myCache)

代码语言:javascript
复制
 // Create an IgniteCache and put some values in it.
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
        cache.put(1, "Hello");
        cache.put(2, "World!");
        System.out.println(">> Created the cache and add the values.");

向服务器发起计算任务

代码语言:javascript
复制
 ignite.compute(ignite.cluster().forServers()).broadcast(new RemoteTask());
 System.out.println(">> Compute task is executed, check for output on the server nodes.");

RemoteTask 任务如下 打印myCache内容

代码语言:javascript
复制
    private static class RemoteTask implements IgniteRunnable {
        @IgniteInstanceResource
        Ignite ignite;

        @Override public void run() {
            System.out.println(">> Executing the compute task");

            System.out.println(
                    "   Node ID: " + ignite.cluster().localNode().id() + "\n" +
                            "   OS: " + System.getProperty("os.name") +
                            "   JRE: " + System.getProperty("java.runtime.name"));

            IgniteCache<Integer, String> cache = ignite.cache("myCache");

            System.out.println(">> " + cache.get(1) + " " + cache.get(2));
        }
    }

结果

客户端日志 Topology snapshot […clients=1… 表示成功加入集群

代码语言:javascript
复制
[16:09:05] Ignite node started OK (id=72ba4f53)
[16:09:05] Topology snapshot [ver=3, locNode=72ba4f53, servers=2, clients=1, state=ACTIVE, CPUs=24, offheap=7.8GB, heap=12.0GB]
[16:09:05]   ^-- Baseline [id=0, size=2, online=2, offline=0]
>> Created the cache and add the values.
>> Compute task is executed, check for output on the server nodes.
[16:09:08] Ignite node stopped OK [uptime=00:00:02.482]

服务端日志 打印>> Hello World!, 即myCache的内容

代码语言:javascript
复制
[16:08:23] Topology snapshot [ver=3, locNode=235e6170, servers=2, clients=1, state=ACTIVE, CPUs=24, offheap=7.8GB, heap=12.0GB]
[16:08:23]   ^-- Baseline [id=0, size=2, online=2, offline=0]
>> Executing the compute task
   Node ID: 235e6170-d2f7-4ea6-a7b3-a67cfa50238c
   OS: Linux   JRE: OpenJDK Runtime Environment
>> Hello World!
[16:08:55] Topology snapshot [ver=4, locNode=235e6170, servers=2, clients=0, state=ACTIVE, CPUs=16, offheap=7.8GB, heap=8.6GB]
[16:08:55]   ^-- Baseline [id=0, size=2, online=2, offline=0]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-10-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码位置
  • 安装
  • helloworld程序
  • 结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档