Configuration points and exception resolution of Springboot connection to Redis

Recently, I learned SpringBoot to integrate Redis, and summarized some problems of Springboot connecting to Redis database, in case of recurrence

Redis configuration file

  1. Comment out the default bind
  2. Disable the security check module protected-mode
  3. Configure Redis access password
  4. After modifying the configuration file, you need to restart Redis to make the configuration take effect

ps: Use an editor to search for keywords such as bind, use /bind for the vi editor under Linux

# bind 127.0.0.1 -::1  
//1.注释掉 , 这样这个redis不会限于本机访问, 可以远程访问

protected-mode  no  
//2.此处修改成no , 关闭保护模块

#3. '#'  requirepass foobared
requirepass 123456   
//3. 设置密码 , 云端更为必要,甚至要复杂一些, 本地虚拟机可以不设置

//4. 重启Redis生效

Linux Firewall & Server Security Groups

  1. Release port 6379 of the Linux firewall
  2. If Redis installed on the cloud server is used, not only port 6379 of Linux but also port 6379 of security group should be released
firewall-cmd --zone=public --add-port=6379/tcp --permanent

firewall-cmd --reload

Spring Boot aspect

  1. yml configuration file
spring:
  # redis
  redis:
    host: 192.168.8.188
    port: 6379
    timeout: 3
    password: 123456  # 如果配置了密码一定要写 ,不然报错
    pool:
      minIdle: 1 # 最小空闲连接数(默认为0,该值只有为正数才有用)
      maxIdle: 10  # 最大空闲连接数(默认为8,负数表示无限)
      maxWait: 3  # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
      maxActive: 8 # 最大可用连接数(默认为8,负数表示无限)

  1. Annotation usage of StringRedisTemplate
    Reason: @Autowired is injected by type, @Resource is injected first based on name
    StringRedisTemplate is a subclass of RedisTemplate, if injected by type,
    an error will be reported
   //正确的用法
    @Resource
    private StringRedisTemplate  stringRedisTemplate;

	//错误的用法
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

//还有一种写法也可以执行
    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

Guess you like

Origin blog.csdn.net/weixin_48011779/article/details/125493032