Springboot Redis分散クラスタ(4)-JedisCluster接続テストクラスタ
14938 ワード
Springboot Redis分散クラスタ(4)-JedisCluster接続テストクラスタ
@(Markdownブログ)
1.resources/application.propertiesにクラスタ接続パラメータを追加する
spring.redis.cluster.nodes=120.77.172.111:7000,120.77.172.111:7001,120.77.172.111:7002,120.77.172.111:7003,120.77.172.111:7004,120.77.172.111:7005
2.クラスタ構成情報を読み込むためのRedisClusterConfigクラスの作成
package me.cnlm.springboot.redis.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import java.util.*;
/**
* Created by [email protected] on 2017/7/21.
*/
@Configuration
public class RedisClusterConfig {
public static Set NODES;
@Value("${spring.redis.cluster.nodes}")
public void setNODES(String nodes) {
try {
Set hostAndPortSet = new LinkedHashSet();
String[] hostAndPorts = nodes.split(",");
for (String hap : hostAndPorts) {
String ip = hap.split(":")[0];
int port = Integer.parseInt(hap.split(":")[1]);
HostAndPort hostAndPort = new HostAndPort(ip, port);
hostAndPortSet.add(hostAndPort);
}
NODES = hostAndPortSet;
} catch (Exception e) {
System.out.println(" ");
throw e;
}
}
}
3.リモートredisクラスタ接続インスタンスを取得するためのJedisClusterFactoryクラスの作成
package me.cnlm.springboot.redis.redis;
import me.cnlm.springboot.redis.config.RedisClusterConfig;
import me.cnlm.springboot.redis.config.RedisConfig;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by [email protected] on 2017/7/22.
*/
public class JedisClusterFactory {
private static class RedisUtilHolder {
private static final JedisClusterFactory instance = new JedisClusterFactory();
}
public static JedisClusterFactory getInstance() {
return RedisUtilHolder.instance;
}
public JedisCluster getJedisCluster() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(RedisConfig.POOL_MAX_ACTIVE);
config.setMaxIdle(RedisConfig.POOL_MAX_IDLE);
config.setMaxWaitMillis(RedisConfig.POOL_MAX_WAIT);
config.setMinIdle(RedisConfig.POOL_MIN_IDLE);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
JedisCluster cluster = new JedisCluster(RedisClusterConfig.NODES, config);
// JedisCluster cluster = new JedisCluster(new HostAndPort("120.77.172.111",7000), config);
return cluster;
}
}
4.作成ユニットJedisClusterTestクラステストクラスタ機能
import me.cnlm.springboot.redis.Application;
import me.cnlm.springboot.redis.config.RedisClusterConfig;
import me.cnlm.springboot.redis.config.RedisConfig;
import me.cnlm.springboot.redis.redis.JedisClusterFactory;
import me.cnlm.springboot.redis.redis.JedisFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import java.util.HashMap;
import java.util.Map;
/**
* Created by [email protected] on 2017/7/21.
*/
@RunWith(SpringJUnit4ClassRunner.class) // SpringJUnit , Spring-Test !
@SpringBootTest(classes = Application.class) // SpringBoot Application ,1.5.4 SpringApplicationConfiguration
@WebAppConfiguration // Web ,Junit ServletContext, @WebAppConfiguration。
public class RedisClusterTest {
@Test
public void testRedis(){
JedisCluster cluster= JedisClusterFactory.getInstance().getJedisCluster();
cluster.set("name","cnlm.me");
cluster.set("email","[email protected]");
cluster.set("phone","13880**8929");
Map map=new HashMap();
map.put("year","2017");
map.put("month","07");
map.put("day","22");
cluster.hmset("map",map);
cluster.set("company","pax");
cluster.set("sex","male");
cluster.set("love","2010");
/*
System.out.println(cluster.get("name"));
System.out.println(cluster.get("email"));
System.out.println(cluster.get("phone"));
System.out.println(cluster.hgetAll("map"));
System.out.println(cluster.get("company"));
System.out.println(cluster.get("sex"));
System.out.println(cluster.get("love"));*/
}
}
ここで、ユニットテストを実行する前に、リモートサーバでmonitorコマンドを使用して、70007002の3つのmasterノードのデータストレージモニタリング初期化のモニタリング状態を観察します.
[root@cnlm redis]# redis-cli -c -h 127.0.0.1 -p 7000
127.0.0.1:7000> clear
127.0.0.1:7000> monitor
OK
[root@cnlm redis]# clear
[root@cnlm redis]# redis-cli -c -h 127.0.0.1 -p 7001
127.0.0.1:7001> monitor
OK
[root@cnlm redis]# clear
[root@cnlm redis]# redis-cli -c -h 127.0.0.1 -p 7002
127.0.0.1:7002> monitor
OK
1回目の実行ユニットはデータを格納するテストを行い、結果:
127.0.0.1:7001> monitor
OK
1500700354.296269 [0 125.82.190.120:20605] "PING"
1500700354.327038 [0 125.82.190.120:20605] "SET" "email" "cnlm.me@qq.com"
1500700354.357832 [0 125.82.190.120:20605] "PING"
127.0.0.1:7002> monitor
OK
1500700159.006154 [0 125.82.190.120:20447] "PING"
1500700354.192247 [0 125.82.190.120:20604] "SET" "name" "cnlm.me"
1500700354.228369 [0 125.82.190.120:20604] "PING
ここには2つのキー値ペア、nameとemailしか格納されていません.他のクライアントは格納時に接続タイムアウトを報告しています.この問題は、テストしたばかりの時に要求回数が多すぎてcluster接続を閉じることができなかったため、最大接続数を超えた可能性があると推測されています.その後の接続はタイムアウト異常を待っています.次のコードを追加すると、一時的にタイムアウト接続の問題は発生しません.
try{
cluster.close();
} catch (IOException e) {
e.printStackTrace();
}
5.疑問?
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
./redis-trib.rb create --replicas 1 172.18.153.216:7000 172.18.153.216:7001 172.18.153.216:7002 172.18.153.216:7003 172.18.153.216:7004 172.18.153.216:7005