springboot2之系统架构基础(五) springboot整合redis

step1 导入pom配置

<!-- spring redis -->
    	<dependency>  
              <groupId>org.springframework.boot</groupId>  
              <artifactId>spring-boot-starter-redis</artifactId>  
              <version>1.4.3.RELEASE</version>  
        </dependency> 

step2 yml配置redis集群

## Spring配置:
spring: 
  http: 
    encoding:
      charset: UTF-8 
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: NON_NULL      
  redis:   
    pool: 
      min-idle: 100
      max-idle: 100
      max-wait: -1
      max-active: 1000
    timeout: 6000  
    cluster: 
      max-redirects: 1000
      nodes: 
          - 192.168.128.12:7001
          - 192.168.128.12:7002
          - 192.168.128.12:7003
          - 192.168.128.12:7004
          - 192.168.128.12:7005
          - 192.168.128.12:7006 

properties 配置

spring.redis.cluster.nodes=192.168.2.124:7001,192.168.2.124:7002,192.168.2.124:7003,192.168.2.124:7004,192.168.2.124:7005,192.168.2.124:7006
spring.redis.timeout=0
# jedis pool configure
spring.redis.pool.max-total=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0

注意:是spring下的redis

step3 配置redis 的 RedisTemplate 对象

@Configuration
public class MyRedisConfiguration {

	@Bean
	public RedisTemplate<String, Object> initRedisTemplate(RedisConnectionFactory redisConnectionFactory){
		
		
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		
		return redisTemplate;
	}
	
}

step4  测试操作集群元素

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {

    private static Logger LOGGER = LoggerFactory.getLogger(ApplicationTests.class);    
    @Autowired  
    RedisTemplate<String, String> redisTemplate;  
      
    /**
     * <B>方法名称:</B>测试redis<BR>
     * <B>概要说明:</B><BR>
     */
    @Test  
    public void redisTest() {  
        String key = "redisTestKey";  
        String value = "I am test value";  
       
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();  
         System.err.println("---------------");
        //数据插入测试:  
        opsForValue.set(key, value);  
        String valueFromRedis = opsForValue.get(key);  
        LOGGER.info("redis value after set: {}", valueFromRedis);  
          
        //数据删除测试:  
        redisTemplate.delete(key);  
        valueFromRedis = opsForValue.get(key);  
        LOGGER.info("redis value after delete: {}", valueFromRedis);  
    }  
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值