Java调用Redis集群
前文
需要使用以下 jar 包
Maven 项目引用以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.26</version> <scope>test</scope> </dependency> |
代码
package Main;import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;@SuppressWarnings("all")
public class RedisMain {
public static void main(String[] args) {
JedisCluster cluster =null;
try {Set</span><HostAndPort> nodes = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashSet<HostAndPort><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一般选用slaveof从IP+端口进行增删改查,不用master</span> nodes.add(<span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("外网IP", 7003<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("外网", 7004<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("外网IP", 7004<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Jedis连接池配置</span> JedisPoolConfig jedisPoolConfig = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最大空闲连接数, 默认8个</span> jedisPoolConfig.setMaxIdle(100<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最大连接数, 默认8个</span> jedisPoolConfig.setMaxTotal(500<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最小空闲连接数, 默认0</span> jedisPoolConfig.setMinIdle(0<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1</span> jedisPoolConfig.setMaxWaitMillis(2000); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置2秒 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对拿到的connection进行validateObject校验</span> jedisPoolConfig.setTestOnBorrow(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">未设置auth Password</span> JedisCluster jedis = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisCluster(nodes, jedisPoolConfig); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置auth Password </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">JedisCluster jedis = new JedisCluster(nodes,5000,3000,10,{auth_password}, new JedisPoolConfig());</span> System.out.println(jedis.get("mykey"<span style="color: rgba(0, 0, 0, 1)">)); }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e) { e.printStackTrace(); }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)">cluster) cluster.close(); } }
}
可能出现的异常
1、DENIED Redis is running in protected mode because protected mode is enabled...
解决方法:redis.conf 默认禁止外网访问,修改”protected-mode yes”为“protected-mode no”
2、No more cluster attempts left.
解决方法:redis 设置集群时,服务器没有配置开启集群总线端口(redis 端口 +10000),如果 redis-cli 端口有 7000-7005,则集群总线端口为 17000-17005,服务器 7000-70005、17000-17005 端口都要打开
3、No reachable node in cluster
解决方法:查看 redis.conf 的 "bind xxxxxxx" 是否限制了 IP 访问,注销 bind 则可以任意 IP 访问服务器 Redis