暑假期间一直好奇关于通过java的redis整合包jar访问外网服务器上面的redis缓存,于是就尝试在外网的linux进行安装,鼓捣了有一天时间,大概终于弄好了。然后在eclipse上面写了一个测试demo,大致的代码如下:
public static boolean existsUserApp(String sessionKey) {
boolean flag = false;
Jedis redis = null;
try{
redis=RedisConfig.redisPool.getResource();
String key = "APP" + sessionKey;
if (redis.exists(key)) {
flag = true;
}
RedisConfig.redisPool.returnResource(redis);
}catch(Exception e){
if(redis!=null){
RedisConfig.redisPool.returnBrokenResource(redis);
flag=false;
}
}
return flag;
}
结果一直显示:链接失败,找到原因大概是服务器端没有开启外网访问。于是查找redis的redis.conf文件
bind的属性,有问题。
于是查找了下bind属性的文档文件:
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
大致的意思就是说:
1. 如果你在生产环境下的话,应该使用bind属性绑定你的被访问ip地址,确保安全。
2. 然后你如果运行环境下,需要将bind属性进行注释,然后redis默认可以接受所有的访问ip。
于是我就注释了,然后依旧有问题,发现linux的redis启动不了服务了。一直报这个错误:
Creating Server TCP listening socket *:6379: unable to bind socket
我一直郁闷,明明已经注释了,为啥还是不行。于是我翻墙出去找了,发现外国友人大致划分两种情况。
redis的版本问题,就是在低于3.0.1时并未产生这个错误,高于就是产生这个问题
问题解决方法
1. 将IPv6的网卡进行关闭(具体方法进行百度,这里不进行详细叙述)
2. 添加在redis.conf中添加 bind 0.0.0.0(博主就是添加这个解决了问题)
具体原因
- redis接受IPv4的访问模式,IPv6它不会接受(额,这些都是我的猜测)
- 至于加上bind 0.0.0.0 是可以得,我现在还不怎么知道,请明白的小伙伴私信我,谢谢了。