Redis安装 java中的连接 序列化 反序列化
安装路径 /webapp/redis/redis-3.2.
#启动redis
/webapp/redis/redis-3.2./src/redis-server &
#关闭redis
/webapp/redis/redis-3.2./src/redis-cli shutdown
1、安装
$ wget http://download.redis.io/releases/redis-3.2.3.tar.gz
$ tar xzf redis-3.2.3.tar.gz
$ cd redis-3.2.3
$ make MALLOC=libc
#启动redis
src/redis-server & #关闭redis
src/redis-cli shutdown
$ src/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
$
使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip
如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar,下载地址:http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip
//连接服务器的 Redis 服务
Jedis jedis = new Jedis("192.168.248.129", 6379);
//权限认证
jedis.auth("123456");
3、报错解决
a、绑定的ip修改,修改redis-3.2.3文件夹下的redis.conf文件
# bind 127.0.0.1 注掉绑定的本机ip地址
b、设置密码
# redis-cli
# config set requirepass 123456

NOAUTH Authentication required.
提示没有权限访问的时候
输入 auth "yourpassword" 即可连接 连接redis之前应该查看服务防火墙是否关闭,或者开启redis默认的端口 序列化的应用
/*
* Copyright (c) 2016 Sohu TV. All rights reserved.
*/
package com.sohu.dao.redis; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema; import com.sohu.model.Seckill; /**
* <P>
* Description:
* </p>
* @author jfw
* @version 1.0
* @Date 2016年2月14日下午2:51:48
*/
public class RedisDao { private final JedisPool jedisPool; private final RuntimeSchema<Seckill> schema=RuntimeSchema.createFrom(Seckill.class); private final Logger logger=LoggerFactory.getLogger(this.getClass()); public RedisDao(String ip,int port){
jedisPool=new JedisPool(ip,port);
} public Seckill getSeckill(long seckillId){
try {
Jedis jedis=jedisPool.getResource();
jedis.auth("123456");
try {
String key="seckill:"+seckillId;
byte[] bytes=jedis.get(key.getBytes());
if(bytes!=null){
Seckill seckill=schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, seckill, schema);
return seckill;
}
}finally{
jedis.close();
}
} catch (Exception e) {
logger.error("{seckillId}"+seckillId+e.getMessage(),e);
}
return null;
}
public String putSeckill(Seckill seckill){
try {
Jedis jedis=jedisPool.getResource();
jedis.auth("123456");
try {
String key="seckill:"+seckill.getId();
byte[] bytes=ProtostuffIOUtil.toByteArray(seckill, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
int timeout=60*60;
String result=jedis.setex(key.getBytes(),timeout, bytes);
return result;
}finally{
jedis.close();
}
} catch (Exception e) {
logger.error("{seckill}"+seckill+e.getMessage(),e);
}
return null;
}
}
spring.xml配置
<bean id="redisDao" class="com.sohu.dao.redis.RedisDao">
<constructor-arg index="0" value="192.168.1.143"/>
<constructor-arg index="1" value="6379"/>
</bean>
<!-- protostuff自定义序列化 -->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
Redis安装 java中的连接 序列化 反序列化的更多相关文章
- 【译】Java中的对象序列化
前言 好久没翻译simple java了,睡前来一篇. 译文链接: http://www.programcreek.com/2014/01/java-serialization/ 什么是对象序列化 在 ...
- java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)
java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver) 1.往项目中添加mysql-conne ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- Java中Jedis连接Linux上的Redis出现connect time out(解决方案)
我的代码: /** * * <p>Title: testJedis</p> * <p>Description: 测试单机版的redis连接(每连接一次构建一个对象) ...
- Redis在java中的使用
1.首先安装Redis环境.可以在Windows.linux.别的服务器上搭建Redis环境 2.在java工程中导入必须的jar包 如maven上导入两个jar] <!-- Redis必须包 ...
- java Serializable和Externalizable序列化反序列化详解--转
一.什么是序列化? “对象序列化”(Object Serialization)是 Java1.1就开始有的特性. 简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存 ...
- java Serializable和Externalizable序列化反序列化详解(转载)
一.什么是序列化? “对象序列化”(Object Serialization)是 Java1.1就开始有的特性. 简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存 ...
- Redis 在java中的使用(登录验证,5分钟内连续输错3次密码,锁住帐号,半小时后解封)(三)
在java中使用redis,做简单的登录帐号的验证,使用string类型,使用redis的过期时间功能 1.首先进行redis的jar包的引用,因为用的是springBoot,springBoot集成 ...
- Linux学习—redis安装配置及远程连接
1.下载安装包并解压 进入文件夹/usr/local cd /usr/local 下载redis安装包: wget http://download.redis.io/releases/redis-.t ...
随机推荐
- Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈
D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- [BZOJ1060][ZJOI2007]时态同步(树形DP)
[我是传送门] 因为边权只能增加,那么设f[u]为u子树上从i出发到达某个叶节点的最大路径, 显然Ans应该增加f[u]-f[v]-e[i].w Code #include <cstdio> ...
- 不同级域名中的 Cookie 共享
HTTP 响应头中 Set-Cookie 行未指定 domain 时则设置访问的域名 seliote.com 可以设置 seliote.com(也可以写成 .seliote.com 意思一样) 与 w ...
- HTML5 canvas 圆盘抽奖
使用html5 canvas 绘制的圆盘抽奖程序 效果图: 贴上全部代码: 1 <!DOCTYPE html> <html> <head> <meta ch ...
- PHP代码审计2-常用超全局变量,常用命令注入,常用XSS漏洞审计,文件包含
超全局变量 $GLOBALS — 引用全局作用域中可用的全部变量$_SERVER — 服务器和执行环境信息$_GET — HTTP GET 变量$_POST — HTTP POST 变量$_FILES ...
- fromkeys语法/set集合/深浅拷贝/列表/字典的删除
fromkeys语法: dic = {"apple":"苹果", "banana":"香蕉"} 返回新字典. 和原来的没 ...
- HTML介绍和head标签-01
主要内容 web标准 浏览器介绍 开发工具介绍 HTML介绍 HTML颜色介绍 HTML规范 HTML结构详解 一.web标准 web准备介绍: w3c:万维网联盟组织,用来制定web标准的机构(组织 ...
- 【Pascal's Triangle】cpp
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- Oracle 自增写给自己的
首先咱先建一张表: CREATE TABLE example( ID Number(4) NOT NULL PRIMARY KEY, NAME VARCHAR(25), PHONE VARCHAR(1 ...
- 【Python】print 方法的参数
当在IDEL或者命令行中执行 help(print) 命令时,就可以看到 print 方法的详细说明: print(value, ..., sep=' ', end='\n', file=sys.st ...