spring boot integrated redis

Keywords: Redis Spring Database SSL

It is convenient to integrate common NO SQL with Springboot following the principle of "convention is greater than configuration". This example makes a simple demo with redis, using Redis's set and get commands.
Introduce dependency packages for redis.


Declare a Redis bean in @Configuration or a class that integrates the annotation's identity, in this case a Bean declared on the entry class:

public class Application {
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);

Inject StringRedisTemplate into Controller:

public class RedisController {
    private StringRedisTemplate stringRedisTemplate;
    public ResultBean set(@RequestParam(name = "name", defaultValue = "CYF") String name){
        stringRedisTemplate.opsForValue().set("test", name);
        return ResultBean.ok();
    public ResultBean set(){
        String value = stringRedisTemplate.opsForValue().get("test");
        return ResultBean.ok(value);

Start the project and visit:
set command:

get command:

In this example, redis is the 6379 redis server accessed by default, and we can modify the configuration so that it can access the specified redis server.
To spring boot Official Instructions You can find the corresponding configurations on the and write them to resources/Appcation.propertiesThere is no demonstration here.

# REDIS (RedisProperties)
spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL, will override host, port and password (user will be ignored), e.g. redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.ssl=false # Enable SSL support.
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
spring.redis.timeout=0 # Connection timeout in milliseconds.

As you can see from the configuration, it supports both a single redis, a redis cluster and a connection pool, or is very powerful, and small and medium concurrency volumes can be supported.

Posted by dcj1978 on Sun, 05 Jul 2020 09:08:30 -0700