码迷,mamicode.com
首页 > 编程语言 > 详细

Java使用Redis

时间:2018-07-03 20:03:14      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:error   thread   serve   spec   blank   may   pack   nts   tar   

Redis的安装略过

首先导入jedis.jar

1.连接redis服务

写个简单的程序:

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个主要的坑点:

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

技术分享图片

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

技术分享图片

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

技术分享图片

如果不加--protected-mode no,Java运行报错误:

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)

正常运行的结果:

技术分享图片

 

Java使用Redis

标签:error   thread   serve   spec   blank   may   pack   nts   tar   

原文地址:https://www.cnblogs.com/fangpengchengbupter/p/9260069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!