Spring Data Redis’s Property-Based Configuration – Spring Data Redis’的基于属性的配置

最后修改: 2020年 5月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

One of the main attractions of Spring Boot is how it often reduces third-party configuration to just a few properties.

Spring Boot的主要吸引力之一是它经常将第三方配置减少到只有几个属性。

In this tutorial, we’re going to see how Spring Boot simplifies working with Redis.

在本教程中,我们将看到Spring Boot如何简化Redis的工作。

2. Why Redis?

2.为什么是Redis?

Redis is one of the most popular in-memory data structure stores. For this reason, it can be used as a database, cache, and message broker.

Redis是最流行的内存数据结构存储之一。由于这个原因,它可以被用作数据库、缓存和消息代理。

In terms of performance, it is well known because of its fast response time. As a result, it can serve hundreds of thousands of operations per second and is easily scalable.

在性能方面,它因其快速响应时间而闻名。因此,它可以每秒为数十万个操作提供服务,并且易于扩展。

And, it pairs well with Spring Boot applications. For example, we can use it as a cache in our microservices architecture. We can also use it as a NoSQL database.

而且,它与Spring Boot应用程序配对良好。例如,我们可以将其作为微服务架构中的缓存。我们还可以把它作为一个NoSQL数据库。

3. Running Redis

3.运行Redis

To get started, let’s create a Redis instance using their official Docker image.

为了开始,让我们使用他们的官方Docker镜像创建一个Redis实例。

$ docker run -p 16379:6379 -d redis:6.0 redis-server --requirepass "mypass"

Above, we’ve just started an instance of Redis on port 16379 with a password of mypass.

上面,我们刚刚在端口16379上启动了一个Redis的实例,密码为mypass

4. Starter

4.启动器

Spring gives us great support for connecting our Spring Boot applications with Redis using Spring Data Redis.

Spring为我们提供了极大的支持,可以使用Spring Data Redis连接我们的Spring Boot应用程序和Redis。

So, next, let’s make sure we’ve got the spring-boot-starter-data-redis dependency in our pom.xml:

所以,接下来,让我们确保在我们的pom.xml中得到spring-boot-starter-data-redis依赖性。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.7.2</version>    
</dependency>

5. Lettuce

5.莴苣

Next, let’s configure the client.

接下来,让我们配置一下客户端。

The Java Redis client we’ll use is Lettuce since Spring Boot uses it by default. However, we could have also used Jedis.

我们将使用的Java Redis客户端Lettuce,因为Spring Boot默认使用它。然而,我们也可以使用Jedis

Either way, the result is an instance of RedisTemplate:

无论哪种方式,结果都是一个RedisTemplate的实例。

@Bean
public RedisTemplate<Long, Book> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Long, Book> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    // Add some specific configuration here. Key serializers, etc.
    return template;
}

6. Properties

6.财产

When we use Lettuce, we don’t need to configure the RedisConnectionFactory. Spring Boot does it for us.

当我们使用Lettuce时,我们不需要配置RedisConnectionFactory.Spring Boot为我们做这个。

All we have left, then, is to specify a few properties in our application.properties file:

那么,我们剩下的就是在我们的application.properties文件中指定几个属性。

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=16379
spring.redis.password=mypass
spring.redis.timeout=60000

Respectively:

分别是

  • database sets the database index used by the connection factory
  • host is where the server host is located
  • port indicates the port where the server is listening
  • password is the login password for the server, and
  • timeout establishes the connection timeout

Of course, there are a lot of other properties we can configure. The complete list of configuration properties is available in the Spring Boot documentation.

当然,我们还可以配置很多其他的属性。配置属性的完整列表可在Spring Boot文档中找到。

7. Demo

7.演示

Finally, let’s try using it in our application. If we imagine a Book class and a BookRepository, we can create and retrieve Books, using our RedisTemplate to interact with Redis as our backend:

最后,让我们尝试在我们的应用程序中使用它。如果我们想象一个Book类和一个BookRepository,我们可以创建和检索Books,使用我们的RedisTemplate与Redis作为我们的后端互动。

@Autowired
private RedisTemplate<Long, Book> redisTemplate;

public void save(Book book) {
    redisTemplate.opsForValue().set(book.getId(), book);
}

public Book findById(Long id) {
    return redisTemplate.opsForValue().get(id);
}

By default, Lettuce will manage serialization and deserialization for us, so there’s nothing more to do at this point. However, it’s good to know that this also can be configured.

默认情况下,Lettuce将为我们管理序列化和反序列化,所以在这一点上没有更多的事情要做。然而,很高兴知道这也是可以配置的。

Another important feature is since RedisTemplate is thread-safe, so it’ll work properly in multi-threaded environments.

另一个重要的特点是,由于RedisTemplate是线程安全的,所以它将在多线程环境中正常工作

8. Conclusion

8.结语

In this article, we configured Spring Boot to talk to Redis via Lettuce. And, we achieved it with a starter, a single @Bean configuration, and a handful of properties.

在这篇文章中,我们将Spring Boot配置为通过Lettuce与Redis对话。而且,我们通过一个启动器、一个@Bean配置和一些属性就实现了。

To wrap up, we used the RedisTemplate to have Redis act as a simple backend.

总结一下,我们使用RedisTemplate来让Redis作为一个简单的后端。

The full example can be found over on GitHub.

完整的例子可以在GitHub上找到over