Java连接Redis

Java连接Redis

Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对redis各类API进行封装调用.

引入jar包

我创建的是maven项目,所以只用在pom文件中加入

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.0.0</version>
		</dependency>

如果不是maven项目,你要确定引入相关依赖
在这里插入图片描述

编写测试类

package cn.jiangdoc;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 
 * @author jiangdoc
 *
 */
public class JedisUtil {
	public static void main(String[] args) {
	//ip地址,端口号
		Jedis jedis = cli_single("192.168.1.103", 6379);
		jedis.set("key", "first Java connect!");
		String value = jedis.get("key");
		System.out.println(value);
		
	}

	/**
	 * 单个连接
	 * 
	 * @param host
	 * @param port
	 * @return
	 */
	public static Jedis cli_single(String host, int port) {
		try {
			return new Jedis(host, port);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 连接池
	 * 
	 * @param host
	 * @param port
	 * @return
	 */
	public static Jedis cli_pool(String host, int port) {
		JedisPoolConfig config = new JedisPoolConfig();
		// 最大连接数
		config.setMaxTotal(10);
		// 最大连接空闲数
		config.setMaxIdle(2);
		JedisPool jedisPool = new JedisPool(config, host, port);
		try{
			
			return jedisPool.getResource();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}

	}
}

注意:如果出现
报错:Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:

  • 检查端口是否开放
    解决方法:1.关闭防火墙:service iptables stop
    2.开放端口:
    (1.修改配置文件:vi /etc/sysconfig/iptabls
    追加:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379-j ACCEPT
    (2.包存改变:service iptables save
    (3.重启服务:service iptables restart
  • 查看redis的配置文件
    在这里插入图片描述
    报错: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.
    报错信息很长,但是主要是说redis开启了protected mode,这也是Redis3.2加入的新特性,开启保护模式的redis只允许本机登录,同样设置在配置文件redis.conf中
    这里原来是yes代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,wq保存退出后再重启redis-server
    在这里插入图片描述
    下面再运行就可以了
    Jedis常用方法API
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值