</pre><pre name="code" class="java">java web如何结合redis? **********第一步:安装redis到linux服务器. 因为redis依赖tcl,所以首先需要安装tcl. 将tcl8.5.10-src.tar.gz拷贝到/opt/tcl目录下执行如下指令: [email protected]:~/tcl# tar -zxvf tcl8.5.10-src.tar.gz [email protected]:~/tcl# cd tcl8.5.10/unix/ [email protected]:~/tcl/tcl8.5.10/unix# ./configure [email protected]:~/tcl/tcl8.5.10/unix# make [email protected]:~/tcl/tcl8.5.10/unix# make install 将redis-3.0.1.tar.gz拷贝到/opt/redis目录下执行如下指令: [email protected]:~/redis# tar -zxvf redis-3.0.1.tar.gz [email protected]:~/redis# cd redis-3.0.1 [email protected]:~/redis/redis-3.0.1# make [email protected]:~/redis/redis-3.0.1# make test [email protected]:~/redis/redis-3.0.1# make install **********第二步:启动redis. 注意:默认情况下redis编译时会将redis-server等命令拷贝至/usr/local/bin目录下. 同时,redis启动默认是在前台,可以通过修改如下配置文件将redis启动模式改为后台. [email protected]:~/redis/redis-3.0.1# vi redis.conf 将daemonize no修改为daemonize yes表示启动后在后台运行. 启动redis需要指定配置文件,如下: [email protected]:/usr/local/bin# ./redis-server /opt/redis/redis-3.0.1/redis.conf [email protected]:/usr/local/bin# ps -ef | grep redis root 5968 1 0 10:41 ? 00:00:00 ./redis-server *:6379 root 5972 5820 0 10:41 pts/1 00:00:00 grep redis [email protected]:/usr/local/bin# **********第三步:停止redis. [email protected]:/usr/local/bin# redis-cli -h 192.168.1.3 -p 6379 shutdown [email protected]:/usr/local/bin# ps -ef | grep redis root 4799 4658 0 19:53 pts/1 00:00:00 grep redis **********第四步:连接redis. [email protected]:/usr/local/bin# redis-cli -h 192.168.1.3 -p 6379 192.168.1.3:6379> set username IS OK 192.168.1.3:6379> get username "IS" 192.168.1.3:6379> **********第五步:创建java项目,引入jedis2.7.2.jar. 马上开启你的redis学习之旅吧! public class Test { private static Jedis jedis = null; public static void main(String[] args) { jedis = new Jedis("192.168.1.3"); jedis.set("username", "IluckySi"); System.out.println(jedis.get("username")); //IluckySi } }
时间: 2024-10-28 09:28:21