SpringBoot 2.2.2.RELEASE集成Redis v6.0.3实现读写数据操作。

官方文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-nosql

Redis is a cache, message broker, and richly-featured key-value store. Spring Boot offers basic auto-configuration for the Lettuce and Jedis client libraries and the abstractions on top of them provided by Spring Data Redis.

There is a spring-boot-starter-data-redis “Starter” for collecting the dependencies in a convenient way. By default, it uses Lettuce. That starter handles both traditional and reactive applications.

we also provide a spring-boot-starter-data-redis-reactive “Starter” for consistency with the other stores with reactive support.

 

Connecting to Redis

You can inject an auto-configured RedisConnectionFactoryStringRedisTemplate, or vanilla RedisTemplate instance as you would any other Spring Bean. By default, the instance tries to connect to a Redis server at localhost:6379. The following listing shows an example of such a bean:

 

@Component
public class MyBean {

    private StringRedisTemplate template;

    @Autowired
    public MyBean(StringRedisTemplate template) {
        this.template = template;
    }

    // ...

}

 

You can also register an arbitrary number of beans that implement LettuceClientConfigurationBuilderCustomizer for more advanced customizations. If you use Jedis, JedisClientConfigurationBuilderCustomizer is also available.

If you add your own @Bean of any of the auto-configured types, it replaces the default (except in the case of RedisTemplate, when the exclusion is based on the bean name, redisTemplate, not its type). By default, if commons-pool2 is on the classpath, you get a pooled connection factory.

以上就是doc里面对SpringBoot和redis的一些介绍。
下面是官方是 reactive  guide: https://spring.io/guides/gs/spring-data-reactive-redis/

demo:https://github.com/spring-guides/gs-spring-data-reactive-redis

pom.xml



    org.springframework.boot

    spring-boot-starter-data-redis

    



    redis.clients

    jedis

    3.3.0

package com.demo.springboot.config;

import com.demo.springboot.redis.RedisPojo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 *  指明当前类是一个配置类 用来替代之前的spring 配置文件
 *  
 */
@Configuration
public class MyAppConfig {

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        // RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("server", 6379);
        return new JedisConnectionFactory();
    }

    @Bean
	RedisOperations redisOperations(final RedisConnectionFactory factory) {
		final Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(RedisPojo.class);

		// final RedisSerializationContext.RedisSerializationContextBuilder builder =
		// 		RedisSerializationContext.newSerializationContext(new StringRedisSerializer());

        // final RedisSerializationContext stringSerializer = builder.value(serializer).build();
        
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        // redisTemplate.setValueSerializer(serializer);
		return redisTemplate;
    }
    
}

bean:

package com.demo.springboot.redis;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RedisPojo {

  private String id;
  private String name;
  
}

controller:

package com.demo.springboot.redis;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RedisControler {

  private final RedisOperations redisOperations;
  private final RedisOperations redisOperationsString;
  private final RedisConnectionFactory redisFactory;
  
  RedisControler(RedisConnectionFactory redisFactory,RedisOperations coffeeOps,RedisOperations redisOperationsString) {
    this.redisOperations = coffeeOps;
    this.redisFactory=redisFactory;
    this.redisOperationsString=redisOperationsString;
  }
  
  /**
   * 获取数据
   * @return
   */
  @GetMapping("/redis_option")
	public String all() {
    Set sets = redisOperations.keys("*");
    Iterator it = sets.iterator();  
    String keyString="";
    while (it.hasNext()) {  
      String str = it.next();  
      Object value=(Object) redisOperations.opsForValue().get(str);
      keyString+=value.toString();
    }  

		return sets.toString()+keyString;
  }

  /**
   * 设置数据
   */
  @GetMapping("/set_redis")
	public void set() {
    ValueOperations opsForSet = redisOperationsString.opsForValue();
    opsForSet.set("name", "redis");
  //  redisFactory.getConnection().flushAll(); //delete all keys 

  }
  
}

浏览器访问:http://localhost:8081/SpringBoot/set_redis

http://localhost:8081/SpringBoot/redis_option
输出:

[63bc016e-1724-41c7-a088-ce3ec29ee6d9, name, 018ca8a1-6cc3-47b2-8cc2-0b9ef1e5ee2d, 648f696c-b307-4f5a-a111-f2679d9248b2]{"id":"63bc016e-1724-41c7-a088-ce3ec29ee6d9","name":"Black Alert Redis"}redis{"id":"018ca8a1-6cc3-47b2-8cc2-0b9ef1e5ee2d","name":"Darth Redis"}{"id":"648f696c-b307-4f5a-a111-f2679d9248b2","name":"Jet Black Redis"}

redis terminal:

127.0.0.1:6379> keys *
1) "63bc016e-1724-41c7-a088-ce3ec29ee6d9"
2) "name"
3) "018ca8a1-6cc3-47b2-8cc2-0b9ef1e5ee2d"
4) "648f696c-b307-4f5a-a111-f2679d9248b2"
127.0.0.1:6379> get name
"redis"
127.0.0.1:6379> 

更多内容 redis properties:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#data-properties

 

 

你可能感兴趣的:(SpringBoot,java)