The WSL subsystem installs redis through docker to solve the problem that the host springboot program accesses the redis connection failure in docker

The WSL subsystem installs redis through docker to solve the problem that the host springboot program accesses the redis connection failure in docker

Environmental description

win10 2004

WSL2+ubuntu20.04, Tsinghua source has been set + self-start after boot + firewall has been turned off

docker: 19.03.12, and set up Alibaba Cloud image acceleration + start automatically with WSL subsystem boot

repeat : 5.0.7

Download the image file

docker pull redis:5.0.7

Create mount directory

Create a mount directory for redis configuration and data to facilitate adjustment of the redis startup configuration file outside the container, and to support data persistence outside the container. .

mkdir -p /usr/data/redis/conf
mkdir -p /usr/data/redis/data
cd /usr/data/redis/conf
vi redis.conf
#wq保存退出
#添加权限,可选操作,为防止因为权限问题报错可以加一下
chmod 777 redis.conf

Modify redis.conf configuration

First go to the official website to download the latest configuration, portal

Then create a copy locally and modify some configurations in the copy

  • bind 127.0.0.1, comment out this part, this is to restrict redis can only be accessed locally
  • protected-mode, the default is yes, the protected mode is turned on, restricted to local access, need to be changed to no
  • appendonly, redis persistence, set to yes to support redis data persistence to local files
  • requirepass, connect redis password, set your password
  • daemonize, the default is no, no need to change, if you change to yes according to some tutorials to start as a daemon process, it will cause the configuration file to fail to start redis
  • port, the redis startup port in the container, can be changed or not, it will be used later, here is a list first

The modified configuration is as follows:

#bind 127.0.0.1
port 6379
protected-mode no
daemonize no
appendonly yes
requirepass 123456

Run redis container

docker run -p 6379:6379 \
	-d -v /usr/data/redis/conf/redis.conf:/etc/redis/redis.conf \
	-v /usr/data/redis/data:/data \
	--restart unless-stopped \
	--name redis redis:5.0.7 redis-server /etc/redis/redis.conf 

Command explanation:

-p 6379:6379 #宿主机与docker容器端口映射
-d #后台启动
-v /usr/data/redis/conf/redis.conf:/etc/redis/redis.conf #指定启动配置文件挂载
-v /usr/data/redis/data:/data #指定持久化数据挂载目录
--restart unless-stopped #指定redis容器重启策略,开启docker,就会自动启动redis容器,除非你自己stop容器
--name redis #指定容器名称
redis:5.0.7 redis-server /etc/redis/redis.conf #指定使用的镜像,指定启动redis-server,指定启动redis-server的配置文件
#-----
#通过docker ps,可以看到redis是否启动成功
#上面有专门修改redis.conf配置文件,所以用Redis Desktop Manager客户端可以直接连接redis

Host springboot integrates docker container redis

Connecting redis with client tools is very smooth, but springboot integration redis is simply hard to say. The windows version of redis was even installed in the middle to verify that the springboot program is no problem. The result is that the springboot program integrates the windows version of redis once, but as long as everything goes to the redis installed in the docker container in the WSL subsystem, the connection fails.

Let's first talk about springboot integration redis:

The first is pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

It should be noted that springboot2.X uses the lettuce connection pool to connect to redis, so commons-pool2 needs to be introduced

application.yml

spring:
  redis:
    host: localhost
    port: 56379
    timeout: 10000
    password: 123456
    testOnBorrow: true
    testWhileIdle: true
    database: 0
    lettuce:
      pool:
        min-idle: 1
        max-idle: 5
        max-active: 10
        max-wait: -1

It should be noted here that springboot2.X, redis connection pool configuration has an extra layer of lettuce. In addition, timeout cannot be configured as 0, 10s configured here.

RedisUtil tool class

@Component
public class RedisUtil {
    
    
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	public void setString(String key, String data) {
    
    
		stringRedisTemplate.opsForValue().set(key, data);
	}
}

RedisController类

@RestController
@RequestMapping("/redis")
public class RedisController {
    
    
    @Autowired
    private RedisUtil redisUtil;

    Random random = new Random();

    @RequestMapping("/add")
    public String fun(){
    
    
        String key="redisDemo:testKey:";
        int i = random.nextInt(100);
        redisUtil.setString(key+i,i+"");

        return "ok";
    }
}

Open the browser and visit http://localhost:8080/redis/add, and it keeps reporting the connection failure exception, Could not connect to Redis at localhost:6379: Connection refused.

The first consideration was that there was a problem with the redis service, but there was no problem with using the Redis Desktop Manager client to directly access it, and I tried to adjust various parameters but it didn't work.

Later, I suspected whether there was a problem with springboot integration redis. Just after installing the windows version of redis on this machine, after switching the redis address to the native windows version of redis, the data can be added successfully immediately.

Finally, after checking a lot of information, the problem was finally solved.

Solve the problem that the host springboot integrated docker container redis connection fails

In fact, I changed the docker run command:

docker run --net host \
	-d -v /usr/data/redis/conf/redis.conf:/etc/redis/redis.conf \
	-v /usr/data/redis/data:/data \
	--restart unless-stopped \
	--name redis redis:5.0.7 redis-server /etc/redis/redis.conf 

Added the -net host command to specify that the container and the host share the network.

Because the container and the host share the network, there is no need to do port mapping anymore. You can modify the redis.conf file if you want to use which port as the port of the redis service.

Guess you like

Origin blog.csdn.net/l229568441/article/details/113816184