Spring Boot和Redis整合

方案一:https://blog.csdn.net/stronglyh/article/details/81173563

其中redis.yml是连接redis的配置文件,RedisConfig.java是java配置类,RedisC.java是操作redis的java类

随着Spring Boot2.x的到来,支持的组件越来越丰富,也越来越成熟,其中对Redis的支持不仅仅是丰富了它的API,更是替换掉底层Jedis的依赖,取而代之换成了Lettuce(生菜)
Lettuce和Jedis的都是连接Redis Server的客户端程序。Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
 

package com.dg.configurer;


import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {


    @Bean
    public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
package com.dg.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RedisC {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //添加 http://localhost:8080/redisAdd
    @GetMapping(value="/redisAdd")
    public void saveRedis(){
        stringRedisTemplate.opsForValue().set("a","test");
    }

    //获取 http://localhost:8080/redisGet
    @GetMapping(value="/redisGet")
    public String getRedis(){
        return stringRedisTemplate.opsForValue().get("a");
    }
}

redis.yml 配置文件

#redis
redis:
  #redis机器ip
  hostname: 127.0.0.1
  #redis端口
  port: 6379
  #redis密码
  password: 123456
  #redis超时时间(毫秒),如果不设置,取默认值2000
  timeout: 10000
#最大空闲数
  maxIdle: 300
#连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal
#maxActive=600
#控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
  maxTotal: 1000
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
  maxWaitMillis: 1000
#连接的最小空闲时间 默认1800000毫秒(30分钟)
  minEvictableIdleTimeMillis: 300000
#每次释放连接的最大数目,默认3
  numTestsPerEvictionRun: 1024
#逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
  timeBetweenEvictionRunsMillis: 30000
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
  testOnBorrow: true
#在空闲时检查有效性, 默认false
  testWhileIdle: true

#redis集群配置
#spring.cluster.nodes=192.168.1.1:7001,192.168.1.1:7002,192.168.1.1:7003,192.168.1.1:7004,192.168.1.1:7005,192.168.1.1:7006
#spring.cluster.max-redirects=3

#哨兵模式
#sentinel.host1=192.168.1.1
#sentinel.port1=26379

#sentinel.host2=192.168.1.2
#sentinel.port2=26379

方案二:

https://blog.csdn.net/u014116780/article/details/84843473

发布了149 篇原创文章 · 获赞 8 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/bluewelkin/article/details/102710065