java连接redis-主从复制,哨兵模式

川长思鸟来 2024-04-18 15:21 114阅读 0赞

1 简单的主从复制模式

  1. public class TextMS {
  2. public static void main(String[] args) {
  3. Jedis jedis_M = new Jedis("127.0.0.1",6379);
  4. Jedis jedis_S = new Jedis("127.0.0.1",6380);
  5. //从机连接到主机
  6. jedis_S.slaveof("127.0.0.1",6379);
  7. //主机写入
  8. jedis_M.set("class","1122");
  9. //从机读取
  10. String result = jedis_S.get("class");
  11. System.out.println(result);
  12. }

2 哨兵模式

  1. // 连接池配置
  2. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  3. jedisPoolConfig.setMaxTotal(10);
  4. jedisPoolConfig.setMaxIdle(5);
  5. jedisPoolConfig.setMinIdle(5);
  6. // 哨兵信息
  7. Set<String> sentinels = new HashSet<String>(Arrays.asList("192.168.11.128:6379", "192.168.11.129:6379", "192.168.11.130:6379"));
  8. //创建连接池
  9. //mymaster是我们配置给哨兵的服务名称
  10. // 123 连接redis服务器的密码
  11. JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, jedisPoolConfig, "123");
  12. //获取客户端
  13. Jedis jedis = pool.getResource();
  14. //执行两个命令
  15. jedis.set("mykey", "myvalue");
  16. System.out.println(jedis.get("mykey"));

3 在spring中使用哨兵模式

  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!--配置Redis连接池 -->
  7. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  8. <property name="maxIdle" value="50" /> <!--最大空闲数 -->
  9. <property name="maxTotal" value="100" /> <!--最大连接数 -->
  10. <property name="maxWaitMillis" value="3000" /> <!--最大等待时间3s -->
  11. </bean>
  12. <!--jdk序列化器,可保存对象 -->
  13. <bean id="jdkSerializationRedisSerializer"
  14. class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
  15. <!--String序列化器 -->
  16. <bean id="stringRedisSerializer"
  17. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  18. <!--哨兵配置 -->
  19. <bean id="sentinelConfig"
  20. class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
  21. <!--服务名称 -->
  22. <property name="master">
  23. <bean class="org.springframework.data.redis.connection.RedisNode">
  24. <property name="name" value="mymaster" />
  25. </bean>
  26. </property>
  27. <!--哨兵服务IP和端口 -->
  28. <property name="sentinels">
  29. <set>
  30. <bean class="org.springframework.data.redis.connection.RedisNode">
  31. <constructor-arg name="host" value="192.168.11.128" />
  32. <constructor-arg name="port" value="26379" />
  33. </bean>
  34. <bean class="org.springframework.data.redis.connection.RedisNode">
  35. <constructor-arg name="host" value="192.168.11.129" />
  36. <constructor-arg name="port" value="26379" />
  37. </bean>
  38. <bean class="org.springframework.data.redis.connection.RedisNode">
  39. <constructor-arg name="host" value="192.168.11.130" />
  40. <constructor-arg name="port" value="26379" />
  41. </bean>
  42. </set>
  43. </property>
  44. </bean>
  45. <!--连接池设置 -->
  46. <bean id="connectionFactory"
  47. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  48. <constructor-arg name="sentinelConfig" ref="sentinelConfig" />
  49. <constructor-arg name="poolConfig" ref="poolConfig" />
  50. <property name="password" value="abcdefg" />
  51. </bean>
  52. <!--配置RedisTemplate -->
  53. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  54. <property name="connectionFactory" ref="connectionFactory" />
  55. <property name="keySerializer" ref="stringRedisSerializer" />
  56. <property name="defaultSerializer" ref="stringRedisSerializer" />
  57. <property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
  58. </bean>
  59. </beans>

测试

  1. public static void testSpringSentinel() {
  2. ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application.xml");
  3. RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
  4. String retVal = (String) redisTemplate.execute((RedisOperations ops) -> {
  5. ops.boundValueOps("mykey").set("myvalue");
  6. String value = (String) ops.boundValueOps("mykey").get();
  7. return value;
  8. });
  9. System.out.println(retVal);
  10. }

发表评论

表情:
评论列表 (有 0 条评论,114人围观)

还没有评论,来说两句吧...

相关阅读