From zero to build a Redis service

Foreword

Own encounter some problems when building redis services, a lot of people just tell you how to build a successful, but there is no finishing problems encountered in the process, all the landlord spent a little time to organize the next.

  • linux environment, install redis
  • Installation problems encountered and solutions
  • How to use the installed code redis
  • Username and password
  • Problems encountered in the application program

Introduction

redis is a key-value storage system. And Memcached Similarly, it supports relatively more stored value type, comprising a string (string), List (list), SET (set), zset (sorted set - ordered set) and hash (hash type). These data types are supported push / pop, add / remove and on the intersection and union, and difference richer operation, and these operations are atomic. On this basis, redis support a variety of different ways of sorting. Like with memcached, in order to ensure efficiency, the data is cached in memory. Redis difference is periodically updated in the data written to disk or to modify the operation of writing additional log file, and on this basis realize the master-slave (master and slave) synchronization.

Redis is a key-value high-performance database. redis appears, to a large extent compensate for the lack of such memcached key / value store, it can play a very good complement to relational database in some situations. It provides Java, C / C ++, C #, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients, very easy to use.

First, install redis

1, download the installation package

  1. cd /www/redis/ 
  2. wget http://download.redis.io/releases/redis-4.0.8.tar.gz 
  3. takes -zxvf Redis-4.0.8.tar.gz 
  4. mv redis-4.0.8 redis 

2, compile redis

  1. cd /www/redis/redis/ 
  2. make MALLOC=libc 
  3. make PREFIX=/usr/local/redis install 

3, prepare profiles

  1. cd /usr/local/redis 
  2. mkdir conf 
  3. conf cd / 
  4. force redis_6379.conf 

Configuration file as follows:

  1. bind 127.0.0.1 
  2. protected-mode no 
  3. port 6379 
  4. tcp-backlog 511 
  5. timeout 0 
  6. tcp-keepalive 300 
  7. daemonize yes 
  8. supervised no 
  9. pidfile /www/redis/data/redis/6379/redis_6379.pid 
  10. loglevel notice 
  11. logfile "/www/redis/data/redis/6379/log.log" 
  12. databases 16 
  13. always-show-logo yes 
  14. save 900 1 
  15. save 300 10 
  16. save 60 10000 
  17. stop-writes-on-bgsave-error yes 
  18. rdbcompression yes 
  19. rdbchecksum yes 
  20. dbfilename dump.rdb 
  21. dir /www/redis/data/redis/6379/ 
  22. slave-serve-stale-data yes 
  23. slave-read-only yes 
  24. repl-diskless-sync no 
  25. repl-diskless-sync-delay 5 
  26. repl-disable-tcp-nodelay no 
  27. slave-priority 100 
  28. lazyfree-lazy-eviction no 
  29. lazyfree-lazy-expire no 
  30. lazyfree-lazy-server-del no 
  31. slave-lazy-flush no 
  32. appendonly yes 
  33. appendfilename "appendonly.aof" 
  34. appendfsync everysec 
  35. no-appendfsync-on-rewrite no 
  36. auto-aof-rewrite-percentage 100 
  37. auto-aof-rewrite-min-size 64mb 
  38. aof-load-truncated yes 
  39. aof-use-rdb-preamble no 
  40. lua- time-limit 5000 
  41. slowlog-log-slower-than 10000 
  42. slowlog- max-only 128 
  43. latency-monitor-threshold 0 
  44. notify-keyspace-events "" 
  45. hash-max-ziplist-entries 512 
  46. hash-max-ziplist-value 64 

4, start the service

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

5, using the client link

  1. ./redis-cli 

Determine whether a successful start

"Proposed Collection" taught you how to build a service from zero redis

6,

View data

  1. keys * 

Settings

  1. set oneKey test 

Get the value

  1. get oneKey 
"Proposed Collection" taught you how to build a service from zero redis

Second, the problems encountered and installation solutions

Question one:

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

Temporary solution:

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

Permanent solution

  1. we /etc/sysctl.conf 
"Proposed Collection" taught you how to build a service from zero redis

Add net.core.somaxconn = 1024 in which then execute sysctl -p to permanently eliminate this warning

"Proposed Collection" taught you how to build a service from zero redis

Question two:

  1. 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. 
"Proposed Collection" taught you how to build a service from zero redis

可以参考问题一的解决

问题三:

  1. 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 
"Proposed Collection" taught you how to build a service from zero redis

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

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

  1. vi /etc/rc.local 

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

需要引用的jar包有

  1. commons-pool-1.6.jar 
  2. jedis-2.9.0.jar 

示例代码

  1. public static void main(String[] args) { 
  2.  //创建redis对象 
  3.  String ip = ""; 
  4.  Jedis jedis=new Jedis(ip,6379);//链接redis 
  5.  //记录操作个数 
  6.  jedis.set("name", "小明"); 
  7.  System.out.println("name已经赋值"); 
  8.  String name = jedis.get("name"); 
  9.  System.out.println("赋值后获取name的值为:"+name); 
  10.  jedis.del("name"); 
  11.  System.out.println("name已经删除"); 
  12.  String nameT = jedis.get("name"); 
  13.  System.out.println("删除后获取name的值为:"+nameT);  
  14.  } 
  15. //结果 
  16. name已经赋值 
  17. 赋值后获取name的值为:小明 
  18. name已经删除 
  19. 删除后获取name的值为:null 

四、设置用户名和密码

1、在配置文件中redis_6379.conf直接添加requirepass 123456

2、通过命令添加

设置密码

  1. #设置密码 
  2. config set requirepass 123456 
"Proposed Collection" taught you how to build a service from zero redis

查看密码

  1. config get requirepass 

需要验证密码以后才可以查看

"Proposed Collection" taught you how to build a service from zero redis

测试代码

  1.  public static void main(String[] args) { 
  2.  //创建redis对象 
  3.  String ip = ""; 
  4.  Jedis jedis=new Jedis(ip,6379);//链接redis 
  5.  jedis.auth("123456"); 
  6.  //记录操作个数 
  7.  jedis.set("name", "小明"); 
  8.  System.out.println("name已经赋值"); 
  9.  String name = jedis.get("name"); 
  10.  System.out.println("赋值后获取name的值为:"+name); 
  11.  jedis.del("name"); 
  12.  System.out.println("name已经删除"); 
  13.  String nameT = jedis.get("name"); 
  14.  . System out.println ( "get name after deleting the value:" + nameT); 
  15.   
  16.  } 
  17.  #result 
  18. name has been assigned 
  19. After obtaining the assignment name values are: Xiao Ming 
  20. name has been deleted 
  21. After obtaining delete name value: null 

Fifth, the application encountered a problem

Link is denied or times out problem

"Proposed Collection" taught you how to build a service from zero redis

ip redis beginning to configure the default ip and port 127.0.0.1:6379, the ip local services can only be linked. Solution:

In the configuration file this ip to comment

"Proposed Collection" taught you how to build a service from zero redis

Configuring security group in the cloud Ali

After modifying the configuration files and security groups, you can access the external network ip and port redis by Ali cloud services.

But then, also reported a problem, denying access to protected mode

"Proposed Collection" taught you how to build a service from zero redis

Follow the prompts to modify the configuration file redis_6379.conf the property protected-mode no, and restart the service

Test code again connected properly ~

【Editor's Choice】

Guess you like

Origin www.cnblogs.com/gucb/p/11411049.html