本文整理匯總了Java中redis.clients.jedis.HostAndPort類的典型用法代碼示例。如果您正苦於以下問題:Java HostAndPort類的具體用法?Java HostAndPort怎麽用?Java HostAndPort使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HostAndPort類屬於redis.clients.jedis包,在下文中一共展示了HostAndPort類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getJedisCluster
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
/**
* 注意:
* 這裏返回的JedisCluster是單例的,並且可以直接注入到其他類中去使用
* @return
*/
@Bean
public JedisCluster getJedisCluster() {
//獲取服務器數組(這裏要相信自己的輸入,所以沒有考慮空指針問題)
String[] serverArray = redisProperties.getClusterNodes().split(",");
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(nodes,10000,
1000,1,redisProperties.getPassword() ,
new GenericObjectPoolConfig());
}
示例2: getResource
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
@Override
public Jedis getResource() {
while (true) {
Jedis jedis = super.getResource();
jedis.setDataSource(this);
// get a reference because it can change concurrently
final HostAndPort master = currentHostMaster;
final HostAndPort connection = new HostAndPort(jedis.getClient().getHost(), jedis.getClient()
.getPort());
if (master.equals(connection)) {
// connected to the correct master
return jedis;
} else {
returnBrokenResource(jedis);
}
}
}
示例3: forceFailover
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
private void forceFailover(JedisSentinelPool pool) throws InterruptedException {
HostAndPort oldMaster = pool.getCurrentHostMaster();
// jedis connection should be master
Jedis beforeFailoverJedis = pool.getResource();
assertEquals("PONG", beforeFailoverJedis.ping());
waitForFailover(pool, oldMaster);
Jedis afterFailoverJedis = pool.getResource();
assertEquals("PONG", afterFailoverJedis.ping());
assertEquals("foobared", afterFailoverJedis.configGet("requirepass").get(1));
assertEquals(2, afterFailoverJedis.getDB().intValue());
// returning both connections to the pool should not throw
beforeFailoverJedis.close();
afterFailoverJedis.close();
}
示例4: testCalculateConnectionPerSlot
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
@Test
public void testCalculateConnectionPerSlot() {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
jc.set("foo", "bar");
jc.set("test", "test");
assertEquals("bar", node3.get("foo"));
assertEquals("test", node2.get("test"));
JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 7379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
jc2.set("foo", "bar");
jc2.set("test", "test");
assertEquals("bar", node3.get("foo"));
assertEquals("test", node2.get("test"));
}
示例5: testStableSlotWhenMigratingNodeOrImportingNodeIsNotSpecified
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
@Test
public void testStableSlotWhenMigratingNodeOrImportingNodeIsNotSpecified()
throws InterruptedException {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort()));
JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
int slot51 = JedisClusterCRC16.getSlot("51");
jc.set("51", "foo");
// node2 is responsible of taking care of slot51 (7186)
node3.clusterSetSlotImporting(slot51, JedisClusterTestUtil.getNodeId(node2.clusterNodes()));
assertEquals("foo", jc.get("51"));
node3.clusterSetSlotStable(slot51);
assertEquals("foo", jc.get("51"));
node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes()));
// assertEquals("foo", jc.get("51")); // it leads Max Redirections
node2.clusterSetSlotStable(slot51);
assertEquals("foo", jc.get("51"));
}
示例6: getJedisCluster
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
@Bean//bean
public JedisCluster getJedisCluster() {
//解析字符串
Set<HostAndPort> nodes = new HashSet<>();
try {
String[] cNodes = clusterNodes.split(",");
for (String cNode : cNodes) {
//192.168.123.1:7001
String[] hp = cNode.split(":");
nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
}
return new JedisCluster(nodes);
} catch (ArrayIndexOutOfBoundsException | PatternSyntaxException | NullPointerException e) {
e.printStackTrace();
}
return null;
}
示例7: init
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
/**
* This method is called by PDI during transformation startup.
*
* @param smi step meta interface implementation, containing the step settings
* @param sdi step data interface implementation, used to store runtime information
*
* @return true if initialization completed successfully, false if there was an error preventing the step from working.
*
*/
public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
// Casting to step-specific implementation classes is safe
RedisOutputStepMeta meta = (RedisOutputStepMeta) smi;
RedisOutputStepData data = (RedisOutputStepData) sdi;
String url = environmentSubstitute(meta.getUrl());
logBasic("creating redis session factory, addresses=" + url);
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("redis cluster url set configured");
}
String[] redisNodes = url.split(",");
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
try {
for (String redisNode: redisNodes) {
String[] config = redisNode.split(":");
jedisClusterNodes.add(new HostAndPort(config[0], Integer.parseInt(config[1])));
}
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("redis cluster url not configured correctly");
}
client = new JedisCluster(jedisClusterNodes, REDIS_TIMEOUT, new GenericObjectPoolConfig());
return super.init(meta, data);
}
示例8: getJedisCluster
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
@Bean
public JedisCluster getJedisCluster() {
List<String> serverArray = redisProperties.getNodes();
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(1000 * 60);
config.setMaxWaitMillis(1000 * 10);
config.setTestOnBorrow(true);
return new JedisCluster(nodes, config);
}
示例9: getJedisNodes
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
/**
* method to get jedis nodes
*
* @param hosts
* @param clusterEnabled
* @return
*/
private Collection<? extends Serializable> getJedisNodes(String hosts, boolean clusterEnabled) {
hosts = hosts.replaceAll("\\s", "");
String[] hostPorts = hosts.split(",");
List<String> node = null;
Set<HostAndPort> nodes = null;
for (String hostPort : hostPorts) {
String[] hostPortArr = hostPort.split(":");
if (clusterEnabled) {
nodes = (nodes == null) ? new HashSet<HostAndPort>() : nodes;
nodes.add(new HostAndPort(hostPortArr[0], Integer.valueOf(hostPortArr[1])));
} else {
int port = Integer.valueOf(hostPortArr[1]);
if (!hostPortArr[0].isEmpty() && port > 0) {
node = (node == null) ? new ArrayList<String>() : node;
node.add(hostPortArr[0]);
node.add(String.valueOf(port));
break;
}
}
}
return clusterEnabled ? nodes : node;
}
示例10: exist
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
private ScanPage exist(String cluster, ScanPage scanPage) throws Exception {
scanPage.setHasMore(false);
List<D_RedisClusterNode> nodes = clusterNodeService.getAllClusterNodes(cluster);
Set<HostAndPort> masters = new HashSet<HostAndPort>();
nodes.forEach(node->{
masters.add(new HostAndPort(node.getHost(), node.getPort()));
});
JedisCluster jedis = new JedisCluster(masters);
try {
if(jedis.exists(scanPage.getQuery())){
scanPage.setKeys(new HashSet<String>());
scanPage.getKeys().add(scanPage.getQuery());
}
} finally {
jedis.close();
}
return scanPage;
}
示例11: query
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
public ScanPage query(String cluster, ScanPage scanPage) throws Exception {
D_ClusterNode_Tree tree = clusterNodeService.getClusterTree(cluster);
Set<HostAndPort> masters = new HashSet<HostAndPort>();
for (D_ClusterNode_Master nodes : tree.getMasters()) {//每一個分片獲取一個節點
D_RedisClusterNode node = nodes.getMaster();
if(node.getStatus() == RedisNodeStatus.CONNECT){
masters.add(new HostAndPort(node.getHost(), node.getPort()));
}else{
for (D_RedisClusterNode slave : nodes.getSlaves()) {
if(slave.getStatus() == RedisNodeStatus.CONNECT){
masters.add(new HostAndPort(slave.getHost(), slave.getPort()));
break;
}
}
}
}
RedisClusterScan scan = new RedisClusterScan(masters);
scanPage.setKeys(null);
return scan.scan(scanPage);
}
示例12: M_ClusterSlots
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public M_ClusterSlots(List<Object> clusterSlot, Map<HostAndPort, M_clusterNode> hpNodes){
hpSlots = new HashMap<HostAndPort, List<Slot>>();
idSlots = new HashMap<M_clusterNode, List<Slot>>();
for (Object object : clusterSlot) {
List<Object> tmp = (List<Object>) object;
Long start = (Long) tmp.get(0);
Long end = (Long) tmp.get(1);
for (int i = 2; i < tmp.size(); i++) {
List<Object> nodeSlot = (List<Object>)tmp.get(i);
HostAndPort hp = new HostAndPort(new String((byte[]) nodeSlot.get(0)), ((Long)nodeSlot.get(1)).intValue());
M_clusterNode node = hpNodes.get(hp);
List<Slot> slot;
if(!hpSlots.containsKey(hp)){
slot = new ArrayList<Slot>();
}else{
slot = hpSlots.get(hp);
}
slot.add(new Slot(start, end));
hpSlots.put(hp, slot);
idSlots.put(node, slot);
}
}
}
示例13: assignSlot
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
/**
* 分配槽
*/
public static void assignSlot(List<HostAndPort> masters, Map<HostAndPort, RedisClusterTerminal> clients, Notify notify) {
message(notify, ">> AssignSlot Start ... ");
int slotSize = 16384 / masters.size();
for (int i = 0; i < masters.size(); i++) {
int start = i * slotSize;
int end = (i + 1) * slotSize;
if((i + 1) == masters.size()){
end = 16384;
}
HostAndPort master = masters.get(i);
RedisClusterTerminal cluster = clients.get(master);
message(notify, ">> assignSlot from " + start + " to " + end + " with " + master);
for(;start < end; start++) {
if(start % 1000 == 0){
message(notify, ">> " + start);
}
cluster.clusterAddSlots(start);
}
}
message(notify, ">> assignSlot Done!");
}
示例14: reshard
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
/**
* 將當前槽遷移到該節點
*/
public void reshard(int start,int end)throws Exception {
message("start move slot from " + start + " to " + end);
List<M_clusterNode> all = getClusterNode_list();
Map<HostAndPort, M_clusterNode> hpNodes = new HashMap<HostAndPort, M_clusterNode>();
M_clusterNode myself = null;
for (M_clusterNode n : all) {
if(n.getMaster() == null || "".equals(n.getMaster())){
hpNodes.put(new HostAndPort(n.getHost(), n.getPort()), n);
}
if(n.getMyself()!= null && n.getMyself()){
myself = n;
}
}
M_ClusterSlots clusterSlots = new M_ClusterSlots(clusterSlots(), hpNodes);
int i = start;
while(i <= end){
M_clusterNode sourceNode = clusterSlots.getNodeBySlot(i);
if(sourceNode == null || !sourceNode.getNode().equals(myself.getNode())){
message(">> move :" + i);
moveSlot(myself, sourceNode, i);
}
i++;
}
}
示例15: selectSlave
import redis.clients.jedis.HostAndPort; //導入依賴的package包/類
/**
* 選取Slave節點
*/
private void selectSlave(Map<HostAndPort, List<HostAndPort>> tree, List<HostAndPort> list) {
List<HostAndPort> keySets = new ArrayList<HostAndPort>(tree.keySet());
while (list.size() > 0) {
Collections.shuffle(list);
Collections.shuffle(keySets);
for (HostAndPort ipPort : keySets) {
List<HostAndPort> slaves = tree.get(ipPort);
List<HostAndPort> exist = new ArrayList<HostAndPort>(slaves);
exist.add(ipPort);
HostAndPort tmp = getOtherHost(list, exist);
if(tmp != null){
slaves.add(tmp);
tree.put(ipPort, slaves);
}
}
}
}