SpringBoot整合Redis

一:创建springboot项目导入redis依赖


   org.springframework.boot
   spring-boot-starter-data-redis



   org.springframework.boot
   spring-boot-starter-test
   test

二:编写redis的测试类TestRedis

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class TestRedis {


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public void testRedis() {
        stringRedisTemplate.opsForValue().set("hello", "China");
        System.out.println(stringRedisTemplate.opsForValue().get("hello"));
    }
}

SpringBoot整合Redis_第1张图片

注:TestRedis类不要忘记注解Component

三:application.properties文件

        声明对应redis的服务器的ip地址和redis的端口号

spring.redis.port=6379
spring.redis.host=127.0.0.1

四:运行测试

出现报错信息:

Exception in thread "main" org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1689)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1383)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1366)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:1093)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:421)
	at org.springframework.data.redis.core.RedisConnectionUtils.fetchConnection(RedisConnectionUtils.java:193)
	at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:144)
	at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:105)
	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191)
	at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:97)
	at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:305)
	at com.example.TestRedis.testRedis(TestRedis.java:15)
	at com.example.RedisSpringBootApplication.main(RedisSpringBootApplication.java:13)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
	at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
	at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
	at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:330)
	at io.lettuce.core.RedisClient.connect(RedisClient.java:216)
	at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:115)
	at java.util.Optional.orElseGet(Optional.java:267)
	at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115)
	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1595)
	... 13 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:6379
Caused by: java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
	at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)

报错原因:

        一:redis默认启动是受保护的,只能本台主机连接本台主机上的redis(我用的是云服务器)

        二:redis默认启动时bind了ip(默认是127.0.0.1)

五:修改redis启动的配置文件(也可以只做临时修改)

使用命令编辑redis配置文件:

        vim + redis配置文件路径

        eg:vim /etc/redis/6379.conf

修改两处:

        1.注释掉bind的ip(或者改成自己springboot项目的ip)

SpringBoot整合Redis_第2张图片

         2.将protected-mode 由默认的yes改成no

SpringBoot整合Redis_第3张图片

 六:重启redis并测试

杀死redis进程后重新启动redis:redis-server /etc/redis/6379.conf

        在TestRedis类里面,用StringRedisTemplate设置的key是“hello”,对应的value是“China”,那么我们看到以下的运行结果,输出了对应的value:

SpringBoot整合Redis_第4张图片

         在redis的cli客户端查看key是否存在?

SpringBoot整合Redis_第5张图片

         先在redis的cli客户端清库再运行的代码,可以看到key已经被成功设置,说明springboot整合redis的小测试已经成功。

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