建一个Maven项目,

pom里加下jedis依赖,

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.</version>
</dependency>
 package com.java56.redis;

 import redis.clients.jedis.Jedis;

 /**
* 测试类
* @author user
*
*/
public class JedisTest { public static void main(String[] args) {
Jedis jedis=new Jedis("192.168.1.107",); // 创建客户端 设置IP和端口
jedis.set("name", "java56教程网"); // 设置值
String value=jedis.get("name"); // 获取值
System.out.println(value);
jedis.close(); // 释放连接资源
}
}

测试代码,

运行 报错了

连接超时,

我们配置下防火墙 开一个6379端口权限

firewall-cmd --zone=public --add-port=6379/tcp --permanent

firewall-cmd --reload

继续运行 还是报错 连接超时 错误;

我们配置下 redis配置文件

[root@localhost redis]# vi /usr/local/redis/redis.conf

这里绑定了本机,我们把这个备注掉;

# bind 127.0.0.1

配置完后

[root@localhost redis]# ./bin/redis-cli shutdown

[root@localhost redis]# ./bin/redis-server ./redis.conf

要重启下redis服务;

继续运行 又报错了

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.Jedis.set(Jedis.java:121)

at com.java1234.redis.JedisTest.main(JedisTest.java:14)

这个是因为远程连接redis redis自我保护 拒绝访问;

有两种方法 解决

第一种 直接去掉自我保护功能(不推荐)

[root@localhost redis]# vi /usr/local/redis/redis.conf

进入配置

找到 protected-mode yes

改成 no即可

编辑后 重启redis服务,然后运行 ,结果出来了

第二种 设置redis连接密码

进入客户端

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> config set requirepass 123456

设置密码 123456

127.0.0.1:6379> quit

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> auth 123456

OK

说明设置成功

 package com.java56.redis;

 import redis.clients.jedis.Jedis;

 /**
* 测试类
* @author user
*
*/
public class JedisTest { public static void main(String[] args) {
Jedis jedis=new Jedis("192.168.1.107",); // 创建客户端 设置IP和端口
jedis.auth(""); // 设置密码
jedis.set("name", "java知识分享网"); // 设置值
String value=jedis.get("name"); // 获取值
System.out.println(value);
jedis.close(); // 释放连接资源
}
}

这样就OK了

Jedis连接 HelloWorld实现的更多相关文章

  1. Jedis与Jedis连接池

    1.Jedis简介 实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis, 对于主流语言,Redis都提供了对应的客户端: https://redis.io/clients 2. ...

  2. 用Jedis连接Redis

    jedis中的方法名,和Redis的命令几乎一样 1.jar包,作为测试只需要一个jar 2.代码 package com; import java.util.HashMap; import java ...

  3. redis linux 安装及jedis连接测试

    一.安装配置 1:下载redis下载地址 http://code.google.com/p/redis/downloads/list推荐下载redis-1.2.6.tar.gz,之前这个版本同事已经有 ...

  4. redis客户端jedis连接和spring结合

    摘自传智博客课程 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt ...

  5. Jedis连接

    Jedis连接 到场api中的jedis.我们能够发现,jedis类提供了4个构造方法.都可用于连接: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29 ...

  6. Redis 学习笔记3:Jedis 连接虚拟机下的Redis 服务

    Jedis 是 Redis 官方首选的 Java 客户端开发包. 虚拟机的IP地址是192.168.8.88. Jedis代码是放在windows上的,启动虚拟机上的Redis服务之后,用Jedis连 ...

  7. Redis安装以及Java客户端jedis连接不上相关问题解决

    安装步骤 1.由于Redis是由C 语言编写的 所以虚拟机编译需要C的编译环境 用命令 yum install gcc-c++ 2.用SFTP上传Redis安装包并解压 3.进入Redis源码目录 b ...

  8. Spring-Data-Redis 下实现jedis连接断开后自动重连

    原先使用jedis的时候,处理手段是在从连接池获取连接时捕获JedisConnectionException异常,在异常处理部分重新获取连接,但是spring data redis似乎不会,如下所示: ...

  9. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

随机推荐

  1. 【Android开发坑系列】之窗口管理

    关键知识要点如下(持续更新): WindowManagerService只负责管理Window,不负责图像的绘制: SurfaceFlinger负责图像的合成:

  2. Android PopupWindow 仿微信弹出效果

    项目中,我须要PopupWindow的时候特别多,这个东西也特别的好使,所以我今天给大家写一款PopupWindow 仿微信弹出效果.这样大家直接拿到项目里就能够用了! 首先让我们先看效果: 那么我首 ...

  3. 如何关闭windows server2012 80端口

    Windows Server禁用本地端口的两种方法 这篇文章主要介绍了Windows Server 2008 禁用本地端口的两种方法,本文讲解了通过Windows防火墙禁用端口.通过IP安全策略禁用端 ...

  4. 【iCore4 双核心板_uC/OS-II】例程五:信号量——共享资源

    一.实验说明: 信号量是操作系统中的一类事件,是实现任务间通信的一个中间环节.当系统中的多个任务 在运行时,经常需要互相无冲突地访问同一个资源,或者需要互相支持的依赖,甚至有时还要互 相加以必要的限制 ...

  5. golang字符串拼接

    四种拼接方案: 1,直接用 += 操作符, 直接将多个字符串拼接. 最直观的方法, 不过当数据量非常大时用这种拼接访求是非常低效的. 2,直接用 + 操作符,这个和+=其实一个意思了. 3,用字符串切 ...

  6. session_id()和session_regenerate_id()对原来session文件和其中数据是怎么处理的

    一.session_id()对原来session文件和里面的数据,是怎么处理的? 测验办法:<?php $sid = md5("aaad");session_id($sid) ...

  7. Failed to execute 'write' on 'Document'动态载入的js不能执行write

    统计代码一般都是直接一个标签,插入js,标签放在哪里,统计图表就放在哪里! 我现在是稍微改了一下,我自己加了一点js,在页面所有元素都加载完成之后我再动态的把统计js插入到我需要的地方. 统计代码的s ...

  8. 用WordPress建立专业网站教程 (一步步建站, 一步也不少)

    最新美国域名中心US Domain Center: http://www.usdomaincenter.com/ 建站教程 (10分钟上线, 无需备案): https://www.qiyewp.com ...

  9. 《effective Go》读后记录:GO基础

    一个在线的Go编译器 如果还没来得及安装Go环境,想体验一下Go语言,可以在Go在线编译器 上运行Go程序. 格式化 让所有人都遵循一样的编码风格是一种理想,现在Go语言通过gofmt程序,让机器来处 ...

  10. js 正则表达式验证网站域名

    正则表达式 ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$