When you first use springboot to connect to redis, you will find that there are many configuration files, which leads to no experience at the beginning of learning. In fact, when using springboot to connect to redis, you don’t need so much configuration.
First, enable the redis server :
Then add configuration files in springboot:
# Redis database index (default is 0) spring.redis.database=0# Redis server address spring.redis.host=localhost# Redis server connection port spring.redis.port=6379# Redis server connection password (default is empty) spring.redis.password=# Maximum number of connections to the connection pool (using negative values means no limit) spring.redis.pool.max-active=8# Maximum blocking waiting time for connection pool (using negative values means no limit) spring.redis.pool.max-wait=-1# Maximum idle connection in connection pool spring.redis.pool.max-idle=8# Minimum idle connection in connection pool spring.redis.pool.min-idle=0# Connection timeout (milliseconds) spring.redis.timeout=0
Finally, add dependencies in springboot's pom.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
Then write a test class in springboot project:
@RunWith(SpringRunner.class)@SpringBootTestpublic class ShoppingApplicationTests {@Autowired private StringRedisTemplate stringRedisTemplate;@Test public void test() throws Exception {List<String> list =new ArrayList<>();list.add("a");list.add("b");list.add("v");stringRedisTemplate.opsForValue().set("abc", "Test");stringRedisTemplate.opsForList().leftPushAll("qq",list);//Save ListstringRedisTemplate.opsForList().range("qwe",0,-1).forEach(value ->{System.out.println(value);});}Then you can see the newly created one in Redis
Then this is the easiest way to connect to redis in springboot
Summarize
The above is all the detailed explanation of SpringBoot's preliminary connection to redis, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
SpringBoot optimization tips
Recommended deployment method for Spring Boot microservice projects
Springboot's Advantages to Spring
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!