栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Apache Ignite 与Redis的兼容性

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Apache Ignite 与Redis的兼容性

Apache Ignite服务端可以兼容Redis Java客户端协议,可以使用Redis客户端像操作redis一样操作。大多数redis客户端操作支持,部分指令无法兼容。

兼容的操作:

Redishttps://apacheignite.readme.io/docs/redis

测试版本

Apache Ignite V2.13.0

Jredis 版本2.8.0

Maven 代码版本依赖


            org.apache.ignite
            ignite-core
            ${ignite.version}


            org.apache.ignite
            ignite-spring
            ${ignite.version}




    redis.clients
    jedis
    2.8.0

此处相当于把Apache Ignite当作Redis服务端使用,ignite需要初始化一个redis-ignite-internal-cache-0缓存,作为默认的存储,该缓存将用作您的默认 Redis 数据库以及您使用 SELECT 命令切换到的其他数据库的模板。

并且绑定6379供redis客户端访问 。

服务端代码

public class RedisMock {
    public static void main(String[] args) {

        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setPeerClassLoadingEnabled(true);

        //设置连接方式
        ConnectorConfiguration configuration = new ConnectorConfiguration();

        //设置主机和端口号,redis服务端默认绑定6379
        configuration.setHost("localhost");
        configuration.setPort(6379);
        cfg.setConnectorConfiguration(configuration);

        //设置缓存
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setName("redis-ignite-internal-cache-0");
        cacheConfiguration.setAtomicityMode(CacheAtomicityMode.ATOMIC);
        cacheConfiguration.setBackups(1);
        cfg.setCacheConfiguration(cacheConfiguration);

        // Setting up an IP Finder to ensure the client can locate the servers.
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));


        // Starting the node
        Ignite ignite = Ignition.start(cfg);

        while (true) {
         //waite
        }

    }
}

启动服务端代码,会在6379监听客户端请求

客户端代码,使用Redis客户端代码访问ignite,默认访问6379.

public class JedisDemo {
    public static void main(String[] args) {

        //创建连接redis数据库的对象
//        Jedis jedis = new Jedis("localhost", 6379);
        //创建连接池对象


        //iginit 默认的兼容端口是11211
        JedisPool jedisPool = new JedisPool(
                new GenericObjectPoolConfig(),
                "localhost",
                6379, 600000,
                null, Protocol.DEFAULT_DATABASE,
                null);
        //new JedisPool("localhost", 6379);
        //通过连接池对象获取连接redis的连接对象
        Jedis jedis = jedisPool.getResource();

        //测试是否连接
        String pong = jedis.ping();
        System.out.println("是否连接:" + pong);

        //切换数据库
        String result = jedis.select(1);
        System.out.println("切换数据库:" + result);
//        //查看当前数据库中所有键key
//        Set keys = jedis.keys("*");
//        System.out.println("所有键:" + keys);
        //获取键的数量
        long keySize = jedis.dbSize();
        System.out.println("键的数量:" + keySize);

        System.out.println("---string类型---");
        //1.string类型
        //存储
        jedis.set("hello", "welcome to redis");
        //获取
        System.out.println(jedis.get("hello"));

        System.out.println("---list类型---");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985951.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号