Java使用Redis

Redis的安装略过java

首先导入jedis.jarredis

1.链接redis服务ide

写个简单的程序:oop

package com.fpc.Test;

import redis.clients.jedis.Jedis;

public class ManipulateRedis {
    public static void main( String[] args ) {
        //链接10.0.20.251的Redis服务
        Jedis jedis = new Jedis("10.0.20.251");
        
        //查看服务是否运行
        System.out.println("服务正在运行 : " + jedis.ping());
    }
}

注意:说下2个主要的坑点:this

1.Redis要配置下redis.conf文件spa

要配置什么呢?配置其监听的ip地址3d

2.第二个要注意的就是redis-server的启动要加参数rest

若是不加--protected-mode no,Java运行报错误:code

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)
    at com.fpc.Test.ManipulateRedis.main(ManipulateRedis.java:11)

正常运行的结果:server

 

Redis&Java 操做元素

package com.fpc.Test;

import redis.clients.jedis.Jedis;

public class ManipulateRedis {
    public static void main( String[] args ) {
        //链接10.0.20.251的Redis服务
        Jedis jedis = new Jedis("10.0.20.251");
        
        //查看服务是否运行
        System.out.println("服务正在运行 : " + jedis.ping());
        
        //设置redis字符串数据
        jedis.set("fpc", "fangpengcheng");
        
        //获取存储的数据并输出
        String value  = jedis.get("fpc");
        System.out.println("Redis 存储的字符串为  :"  + value);
    }
}

运行结果:

 

 Redis&Java 操做列表

package com.fpc.Test;

import java.util.List;

import redis.clients.jedis.Jedis;

public class ManipulateRedis {
    public static void main( String[] args ) {
        //链接10.0.20.251的Redis服务
        Jedis jedis = new Jedis("10.0.20.251");
        
        //查看服务是否运行
        System.out.println("服务正在运行 : " + jedis.ping());
        
        //将数据存储到列表中,每次都是从左边push进入list
        jedis.lpush("list", "fangpengcheng1");
        jedis.lpush("list", "fangpengcheng2");
        jedis.lpush("list", "fangpengcheng3");
        
        //获取存储的数据并输出
        List<String> list = jedis.lrange("list", 0, 2);
        System.out.println(list.toString());
        
        list = jedis.lrange("list", 0, 3);
        System.out.println(list.toString());
        
        list = jedis.lrange("list", 0, 4);
        System.out.println(list.toString());
        
        list = jedis.lrange("list", 0, 5);
        System.out.println(list.toString());
        
        list = jedis.lrange("list", 0, 6);
        System.out.println(list.toString());
        
        list = jedis.lrange("list", 0, 7);
        System.out.println(list.toString());
        
        list = jedis.lrange("list", 0, 8);
        System.out.println(list.toString());
    }
}

运行的结果是:

可见若是jedis.lrange中给出的范围是超过Redis中实际list的长度的话是会循环继续取元素的。

 Redis&Java也能够操做Redis中的Set,Map等,再这里就不赘述了,查下文档就能够。下面打算用Redis&Java实现一个消息队列。

相关文章
相关标签/搜索