java连接redis-主从复制,哨兵模式
1 简单的主从复制模式
public class TextMS {
public static void main(String[] args) {
Jedis jedis_M = new Jedis("127.0.0.1",6379);
Jedis jedis_S = new Jedis("127.0.0.1",6380);
//从机连接到主机
jedis_S.slaveof("127.0.0.1",6379);
//主机写入
jedis_M.set("class","1122");
//从机读取
String result = jedis_S.get("class");
System.out.println(result);
}
2 哨兵模式
// 连接池配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10);
jedisPoolConfig.setMaxIdle(5);
jedisPoolConfig.setMinIdle(5);
// 哨兵信息
Set<String> sentinels = new HashSet<String>(Arrays.asList("192.168.11.128:6379", "192.168.11.129:6379", "192.168.11.130:6379"));
//创建连接池
//mymaster是我们配置给哨兵的服务名称
// 123 连接redis服务器的密码
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, jedisPoolConfig, "123");
//获取客户端
Jedis jedis = pool.getResource();
//执行两个命令
jedis.set("mykey", "myvalue");
System.out.println(jedis.get("mykey"));
3 在spring中使用哨兵模式
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!--配置Redis连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" /> <!--最大空闲数 -->
<property name="maxTotal" value="100" /> <!--最大连接数 -->
<property name="maxWaitMillis" value="3000" /> <!--最大等待时间3s -->
</bean>
<!--jdk序列化器,可保存对象 -->
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<!--String序列化器 -->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!--哨兵配置 -->
<bean id="sentinelConfig"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<!--服务名称 -->
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster" />
</bean>
</property>
<!--哨兵服务IP和端口 -->
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.11.128" />
<constructor-arg name="port" value="26379" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.11.129" />
<constructor-arg name="port" value="26379" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.11.130" />
<constructor-arg name="port" value="26379" />
</bean>
</set>
</property>
</bean>
<!--连接池设置 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="sentinelConfig" />
<constructor-arg name="poolConfig" ref="poolConfig" />
<property name="password" value="abcdefg" />
</bean>
<!--配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="defaultSerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
</bean>
</beans>
测试
public static void testSpringSentinel() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application.xml");
RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
String retVal = (String) redisTemplate.execute((RedisOperations ops) -> {
ops.boundValueOps("mykey").set("myvalue");
String value = (String) ops.boundValueOps("mykey").get();
return value;
});
System.out.println(retVal);
}
还没有评论,来说两句吧...