• Redis(三)


    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微服务项目作为一个简单的例子

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>io.projectreactor</groupId>
                <artifactId>reactor-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>3.2.0</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    package com.example.demo;
    
    import redis.clients.jedis.Jedis;
    
    public class Test {
        /**
         * 测试java连接redis
         */
        public static void main(String[] args)
        {
            Jedis jedis=new Jedis("127.0.0.1",6379);
            String string =jedis.ping();
            System.out.println(string);
            jedis.set("a","b");
            jedis.get("a");
            jedis.close();
    
        }
    }

    注意:我这里为了测试连接是本机,如果需要连接服务器,需要注掉redis.conf里里面的bind选择,同时设置protected-mode的值为no

  • 相关阅读:
    模板代码生成器 Template Code Creater
    Oracle编程入门经典 第2章 SQLPlus和基本查询
    Oracle编程入门经典 第5章 体系结构
    数据仓库
    C++ WINDOWS API 第1章 Windows 应用程序开发入门
    C++ WINDOWS API 第2章 Windows API概要
    Oracle编程入门经典 第7章 表
    单交换机VLAN虚拟局域网划分
    Oracle日志文件被误删除
    Oracle编程入门经典 第4章 新9i示例模式
  • 原文地址:https://www.cnblogs.com/qyx66/p/12185106.html
Copyright © 2020-2023  润新知