• RedisTemplate连接密码设置教程


    最近在一个项目中使用Redis保存Token时,出现连接Redis报错的情况

    1. org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
    2. at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1689)
    3. at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597)
    4. at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1383)
    5. at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1366)
    6. at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:1093)
    7. at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:421)
    8. at org.springframework.data.redis.core.RedisConnectionUtils.fetchConnection(RedisConnectionUtils.java:193)
    9. at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:144)
    10. at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:105)
    11. at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
    12. at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191)
    13. at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:97)
    14. at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:305)
    15. at com.zhangxi.SpringDataRedisTest.testString(SpringDataRedisTest.java:36)
    16. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    17. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    18. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    19. at java.lang.reflect.Method.invoke(Method.java:498)
    20. at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
    21. at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    22. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    23. at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    24. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    25. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    26. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    27. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    28. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    29. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    30. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    31. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    32. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    33. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    34. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
    35. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    36. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
    37. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
    38. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
    39. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
    40. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    41. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    42. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    43. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    44. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    45. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    46. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    47. at java.util.ArrayList.forEach(ArrayList.java:1257)
    48. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    49. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    50. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    51. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    52. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    53. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    54. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    55. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    56. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    57. at java.util.ArrayList.forEach(ArrayList.java:1257)
    58. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    59. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    60. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    61. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    62. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    63. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    64. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    65. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    66. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    67. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
    68. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    69. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
    70. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
    71. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    72. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    73. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)......

    整理一下连接密码步骤:

    实现RedisTemplate连接密码设置的步骤概览:

    1. section 步骤一: 导入依赖
    2. section 步骤二: 配置连接信息
    3. section 步骤三: 创建Redis连接工厂
    4. section 步骤四: 创建RedisTemplate实例

    步骤一: 导入依赖

    首先,你需要在你的项目中导入以下依赖:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-data-redis</artifactId>
    4. </dependency>

    步骤二: 配置连接信息

    在你的Spring Boot项目的配置文件(如application.yml或application.properties)中,添加Redis连接相关的配置信息。

    示例配置文件(application.yml):

    1. spring:
    2. redis:
    3. host: localhost
    4. port: 6379
    5. password: your_password

    上述配置中,你需要将your_password替换为你实际的Redis连接密码。

    步骤三: 创建Redis连接工厂

    在你的Java代码中,创建Redis连接工厂对象,并配置连接信息。

    1. import org.springframework.beans.factory.annotation.Value;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.data.redis.connection.RedisConnectionFactory;
    5. import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
    6. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    7. @Configuration
    8. public class RedisConfig {
    9. @Value("${spring.redis.host}")
    10. private String redisHost;
    11. @Value("${spring.redis.port}")
    12. private int redisPort;
    13. @Value("${spring.redis.password}")
    14. private String redisPassword;
    15. @Bean
    16. public RedisConnectionFactory redisConnectionFactory() {
    17. RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(redisHost, redisPort);
    18. redisConfig.setPassword(redisPassword);
    19. return new LettuceConnectionFactory(redisConfig);
    20. }
    21. }

    上述代码中,RedisConfig类使用@Configuration注解表示这是一个配置类。通过@Value注解获取配置文件中的Redis连接信息,并创建RedisStandaloneConfiguration对象来配置连接信息。然后,使用LettuceConnectionFactory来创建RedisConnectionFactory对象。

    步骤四: 创建RedisTemplate实例

    在你的Java代码中,创建RedisTemplate实例,并设置Redis连接工厂。

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.data.redis.connection.RedisConnectionFactory;
    5. import org.springframework.data.redis.core.RedisTemplate;
    6. @Configuration
    7. public class RedisConfig {
    8. @Autowired
    9. private RedisConnectionFactory redisConnectionFactory;
    10. @Bean
    11. public RedisTemplate<String, Object> redisTemplate() {
    12. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    13. redisTemplate.setConnectionFactory(redisConnectionFactory);
    14. return redisTemplate;
    15. }
    16. }

    上述代码中,通过@Autowired注解注入RedisConnectionFactory对象,然后在redisTemplate()方法中创建RedisTemplate实例,并设置连接工厂。

    END

    目录评论

  • 相关阅读:
    【Linux】进程间通信
    Web缓存
    基于RRT算法的最优动力学路径规划(Matlab代码实现)
    如何使用API数据接口给自己创造收益
    工业和能源1994-2019年省级面板数据
    java代理模式教你中间商赚差价
    关于jar包的一些思考
    -EasyUI-------BootStrap,layer-已经谢幕了,为了感谢曾经相遇,说说里面个人认为比较好的功能吧
    探索ABP基础架构
    LayaBox---知识点
  • 原文地址:https://blog.csdn.net/weixin_46039745/article/details/134282517