Day11: Spring Integrates Redis Sentinel and Redis Cluster

Posted by justsomeone on Thu, 08 Aug 2019 11:41:49 +0200

Part I Spring Integration Redis

I. Preparations




Introduction Case 1


Master Name host name: mymaster
Sentinel: ip: port number

import redis.clients.jedis.JedisSentinelPool;

public class TestSentinel {

	@Test
	public void testSentinel1() {
		String masterName = "mymaster";//host name
		Set<String> sentinels = new HashSet<>();//Sentinel type
		sentinels.add("192.168.126.166:26379"); //Add Sentinel Host Name Port Number
		// sentinels.add("192.168.126.166:26380");
		// sentinels.add("192.168.126.166:26381");
		// sentinels.add("192.168.126.166:26382");

		// Get Sentinel Connection Host Name Sentinel IP: Port Number
		JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName, sentinels);
		
//		Sentinel Setting Resources
		Jedis jedis = sentinelPool.getResource();
		jedis.set("commandOne", "commandOne Sentry jedis Send a congratulatory message");
//		Output Sentinel Resources
		System.out.println("Access to resources:"+jedis.get("commandOne"));

//		Return the connection pool with jedis to the resource
		sentinelPool.returnResource(jedis);
	}
}


Introduction Case 2

1. Editing Pro files

#redis.host=192.168.126.166
#redis.port.a=6379
#redis.port.b=6380
#redis.port.c=6381
redis.maxTotal=10000
redis.sentinel=192.168.126.166:26379
redis.masterName=mymaster

2. Edit application-redis configuration file, comment out single redis, fragmented redis configuration

<!-- Tectonic injection, sentinels Attribute correspondence set Collection, to use set Label -->
	<bean id="jedisSentinelPool"
		class="redis.clients.jedis.JedisSentinelPool">
		<constructor-arg name="masterName"
			value="${redis.masterName}" />
		<constructor-arg name="sentinels">
			<set>
				<value>${redis.sentinels}</value>
			</set>
		</constructor-arg>
		<constructor-arg name="poolConfig" ref="poolConfig" />
	</bean>

	<!-- Define pool objects -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxTotal}" />
	</bean>

3. Modify RedisService under common Project to comment out the content used in fragmentation

@Service
public class RedisService {

//	Use redis Sentinel
	@Autowired(required=false)
	private JedisSentinelPool jedisSentinelPool;
	
	public void set(String key,String value) {
		Jedis jedis = jedisSentinelPool.getResource();
		jedis.set(key, value);
		jedisSentinelPool.returnResource(jedis);
	}
	
	public String get(String key) {
		Jedis jedis = jedisSentinelPool.getResource();
		String result = jedis.get(key);
		jedisSentinelPool.returnResource(jedis);
		return result;
	}
}

Note: Repackaged release after modification, maven install

Part II Redis Cluster

Redis Cluster

1. Fragmentation: It can dynamically realize memory expansion and disperse data storage.
2. Sentinel role: high availability of redis
Redis cluster implementation:
Using redis cluster configuration, communication and election can be realized within the cluster, while fragmentation expansion and high availability of redis can be realized.
3. Brain fissure
Because the result of voting flat in the election mechanism leads to the emergence of multiple hosts, this phenomenon is called brain fissure.
Solution: Increase the number of nodes (more than half can be guaranteed)

Cluster building step [Close all open redis]


I. Dividing the Scale of Clusters
Scale: 3 Masters and 6 Subordinates [Truth: 15-20]
Port: 70000-7008 9 sets
2. Construction steps:

1. Copy redis file to 7000 folder


  • Turn off AOF and use RDB storage mode

  • Unbound ip

  • Turn off protection mode

  • Modify port number

  • Open Background Start

  • Modify the PID path

  • Modify the persistent file path

  • Modify memory policy

  • Open Cluster

  • Open Cluster Profile

  • Modify cluster selection time

2. Copy 7000 folders


Quick Replacement


Writing Quick Start Script Files


Start script

sh start.sh

Create a management redis cluster through ruby

Topics: Redis Jedis Spring Attribute