Redis_简介(连接demo)

Redis简介-认识Redis

1、什么是redis:

redis是开源BSD许可高级的key-value存储系统(NoSQL),可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务。

在Java程序中通过jedis连接Redis。

redis是一种高级的key_value的存储系统,其中value支持五中数据类型。
1、字符串(String)
2、哈希(hash)
3、字符串列表(list)
4、字符串集合(set)
5、有序字符串集合(sorted set)
所有的操作都是针对与Key关联的Value的。

redis是用c语言开发的一个开源的高性能键值对数据库。

 

2、简单使用

redis-cli.exe是客户端。连接Redis服务器后,就可以往redis服务器存数据。
需要使用一些命令。例如:ping,set/get,del等。

3、怎么用java连接redis

1.导入相应的jar包。
2.单实例连接:

public class JedisTest {

	public void testJedis() {
		Jedis jedis = testPool().getResource();
		// Jedis jedis = new Jedis("192.168.20.112", 6379); //连接Redis
		jedis.auth("123456");// 如果需密码
		int i = 0;// 记录操作次数
		try {
			long start = System.currentTimeMillis();// 开始毫秒数
			while (true) {
				long end = System.currentTimeMillis();
				if (end - start >= 1000) {// 当大于等于1000毫秒(相当于1秒)时,结束操作
					break;
				}
				i++;
				jedis.set("test" + i, i + "");
			}
		} finally {// 关闭连接
			jedis.close();
		}
		System.out.println("redis每秒操作:" + i + "次");// 打印1秒内对Redis的操作次数
	}

	private JedisPool testPool() {
		JedisPoolConfig poolCfg = new JedisPoolConfig();
		// 最大空闲数
		poolCfg.setMaxIdle(50);
		// 最大连接数
		poolCfg.setMaxTotal(100);
		// 最大等待毫秒数
		poolCfg.setMaxWaitMillis(20000);
		// 使用配置创建连接池
		JedisPool pool = new JedisPool(poolCfg, "192.168.20.112");
		// 从连接池中获取单个连接
		/*
		 * Jedis jedis = pool.getResource(); // 如果需密码 jedis.auth("123456");
		 */
		return pool;
	}
}


redis就是一个服务器,无论是从redis-cli.exe客户端,还是通过Java代码提交的数据都存放在里面。

3.连接池连接:
 

// 1.获得连接池配置对象,设置配置项。
JedisPoolConfig config = new JedisPoolConfig();
// 1.1最大连接数。
config.setMaxTotal(200);
// 1.2最大空闲连接数。
config.setMaxIdle(50);
config.setMinIdle(8);// 设置最小空闲数
config.setMaxWaitMillis(10000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
// Idle时进行连接扫描
config.setTestWhileIdle(true);
// 表示idle object evitor两次扫描之间要sleep的毫秒数
config.setTimeBetweenEvictionRunsMillis(30000);
// 表示idle object evitor每次扫描的最多的对象数
config.setNumTestsPerEvictionRun(10);
// 表示一个对象至少停留在idle状态的最短时间,然后才能被idle object
// evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
config.setMinEvictableIdleTimeMillis(60000);

 

4、redis和memcached区别:

1、redis可以用来做存储(storage),而memcached是用来做缓存(cache)这个特点主要因为其有持久化功能。

2、redis中存储的数据有多种结构,而memcached存储的数据只有一种类型“字符串”。

5、redis相关特性:

1.多数据库:
一个redis实例可以包含多个数据库,客户端可以指定连接某个redis实例的那个数据库。
2、服务器命令
3、消息订阅与发布
4、redis事务

 

6、java连接虚拟机中的redis出现的问题,redis已经启动就是连不上

redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:155)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:83)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:94)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:89)
    at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:539)
    at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2000)
    at com.taotao.redis.RedisTest.testJedisSingle(RedisTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
。。。。。。

原因:

检查redis的配置文件redis.conf,注释掉bind。

然后还会继续报错:

DENIED Redis is running in protected mode because protected mode is enabled…

需要设置密码,登录redis-cli

然后输入config set requirepass 123456   //123456是密码

 

开启外部客户端访问 redis 服务器的几种方式:

1、 bind 127.0.0.1 后直接添加 ip 地址,空格隔开;

2、注释掉 bind 127.0.0.1,并关闭保护模式: protected-mode no;

3、注释掉 bind 127.0.0.1,不修改默认保护模式,配置 requirepass yourpassword(你设置的密码), 以后客户端(以windows为例)可以通过命令 redis-cli.exe -h yourIP -p 6379 连接上服务器后输入 auth yourpassword(密码); 或 redis-cli.exe -h yourIP -p 6379 -a yourpassword(密码) 访问

 

Redis (error) NOAUTH Authentication required 解决方法:

输入密码即可解决 auth redis (redis是redis的密码)

127.0.0.1:6379> set mykey lisi
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> set mykey lisi
OK
127.0.0.1:6379> get mykey
"lisi"

 

 

 

每天努力一点,每天都在进步。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

powerfuler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值