springboot整合jedisCluster


maven依賴

springboot整合jedisCluster相當簡單,maven依賴如下:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-redis</artifactId>
 </dependency>

加了這一個依賴之后就不要再加上jedis的這一個依賴了:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

加這個可能在本身測試的時候,可能會導致jedisCluster對象正常,但是在測試的時候會發現set數據的時候會出現問題,我把jedis的依賴去掉之后,這個問題解決,因此不要加上jedis的這一個依賴,spring-boot-starter-redis這一個引入相關jedis需要的包。

application.properties配置

這里的配置相當簡單,只需要天上redis的相關地址就行了,如下:

#redis cluster
spring.redis.cache.clusterNodes=192.168.xx.xx:6379,192.168.xx.:6380,192.168.xx.xx:6381
spring.redis.cache.commandTimeout=5000

定義一個類命名問RedisProperties,在里面定義的字段與配置文件中相對應,即可取到配置,如下:

@Component
@ConfigurationProperties(prefix = "spring.redis.cache")
@Data
public class RedisProperties {

    private String clusterNodes;
    private Integer   commandTimeout;
}

JedisClusterConfig

@Configuration
@ConditionalOnClass({JedisCluster.class})
@EnableConfigurationProperties(RedisProperties.class)
public class JedisClusterConfig {

    @Inject
    private RedisProperties redisProperties;

    @Bean
    @Singleton
    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, redisProperties.getCommandTimeout());
    }
}

配置就完成,現在進行測試一次。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootWebApplication.class)
@WebAppConfiguration
public class TestJedisCluster {

    @Inject
    private JedisCluster jedisCluster;

    @Test
    public void testJedis() {
        jedisCluster.set("test_jedis_cluster", "38967");
        Assert.assertEquals("38967", jedisCluster.get("test_jedis_cluster"));
        jedisCluster.del("test_jedis_cluster");
    }
}

 

使用RedisTemplate
1、引入依賴:
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>  
 
        

2、配置文件application.yml在添加配置(假設有6個nodes):

spring:  
  redis:  
    cluster:  
      nodes:  
        - 192.168.0.17:6390  
        - 192.168.0.17:6391  
        - 192.168.0.17:6392  
        - 192.168.0.9:6390  
        - 192.168.0.9:6391  
        - 192.168.0.9:6392  

代碼測試:

@Autowired  
RedisTemplate<String, String> redisTemplate;  
  
@Test  
public void redisTest() {  
    String key = "redisTestKey";  
    String value = "I am test value";  
      
    ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();  
      
    //數據插入測試:  
    opsForValue.set(key, value);  
    String valueFromRedis = opsForValue.get(key);  
    logger.info("redis value after set: {}", valueFromRedis);  
    assertThat(valueFromRedis, is(value));  
      
    //數據刪除測試:  
    redisTemplate.delete(key);  
    valueFromRedis = opsForValue.get(key);  
    logger.info("redis value after delete: {}", valueFromRedis);  
    assertThat(valueFromRedis, equalTo(null));  
}  
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM