redis单机的搭建
一、单机版redeis
1、安装包下载
http://download.redis.io/releases/ 下载redis的压缩包,并放在/usr/soft文件夹下
2、解压压缩包:
tar -zxf redis-3.0.7.tar.gz
3、安装
这里安装redis在/usr/local/redis文件夹中
进入安装包:cd /usr/soft/redis-3.0.7,执行命令
make PREFIX=/usr/local/redis/ install
安装成功后
redis.conf是redis的配置文件,redis.conf在redis源码目录。注意修改port作为redis进程的端口,port默认6379。
4、在安装目录中创建目录conf,将redis源安装文件中的redis.cinf拷贝到redis的安装目录中
cp /usr/soft/redis-3.0.7/redis.conf /usr/local/redis/bin/
5、redis启动
直接运行./redis-server 是前台启动,在关闭运行的窗口后redis也将关闭
为了关闭窗口后不关闭redis,需要使用后台启动
5.1修改redis.conf的daemonize的no为yes
使用以下命令启动
./redis-server redis.conf
6、检测redis是否运行正常
6.1使用 ps -ef|grep redis 查看进程
6.2使用redis的客户端查看
当输入ping命令时,返回PONG就表示连接正常
7、通过jedis连接redis单机
需要的依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
</dependency>
可以使用以下代码测试
8、jedis与spring整合
添加配置文件applicationContext-jedis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 读取jedis配置文件; 这里可以不用配置,-dao已经配置了扫描配置文件 -->
<!-- <context:property-placeholder location="classpath:/properties/*.properties"/> -->
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!--jedis客户端单机版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${JEDIS_HOST}"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="redisClientDao" class="com.taotao.rest.dao.impl.JedisClientSingle"></bean> <!--定义自己定义的jedis工具类-->
</beans>
测试
这里可是自己封装一个工具类

浙公网安备 33010602011771号