Home > Database > Redis > Docker+Redis+SpringBoot connection method

Docker+Redis+SpringBoot connection method

WBOY
Release: 2023-06-03 09:46:07
forward
1286 people have browsed it

Docker installation

After successful installation, open docker engine

Docker+Redis+SpringBoot connection method

Add domestic mirror

"registry-mirrors" : [ "http://hub-mirror.c.163.com" ],

Redis mirror installation

Enter https://hub.docker.com/ and search redis, find the first official image

Docker+Redis+SpringBoot connection method

You can check how to start redis in the docker container

The relevant commands are as follows:

  • docker pull redis # Pull the remote redis image

  • docker run –name some-redis -p 6379:6379 -d redis #Start redis and expose it to the host’s 6379 Port

  • docker ps #View the currently running mirror process

  • docker restart some-redis #Restart a mirror

SpringBoot connects to Redis

Start the SpringBoot project, configuration file

redis.host=localhost
redis.maxTotal=5
redis.maxIdle=5
redis.testOnBorrow=true
Copy after login

Use Jedis to connect to redis, introduce pom

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Copy after login

Add configuration class

	@Bean
	@ConfigurationProperties("redis")
	public JedisPoolConfig jedisPoolConfig() {
		return new JedisPoolConfig();
	}

	@Bean(destroyMethod = "close")
	public JedisPool jedisPool(@Value("${redis.host}") String host) {
		return new JedisPool(jedisPoolConfig(), host);
	}
Copy after login

Note: Jedis is not thread-safe, so you need to get it from JedisPool

The above is the detailed content of Docker+Redis+SpringBoot connection method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template