java、spring、springboot中整合Redis的详细讲解

Redis的配置

java整合Redis

1、引入依赖或者导入jar包

<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
    <version>2.9.0version>
dependency>

2、代码实现

public class JedisTest {
     
    public static void main(String[] args) {
     
        //连接redis
        //Jedis jedis=new Jedis("192.168.65.128",6379);

        //使用Jedis连接池
        Jedis jedis=getJedis();
        //操作redis
        jedis.set("name","小白");
        jedis.set("age","19");
        System.out.println("操作成功!");
        jedis.close();
    }

    public static Jedis getJedis(){
     
        //创建连接池配置对象
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxIdle(10);
        config.setMinIdle(5);
        config.setMaxTotal(100);
        //需要redis的服务密码时,使用第一种创建方式
        //JedisPool jedisPool=new JedisPool(config,"192.168.65.128",6379,10000,"root");
        JedisPool jedisPool=new JedisPool(config,"192.168.65.128",6379,10000);
        return jedisPool.getResource();
    }
}

Spring整合Redis

1、添加依赖

<dependency>
    <groupId>org.springframework.datagroupId>
    <artifactId>spring-data-redisartifactId>
    <version>2.1.8.RELEASEversion>
dependency>

<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
    <version>2.9.0version>
dependency>

2、redis配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        
        <property name="maxIdle" value="50"/>
        
        <property name="maxTotal" value="100"/>
        
        <property name="maxWaitMillis" value="60000"/>
    bean>

    
    <bean id="factory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        
        <property name="poolConfig" ref="poolConfig"/>
        
        <property name="hostName" value="192.168.65.128"/>
        
        <property name="port" value="6379"/>
        
        
        
    bean>

    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        
        <property name="connectionFactory" ref="factory"/>
    bean>
beans>

3、注入模板对象

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-redis.xml")
public class RedisTest {
     
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test(){
     
        //redisTemplate.opsForValue().set("name","小黑");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
        System.out.println("操作完成");
    }
}

注意:使用Spring(SpringBoot)整合redis后,RedisTemplate对象会自带key和value的序列化功能。在redis的客户端操作时,获取的key是被序列化后的key.
思考:为什么Spring要提供一个序列化的功能? 为了开发者方便将对象存入redis中。可在xml中配置自定义的序列化类型。

 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        
        <property name="connectionFactory" ref="factory"/>
        
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <property name="valueSerializer" ref="stringRedisSerializer"/>
    bean>
    
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>

springboot整合Redis

1、添加依赖

<dependency>
  <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

2、配置application.yml
java、spring、springboot中整合Redis的详细讲解_第1张图片
3、注入模板对象

@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootRedisApplicationTests {
     

    @Autowired
    private RedisTemplate redisTemplate;

    @PostConstruct
    public void init(){
     
        //配置String类型的key  value序列化方式
        redisTemplate.setStringSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
    }

    @Test
    void contextLoads() {
     
        redisTemplate.opsForValue().set("age",12);
        Object age = redisTemplate.opsForValue().get("age");
        System.out.println(age);
        System.out.println("操作成功");
    }
}

注意:不能在yml配置文件中配置自定义序列化,可以在springboot启动类或者测试类中,通过@PostConstruct注解来触发执行方法,从而达到配置自定义序列化的效果。
补充:
1.@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
2.@PreDestroy说明
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

总结

1、当项目报以下错误:Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
报错的原因:是redis服务没设置密码,而项目配置文件中写了有redis密码
解决方案:
1)把项目配置文件中的密码password设置为空或者不设置。
2)设置redis服务密码
——可通过直接修改redis.conf配置文件中的requirepass属性方式,如果修改不生效,可通过命令方式修改,进入redis的客户端
redis 127.0.0.1:6379> CONFIG SET requirepass “root”
OK
redis 127.0.0.1:6379> AUTH root
Ok
然后重启项目就可以连接本机的redis服务了。

有不清除的地方,可以在评论下方留言。。。既然来了,不妨点个关注,点个赞吧!!!

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