redis问题笔记

一、redis启动问题

1、启动后在日志文件中发现如下警告:

a.服务器分配内存太小了,将其修改为511以上即可,其实也可以不用管。

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

解决方案:修改系统配置文件sysctl.conf;方式如下:

vim /etc/sysctl.conf
在这个文件中添加 net.core.somaxconn= 1024
然后执行:sysctl -p  #使配置文件生效

b、内存分配策略问题,这个警告也可以不用管

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.

解决方案:同上面的步骤修改sysctl.conf文件,在文件中添加如下配置即可:

vm.overcommit_memory=1
它是 内存分配策略
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

************************END*************************************


二、redis使用Java操作时遇到的问题

1.使用spring注入jedisPool时,在获取jedis时报

redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool异常

这个可能是配置testOnBorrow做链接可用性检查,且redis配置了权限认证。则在注入jedisPool时必须将密码设置进去。
具体spring配置文件入下

<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!-- 最大连接数 -->
    <property name="maxTotal" value="10" />
    <!-- 最大空闲连接数 -->
    <property name="maxIdle" value="5" />
    <!-- 每次释放连接的最大数目 -->
    <property name="numTestsPerEvictionRun" value="1024" />
    <!-- 释放连接的扫描间隔(毫秒) -->
    <property name="timeBetweenEvictionRunsMillis" value="30000" />
    <!-- 连接最小空闲时间 -->
    <property name="minEvictableIdleTimeMillis" value="1800000" />
    <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
    <property name="softMinEvictableIdleTimeMillis" value="10000" />
    <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
    <property name="maxWaitMillis" value="1500" />
    <!-- 在获取连接的时候检查有效, 默认false,设置为true时则获取到的jedis一定是可用的 -->
    <property name="testOnBorrow" value="true" />
    <!-- 在空闲时检查有效性, 默认false -->
    <property name="testWhileIdle" value="true" />
    <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
    <property name="blockWhenExhausted" value="false" />
</bean>
 <!-- redis单机 通过连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
    <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
    <constructor-arg name="host" value="192.168.0.100"/>
    <constructor-arg name="port" value="6379"/>
    <!--若干testOnBorrow的参数为true,且redis设置了权限认证,那么这里一定要使用这个构造方法-->
    <constructor-arg name="password" value="aishfiawefnaw"/>
    <constructor-arg name="timeout" value="5000"/>
</bean>

Java测试

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis 和spring 整合测试
 * Created by vf on 2017/9/20.
 */
public class RedisSpringTest {

    private ApplicationContext applicationContext;

    @Before
    public void setUp(){
        String configLocation = "classpath*:spring/applicationContext-redis.xml";
        applicationContext = new ClassPathXmlApplicationContext(configLocation);
    }

    @Test
    public void JedisSpringTest() throws Exception{
        JedisPool jedisPool = (JedisPool)applicationContext.getBean("jedisPool");
        Jedis jedis = jedisPool.getResource();
        System.out.println( jedis.get("tmp"));
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,067评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,977评论 6 342
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,385评论 11 349
  • 5月,多么美好的季节!一边是各种成熟的收获,如昨天难得看到一片麦子,黄黄的,熟了!还有,油菜籽也是黄了,正是收割的...
    天使小鱼儿阅读 392评论 0 2
  • 你说太忙 本该多陪你溜达溜达 我的傻瓜 只要是你 哪怕是陪在我身边一秒 我就开心好久 如果能恰好途径一朵花的盛放 ...
    天堂小贼阅读 428评论 0 1