Jedis


依赖

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>3.2.0</version>
  5. </dependency>

测试连接

  1. public class TestPing {
  2. public static void main(String[] args) {
  3. Jedis jedis = new Jedis("127.0.0.1", 6379);
  4. System.out.println(jedis.ping());
  5. }
  6. }

测试事务

  1. public class TestTransactions {
  2. public static void main(String[] args) {
  3. Jedis jedis = new Jedis("127.0.0.1", 6379);
  4. JSONObject js = new JSONObject();
  5. js.put("name", "Tom");
  6. js.put("age", 5);
  7. String user = js.toJSONString();
  8. Transaction multi = jedis.multi();
  9. try {
  10. multi.set("user1", user);
  11. multi.set("user2", user);
  12. multi.exec();
  13. } catch (Exception e) {
  14. multi.discard();
  15. e.printStackTrace();
  16. } finally {
  17. System.out.println(jedis.get("user2"));
  18. jedis.close();
  19. }
  20. }
  21. }

SpringBoot+Redis

在SpringBoot2.x之后,jedis被替换为lettuce,jedis采用直连,多线程不安全,使用jedis pool连接池,类似BIO模式,lettuce采用netty,实例可以在多个线程中进行共享,不存在线程安全问题,可以减少线程数据,类似NIO模式。

依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

源码

  1. // @ConditionalOnMissingBean可以自定义
  2. @Bean
  3. @ConditionalOnMissingBean(
  4. name = {"redisTemplate"}
  5. )
  6. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  7. RedisTemplate<Object, Object> template = new RedisTemplate();
  8. template.setConnectionFactory(redisConnectionFactory);
  9. return template;
  10. }
  11. @Bean
  12. @ConditionalOnMissingBean
  13. public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  14. StringRedisTemplate template = new StringRedisTemplate();
  15. template.setConnectionFactory(redisConnectionFactory);
  16. return template;
  17. }

测试

Java中使用哨兵模式

  1. public class TestSentinels {
  2. @SuppressWarnings("resource")
  3. @Test
  4. public void testSentinel() {
  5. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  6. jedisPoolConfig.setMaxTotal(10);
  7. jedisPoolConfig.setMaxIdle(5);
  8. jedisPoolConfig.setMinIdle(5);
  9. // 哨兵信息
  10. Set<String> sentinels = new HashSet<>(Arrays.asList("192.168.11.128:26379",
  11. "192.168.11.129:26379","192.168.11.130:26379"));
  12. // 创建连接池
  13. JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,jedisPoolConfig,"123456");
  14. // 获取客户端
  15. Jedis jedis = pool.getResource();
  16. // 执行两个命令
  17. jedis.set("mykey", "myvalue");
  18. String value = jedis.get("mykey");
  19. System.out.println(value);
  20. }
  21. }

上面是通过Jedis进行使用的,同样也可以使用Spring进行配置RedisTemplate使用。

  1. <bean id = "poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  2. <!-- 最大空闲数 -->
  3. <property name="maxIdle" value="50"></property>
  4. <!-- 最大连接数 -->
  5. <property name="maxTotal" value="100"></property>
  6. <!-- 最大等待时间 -->
  7. <property name="maxWaitMillis" value="20000"></property>
  8. </bean>
  9. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  10. <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
  11. <constructor-arg name="sentinelConfig" ref="sentinelConfig"></constructor-arg>
  12. <property name="password" value="123456"></property>
  13. </bean>
  14. <!-- JDK序列化器 -->
  15. <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
  16. <!-- String序列化器 -->
  17. <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
  18. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  19. <property name="connectionFactory" ref="connectionFactory"></property>
  20. <property name="keySerializer" ref="stringRedisSerializer"></property>
  21. <property name="defaultSerializer" ref="stringRedisSerializer"></property>
  22. <property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
  23. </bean>
  24. <!-- 哨兵配置 -->
  25. <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
  26. <!-- 服务名称 -->
  27. <property name="master">
  28. <bean class="org.springframework.data.redis.connection.RedisNode">
  29. <property name="name" value="mymaster"></property>
  30. </bean>
  31. </property>
  32. <!-- 哨兵服务IP和端口 -->
  33. <property name="sentinels">
  34. <set>
  35. <bean class="org.springframework.data.redis.connection.RedisNode">
  36. <constructor-arg name="host" value="192.168.11.128"></constructor-arg>
  37. <constructor-arg name="port" value="26379"></constructor-arg>
  38. </bean>
  39. <bean class="org.springframework.data.redis.connection.RedisNode">
  40. <constructor-arg name="host" value="192.168.11.129"></constructor-arg>
  41. <constructor-arg name="port" value="26379"></constructor-arg>
  42. </bean>
  43. <bean class="org.springframework.data.redis.connection.RedisNode">
  44. <constructor-arg name="host" value="192.168.11.130"></constructor-arg>
  45. <constructor-arg name="port" value="26379"></constructor-arg>
  46. </bean>
  47. </set>
  48. </property>
  49. </bean>