Redis单机实例搭建
2016-12-11 14:11
281 查看
搭建环境:
环境: CentOS 7.0 使用IPtable防火墙
Redis 版本: redis-3.2.6 安装源码包(目前最新稳定版本)
安装目录: /usr/local/redis
用户: root
Redis优点:
异常快速 : Redis是非常快的,每秒可以执行大约110000设置操作,81000个/每秒的读取操作。
支持丰富的数据类型 : Redis支持最大多数开发人员已经知道如列表,集合,可排序集合,哈希等数据类型。
这使得在应用中很容易解决的各种问题,因为我们知道哪些问题处理使用哪种数据类型更好解决。
操作都是原子的 : 所有 Redis 的操作都是原子,从而确保当两个客户同时访问 Redis 服务器得到的是更新后的值(最新值)。
MultiUtility工具:Redis是一个多功能实用工具,可以在很多如:缓存,消息传递队列中使用(Redis原生支持发布/订阅),在应用程序中,如:Web应用程序会话,网站页面点击数等任何短暂的数据;
安装步骤:
下载redis3.6.tar.gz源码包,并安装到指定目录
将 Redis 配置成服务:
按上面的操作步骤, Redis 的启动脚本为: /usr/local/src/redis3.6/utils/redis_init_script
将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为 redis.
修改redis配置文件:
以上配置操作完成后, 便可将 Redis 注册成为服务:
修改 redis 配置文件设置:
默认情况下, Redis 开启安全认证,可以通过/usr/local/redis/conf/6379.conf 的 requirepass 指定一个
验证密码。参考博客
注意事项:
redis安全配置只可以本机访问(可参考 Redis官网安全介绍),如果需要公网IP访问可以修改如下配置,注意配置公网可以访问最后加上密码进行授权。
Java推荐使用 Jedis 连接服务器,spring-redis.xml 配置如下:
Jedis简单使用实例:
环境: CentOS 7.0 使用IPtable防火墙
Redis 版本: redis-3.2.6 安装源码包(目前最新稳定版本)
安装目录: /usr/local/redis
用户: root
Redis优点:
异常快速 : Redis是非常快的,每秒可以执行大约110000设置操作,81000个/每秒的读取操作。
支持丰富的数据类型 : Redis支持最大多数开发人员已经知道如列表,集合,可排序集合,哈希等数据类型。
这使得在应用中很容易解决的各种问题,因为我们知道哪些问题处理使用哪种数据类型更好解决。
操作都是原子的 : 所有 Redis 的操作都是原子,从而确保当两个客户同时访问 Redis 服务器得到的是更新后的值(最新值)。
MultiUtility工具:Redis是一个多功能实用工具,可以在很多如:缓存,消息传递队列中使用(Redis原生支持发布/订阅),在应用程序中,如:Web应用程序会话,网站页面点击数等任何短暂的数据;
安装步骤:
下载redis3.6.tar.gz源码包,并安装到指定目录
//编译和安装所需的包: # yum install gcc tcl //下载redis-3.2.6 版本,并上传到 /usr/local/src目录 # cd /usr/local/src //创建安装目录: # mkdir /usr/local/redis //解压: # tar -zxvf redis3.6.tar.gz # cd redis3.6 //安装(使用 PREFIX 指定安装目录): # make PREFIX=/usr/local/redis install //安装完成后, 可以看到/usr/local/redis 目录下有一个 bin 目录, bin 目录里就是 redis 的命令脚本
将 Redis 配置成服务:
按上面的操作步骤, Redis 的启动脚本为: /usr/local/src/redis3.6/utils/redis_init_script
将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为 redis.
//编辑/etc/rc.d/init.d/redis, 修改相应配置,使之能注册成为服务: # cp /usr/local/src/redis3.6/utils/redis_init_script /etc/rc.d/init.d/redis # vim /etc/rc.d/init.d/redis
修改redis配置文件:
(1) 在脚本的第一行后面添加一行内容如下: #chkconfig: 2345 80 90 (如果不添加上面的内容,在注册服务时会提示: service redis does not support chkconfig) (2) REDISPORT 端口保持 6379 不变; (注意,端口名将与下面的配置文件名有关) (3) EXEC=/usr/local/bin/redis-server 改为 EXEC=/usr/local/redis/bin/redis-server (4) CLIEXEC=/usr/local/bin/redis-cli 改为 CLIEXEC=/usr/local/redis/bin/redis-cli (5) 配置文件设置: 创建 redis 配置文件目录 # mkdir /usr/local/redis/conf 复制 redis 配置文件/usr/local/src/redis3.6/redis.conf到/usr/local/redis/conf 目录并按端口 号重命名为 6379.conf # cp /usr/local/src/redis3.6/redis.conf /usr/local/redis/conf/6379.conf 做了以上准备后,再对 CONF 属性作如下调整: CONF="/etc/redis/${REDISPORT}.conf" 改为 CONF="/usr/local/redis/conf/${REDISPORT}.conf" (6) 更改 redis 开启的命令,以后台运行的方式执行: $EXEC $CONF & #“ &”作用是将服务转到后面运行 修改文件如图所示: 修改后的/etc/rc.d/init.d/redis 服务脚本内容为: #!/bin/sh #chkconfig: 2345 80 90 # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server CLIEXEC=/usr/local/redis/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/usr/local/redis/conf/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF & fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ... sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
以上配置操作完成后, 便可将 Redis 注册成为服务:
//注册服务 # chkconfig --add redis //防火墙中打开对应的端口,注意云服务器要同步打开安全组端口 # vi /etc/sysconfig/iptables //添加: -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT //重启防火墙: # service iptables restart
修改 redis 配置文件设置:
//修改如下配置 //改为 yes ,指定redis转为后台服务运行 daemonize no 改为> daemonize yes //redis启动生成PID位置 ,注意要和上面 redis配置文件 pidfile 4000 位置一致,否则将无法关闭redis服务。 pidfile /var/run/redis.pid 改为> pidfile /var/run/redis_6379.pid //启动 Redis 服务 # service redis start 将 Redis 添加到环境变量中: # vi /etc/profile 在最后添加以下内容: ## Redis env export PATH=$PATH:/usr/local/redis/bin 使配置生效: # source /etc/profile
默认情况下, Redis 开启安全认证,可以通过/usr/local/redis/conf/6379.conf 的 requirepass 指定一个
验证密码。参考博客
注意事项:
redis安全配置只可以本机访问(可参考 Redis官网安全介绍),如果需要公网IP访问可以修改如下配置,注意配置公网可以访问最后加上密码进行授权。
# By default, if no "bind" configuration directive is specified, Redis listens # for connections from all the network interfaces available on the server. # It is possible to listen to just one or multiple selected interfaces using # the "bind" configuration directive, followed by one or more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //注释掉 bind 127.0.0.1 本地绑定,默认是打开的 #bind 127.0.0.1 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # When protected mode is on and if: # # 1) The server is not binding explicitly to a set of addresses using the # "bind" directive. # 2) No password is configured. # # The server only accepts connections from clients connecting from the # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain # sockets. # # By default protected mode is enabled. You should disable it only if # you are sure you want clients from other hosts to connect to Redis # even if no authentication is configured, nor a specific set of interfaces # are explicitly listed using the "bind" directive. //关闭保护模式 默认是 yes protected-mode no
Java推荐使用 Jedis 连接服务器,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="115.159.109.33" /> <constructor-arg index="1" value="6379" type="int" /> </bean> </list> </constructor-arg> </bean> </beans>
Jedis简单使用实例:
package wusc.edu.demo.redis; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; /** * * @描述: Redis测试 . * @创建时间: 2015-3-23,上午1:30:40 . * @版本号: V1.0 . */ 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 = "wusc"; String value = ""; jedis.del(key); // 删数据 jedis.set(key, "keyValue"); // 存数据 value = jedis.get(key); // 取数据 log.info(key + "=" + value); jedis.set(key, "keyValue2"); // 存数据 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); } } }
相关文章推荐
- CentOS 7 搭建 Redis 集群 - 单机多实例
- pringmvc整合redis架构搭建实例
- redis单机及其集群的搭建
- Redis3.2.1分布式集群搭建(非单机版本)
- redis单机及其集群的搭建
- redis单机单实例一键安装脚本 推荐
- Redis的make,make test,make install、单机多实例配置,以及API程序写数据!
- win7 linux 搭建redis单机环境 详细
- redis单机搭建--详细
- Redis探索之旅(2)- 在Windows下搭建单实例Redis服务器
- 在一台机器上搭建多个redis实例
- Spring+Shiro搭建基于Redis的分布式权限系统(有实例)
- Redis配置文件解析+单机多实例(主从)配置
- ubuntu 14.04 Spark单机环境搭建与实例使用
- springmvc整合redis架构搭建实例
- redis实现单机运行多端口、多实例
- springmvc整合redis架构搭建实例
- redis单机的搭建
- Redis探索之旅(4)- 在Linux环境下搭建单实例Redis服务实例