一、prepare redis test environment
here use docker-compose to build redis test environment,use redis single mode
docker-compose-redis.yml
version: '3'
services:
redis:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 # image 'redis:6.0.8'
container_name: redis # docker name'redis'
restart: unless-stopped #
command: redis-server /etc/redis/redis.conf --requirepass 123456 --appendonly no
# command: redis-server --requirepass 123456 --appendonly yes # run redis wtih password:123456,and start with aof
environment: #
TZ: Asia/Shanghai
LANG: en_US.UTF-8
volumes: # local file mapping to docker directory
- "./redis/data:/data"
- "./redis/config/redis.conf:/etc/redis/redis.conf" # `redis.conf` content `http://download.redis.io/redis-stable/redis.conf`
ports: #mapping port
- "6379:6379"
二、build the redis module
1.pom.xml
<!--redis client-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
2.RedisConfig.java
package com.et59.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redisTemplate config
*/
@Configuration
public class RedisConfig {
/**
* redis template.
*
* @param factory factory
* @return RedisTemplate
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
3.application.yaml
server:
port: 8088
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
password: 123456
lettuce:
pool:
min-idle: 0
max-active: 8
max-idle: 8
max-wait: -1ms
connect-timeout: 30000ms
4.Verify service availability
package com.et.redis;
import com.et59.redis.DemoApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class RedisTests {
private Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private RedisTemplate redisTemplate;
@Test
public void save() {
redisTemplate.opsForValue().set("test","this is a test");
System.out.println(redisTemplate.opsForValue().get("test"));
}
}
the result is same as the expected
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)2024-01-25 17:10:48.566 INFO 18120 --- [ main] com.et.redis.RedisTests : Starting RedisTests on BJDPLHHUAPC with PID 18120 (started by Dell in D:\IdeaProjects\ETFramework\redis)
2024-01-25 17:10:48.567 INFO 18120 --- [ main] com.et.redis.RedisTests : No active profile set, falling back to default profiles: default
2024-01-25 17:10:49.143 INFO 18120 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2024-01-25 17:10:49.147 INFO 18120 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-01-25 17:10:49.178 INFO 18120 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 Redis repository interfaces.
2024-01-25 17:10:50.165 INFO 18120 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2024-01-25 17:10:50.500 INFO 18120 --- [ main] com.et.redis.RedisTests : Started RedisTests in 2.222 seconds (JVM running for 2.901)
2024-01-25 17:10:50.785 INFO 18120 --- [ main] io.lettuce.core.EpollProvider : Starting without optional epoll library
2024-01-25 17:10:50.787 INFO 18120 --- [ main] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
this is a test
2024-01-25 17:10:51.312 INFO 18120 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
三、reference
code demo: https://github.com/Harries/springboot-demo/