Dubbo高级篇_03_Redis的安装与使用
2016-04-03 20:31
633 查看
IP:192.168.1.121
环境:CentOS6.6
Redis版本:redis-3.0
安装目录:/usr/local/redis
1.编译和安装所需的包:
2.上传redis3安装包,解压
创建安装目录
[root@yxq src]# mkdir /usr/local/redis
解压
3.安装
(使用PREFIX指定安装目录):
安装完成后,可以看到/usr/local/redis 目录下有一个 bin 目录,bin 目录里就是 redis 的命令脚本:
4.将 Redis 配置成服务:
Redis 的启动脚本为:/usr/local/src/redis3.0/utils/redis_init_script
将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为 redis
编辑/etc/rc.d/init.d/redis,修改相应配置,使之能注册成为服务:
配置文件设置:
创建 redis 配置文件目录
以上配置操作完成后,便可将 Redis 注册成为服务:
防火墙中打开对应的端口
修改 redis 配置文件设置:
将daemonize no 改为> daemonize yes,改为yes作为后台进程使用,
pidfile /var/run/redis.pid 改为> pidfile /var/run/redis_6379.pid
设置为no,pid文件是不会生成,stop等命令就不会生效,/etc/rc.d/init.d
会报如下错误:
/var/run/redis_6379.pid does not exist, process is not running
启动 Redis 服务
将 Redis 添加到环境变量中:
停止服务
默认情况下,Redis开启安全认证,可以通过/usr/local/redis/conf/6379.conf的requirepass指定一个验证密码
使用RedisDesktopManager登录管理
不输入验证密码
输入验证密码,测试连接
客户端使用console
Jedis的使用
Jedis连接池的使用,这里设置redis不使用密码验证
spring-redis.xml
环境:CentOS6.6
Redis版本:redis-3.0
安装目录:/usr/local/redis
1.编译和安装所需的包:
2.上传redis3安装包,解压
创建安装目录
[root@yxq src]# mkdir /usr/local/redis
解压
3.安装
(使用PREFIX指定安装目录):
安装完成后,可以看到/usr/local/redis 目录下有一个 bin 目录,bin 目录里就是 redis 的命令脚本:
4.将 Redis 配置成服务:
Redis 的启动脚本为:/usr/local/src/redis3.0/utils/redis_init_script
将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为 redis
编辑/etc/rc.d/init.d/redis,修改相应配置,使之能注册成为服务:
配置文件设置:
创建 redis 配置文件目录
以上配置操作完成后,便可将 Redis 注册成为服务:
防火墙中打开对应的端口
修改 redis 配置文件设置:
将daemonize no 改为> daemonize yes,改为yes作为后台进程使用,
pidfile /var/run/redis.pid 改为> pidfile /var/run/redis_6379.pid
设置为no,pid文件是不会生成,stop等命令就不会生效,/etc/rc.d/init.d
会报如下错误:
/var/run/redis_6379.pid does not exist, process is not running
启动 Redis 服务
将 Redis 添加到环境变量中:
停止服务
默认情况下,Redis开启安全认证,可以通过/usr/local/redis/conf/6379.conf的requirepass指定一个验证密码
使用RedisDesktopManager登录管理
不输入验证密码
输入验证密码,测试连接
客户端使用console
Jedis的使用
public class RedisTest { private static final Log log = LogFactory.getLog(RedisTest.class); public static void main(String[] args) { Jedis jedis = new Jedis("192.168.1.121"); jedis.auth("yxq123456"); String key = "redis"; String value = ""; jedis.del(key); // 删数据 jedis.set(key, "YiXiaoqun"); // 存数据 value = jedis.get(key); // 取数据 log.info(key + "=" + value); jedis.set(key, "YiXiaoqun2"); // 存数据 value = jedis.get(key); // 取数据 log.info(key + "=" + value); //jedis.del(key); // 删数据 //value = jedis.get(key); // 取数据 //log.info(key + "=" + value); } }
Jedis连接池的使用,这里设置redis不使用密码验证
spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Jedis链接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="testWhileIdle" value="true" /> <property name="minEvictableIdleTimeMillis" value="60000" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="numTestsPerEvictionRun" value="-1" /> <property name="maxTotal" value="8" /> <property name="maxIdle" value="8" /> <property name="minIdle" value="0" /> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="192.168.1.121" /> <constructor-arg index="1" value="6379" type="int" /> </bean> </list> </constructor-arg> </bean> </beans>spring-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 采用注释的方式配置bean --> <context:annotation-config /> <!-- 配置要扫描的包 --> <context:component-scan base-package="redis.edu.demo" /> <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <import resource="spring-redis.xml" /> </beans>pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>redis.edu.demo</groupId> <artifactId>edu-demo-redis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>edu-demo-redis</name> <url>http://maven.apache.org</url> <build> <finalName>edu-demo-redis</finalName> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build> <dependencies> <!-- Common Dependency Begin --> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <classifier>jdk15</classifier> <scope>compile</scope> </dependency> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> </dependency> <dependency> <groupId>oro</groupId> <artifactId>oro</artifactId> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </dependency> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <!-- Common Dependency End --> <!-- Spring Dependency Begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <!-- Spring Dependency End --> <!-- Redis client --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.3</version> </dependency> </dependencies> </project>RedisSpringTest.java
public class RedisSpringTest { private static final Log log = LogFactory.getLog(RedisSpringTest.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml"); context.start(); ShardedJedisPool pool = (ShardedJedisPool) context.getBean("shardedJedisPool"); ShardedJedis jedis = pool.getResource(); String key = "redis"; String value = ""; jedis.del(key); // 删数据 jedis.set(key, "yixiaoqun"); // 存数据 value = jedis.get(key); // 取数据 log.info(key + "=" + value); jedis.set(key, "yixiaoqun2"); // 存数据 value = jedis.get(key); // 取数据 log.info(key + "=" + value); jedis.del(key); // 删数据 value = jedis.get(key); // 取数据 log.info(key + "=" + value); context.stop(); } catch (Exception e) { log.error("==>RedisSpringTest context start error:", e); System.exit(0); } finally { log.info("===>System.exit"); System.exit(0); } } }
相关文章推荐
- redis事务分析
- Redis and Python 实践之一
- 使用Redis分布式队列
- 监控redis进程,如果没有自动重启
- 搜狐视频Redis私有云平台CacheCloud
- Redis源码解析:13Redis中的事件驱动机制
- redis的学习和使用
- 一个简单的log4j2的redis appender ,依赖于Jedis (A simple log4j2 redis appender which uses Jedis)
- Redis 的性能幻想与残酷现实(转)
- redis
- Yii2框架与MongoDB拓展、Redis拓展的安装流程
- 每天laravel-20160625|RedisStore
- 每天laravel-20160625|RedisStore
- redis集群
- Redis 的性能幻想与残酷现实
- 四、Redis常用命令
- 安装xampp时提示找不到文件“-n”,提示没有安装Microsoft Visual C++ 2008 Redistributable Package (x86)
- Python—redis
- Python—操作redis
- redis 3.0尝鲜