redis jar包_「建议收藏」手把手教你从零搭建一个redis服务

b9dae5c261c79137989849619e44e49d.png

前言

自己在搭建redis服务的时候碰到一些问题,好多人只告诉你怎么成功搭建,但是并没有整理过程中遇到的问题,所有楼主就花了点时间来整理下。

  • linux环境安装redis
  • 安装中的碰到的问题和解决办法
  • 怎么在代码中使用安装的redis
  • 设置用户名和密码
  • 程序应用中碰到的问题

介绍

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。

一、安装redis

1、下载安装包

cd /www/redis/wget http://download.redis.io/releases/redis-4.0.8.tar.gztar -zxvf redis-4.0.8.tar.gzmv redis-4.0.8 redis

2、编译redis

cd /www/redis/redis/make MALLOC=libcmake PREFIX=/usr/local/redis install

3、准备配置文件

cd /usr/local/redismkdir confcd conf/vi redis_6379.conf

配置文件内容如下:

bind 127.0.0.1protected-mode noport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /www/redis/data/redis/6379/redis_6379.pidloglevel noticelogfile "/www/redis/data/redis/6379/log.log"databases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /www/redis/data/redis/6379/slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noslave-lazy-flush noappendonly yesappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble nolua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64

4、启动服务

mkdir -p /www/redis/data/redis/6379/cd ../bin/./redis-server ../conf/redis_6379.conf

5、使用客户端链接

./redis-cli

判断是否启动成功

0464288ecd6f7a3684b28e242e63e0d3.png

6、使用

查看数据

keys *

设置值

 set oneKey 测试

获取值

get oneKey
3b20c663a3c360a1ca9d6852fa185e1a.png

二、安装中的碰到的问题和解决办法

问题一:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
699c464415a36d3fb3993b6181c22c27.png

临时解决办法:

echo 511 > /proc/sys/net/core/somaxconn

永久解决办法

vi /etc/sysctl.conf
ad6ce21205f4dfe12f56a34d9d98b016.png

在里面添加net.core.somaxconn= 1024 然后执行sysctl -p 就可以永久消除这个warning

d484f0530f2f40f1ca30e5aa4ddfa1ca.png

问题二:

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
814e62280ef2c8d296dcf8cad612d5ec.png

可以参考问题一的解决

问题三:

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled
4c17d94661793f68b4ad5e8d5309ab67.png

执行命令echo never>/sys/kernel/mm/transparent hugepage/enabled

永久解决添加配置文件即可

vi /etc/rc.local
cd7393e53755ca5314f27802c8e880ae.png

三、怎么在代码中使用安装的redis呢

需要引用的jar包有

commons-pool-1.6.jarjedis-2.9.0.jar

示例代码

 public static void main(String[] args) { //创建redis对象 String ip = ""; Jedis jedis=new Jedis(ip,6379);//链接redis //记录操作个数 jedis.set("name
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值