Redis相关配置
ip地址的绑定(bind)
默认情况下bind=127.0.0.1只能接受本机的访问请求
不写的情况下,无限制接受任何ip地址的访问
生产环境环境肯定要写你应用服务器的地址
如果开启了protected-mode,那么在没有设定bind ip且没有设密码的情况下,Redis只允许接受本机的响应
tcp-backlog
可以理解成一个请求到达后至到接受进程处理前的队列里所允许存在的请求个数
backlog队列总和=未完成三次握手队列+已经完成三次握手队列
高并发环境tcp_backlog设置值跟超时时限内的Redis吞吐量决定
timeout
一个空闲的客户端维持多少秒会自动关闭,0为永不关闭
TCP keepalive
对访问客户端的一种心跳检测,每隔n秒检测一次,官方推荐设为60s
daemonize
是否为后台进程
pidfile (pid是进程号)
存放在pid文件的位置,每个实例都会产生一个不同的pid文件
log level
四个级别根据使用阶段来选择,生产环境选择notice或者warning
logfile
日志文件名称
syslog
是否将Redis日志输送到inux系统日志服务中
syslog-ident
日志的标志
syslog-facility
输出日志的设备
database
设定库的数量
security
在命令行中设置密码
例:
注意都是临时密码,因为在指令中的命令只在内存中有效
config get requirepass 获取密码
config set requirepass "123456" 设置密码
auth 123456 //登录
如果设置永久密码可在redis.conf文件中找到requirepass选择,在其后面添加密码即可
maxclient
最大客户端连接数
maxmemory
设置Redis可以使用的内存量,一旦到达内存使用上限,Redis将会试图移除内部数据,移动规则可以通过maxmemory-policy来指定,如果Redis无法根据移除规则来移除内存中的数据,或者设置了“不允许移除”
那么redis则会针对那些需要申请内存的指令返回错误信息,比如SET、LPUSH
Maxmemory-policy
volatile-lru 使用LRU(最近最少使用)算法移除key,只对设置了过期时间的键
allkeys-lru :使用LUR算法移除key
volatitle-random 在过期集合中移除随机的key,只对设置了过期时间的键
allkeys-random:移动随机的key
volatitle-ttl(即将过期):移除那么TTL值最小的key,即那么些最近要过期的key
noeviction 不进行移除,针对写操作,只是返回错误信息
Maxmemory-samples
设置样本数量,LRU算法和最小TTL算法都并非是精确的算法,而是估算值,所有你可设置样本的大小,一般设置3到7的数字,数值越小样本越不准确,但是性能消耗也越大
java连接Redis,我这里是以spring boot微服务项目作为一个简单的例子
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.2.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>demo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-data-redis-reactive</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-test</artifactId> 34 <scope>test</scope> 35 <exclusions> 36 <exclusion> 37 <groupId>org.junit.vintage</groupId> 38 <artifactId>junit-vintage-engine</artifactId> 39 </exclusion> 40 </exclusions> 41 </dependency> 42 <dependency> 43 <groupId>io.projectreactor</groupId> 44 <artifactId>reactor-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> 48 <dependency> 49 <groupId>redis.clients</groupId> 50 <artifactId>jedis</artifactId> 51 <version>3.2.0</version> 52 </dependency> 53 54 </dependencies> 55 56 <build> 57 <plugins> 58 <plugin> 59 <groupId>org.springframework.boot</groupId> 60 <artifactId>spring-boot-maven-plugin</artifactId> 61 </plugin> 62 </plugins> 63 </build> 64 65</project> 66 67package com.example.demo; 68 69import redis.clients.jedis.Jedis; 70 71public class Test { 72 /** 73 * 测试java连接redis 74 */ 75 public static void main(String[] args) 76 { 77 Jedis jedis=new Jedis("127.0.0.1",6379); 78 String string =jedis.ping(); 79 System.out.println(string); 80 jedis.set("a","b"); 81 jedis.get("a"); 82 jedis.close(); 83 84 } 85}
注意:我这里为了测试连接是本机,如果需要连接服务器,需要注掉redis.conf里里面的bind选择,同时设置protected-mode的值为no