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

Docker+Redis+SpringBoot connection method

WBOY
WBOYforward
2023-06-03 09:46:071282browse

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

Use Jedis to connect to redis, introduce pom

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

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);
	}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete