SpringBoot redis哨兵模式使用

1.pom.xm文件配置


      4.0.0
      szw
      springboot_redis_sentinel
      0.0.1-SNAPSHOT
      springboot_redis_sentinel
      springboot整合redis哨兵
  
      
        UTF-8
        UTF-8
        1.8
        com.sicdt.sicsign.web.SicSignWebApplication
    
  
      
        org.springframework.boot
        spring-boot-starter-parent
        1.4.2.RELEASE
        
    
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-redis
        
    
    
    
    
        
            releases
            Releases
            http://192.168.3.71:8081/nexus/content/repositories/releases/
        
        
            snapshots
            Snapshots
            http://192.168.3.71:8081/nexus/content/repositories/snapshots/
        
    

    
    
        
            sicdt
            Sicdt
            http://192.168.3.71:8081/nexus/content/groups/public
        
        
            spring-releases
            https://repo.spring.io/libs-release
        
    
    
        
            spring-releases
            https://repo.spring.io/libs-release
        
    
    
      
         
              
              
                org.apache.maven.plugins  
                maven-source-plugin  
                  
                    true  
                  
                  
                      
                        compile  
                          
                            jar  
                          
                      
                  
            
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
              
          
    

2.配置application.properties

##单服务器
spring.redis.host=39.107.119.256
##单端口
spring.redis.port=6381
## 连接池最大连接数(使用负值表示没有限制) 
spring.redis.pool.max-active=300
## Redis数据库索引(默认为0) 
spring.redis.database=0
## 连接池最大阻塞等待时间(使用负值表示没有限制) 
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接 
spring.redis.pool.max-idle=100
## 连接池中的最小空闲连接 
spring.redis.pool.min-idle=20
## 连接超时时间(毫秒) 
spring.redis.timeout=60000

#哨兵的配置列表  
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=39.107.119.256:26379

3.启动类

package com.szw.redis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SzwRedisApplication {
    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(SzwRedisApplication.class, args);
    }
}

4.单元测试类

package com.sze.redis;

import javax.annotation.PostConstruct;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SentinelTest {
    
    @Autowired
    StringRedisTemplate redisTemplate;
    
    ValueOperations stringRedis;
    
    @PostConstruct
    public void init(){
        stringRedis=redisTemplate.opsForValue();
    }
    
    
    @Test
    public void testString (){
        stringRedis.set("name", "zz");
        System.out.println(stringRedis.get("name"));
    }
}

 

你可能感兴趣的:(java,SpringBoot,redis)