我每 5 分钟运行一次批处理作业,并且我不希望其他节点运行相同的作业,因此我使用 Jedis 锁来锁定对象 5 分钟。这样,如果其他节点尝试运行相同的作业,它们将不会获得锁定。作业在获取锁后开始,当我尝试从 Redis 读取它时,出现以下异常:
'Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:194)
... 40 more
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
at redis.clients.jedis.Connection.disconnect(Connection.java:224)
at redis.clients.jedis.BinaryClient.disconnect(BinaryClient.java:941)
at redis.clients.jedis.Connection.close(Connection.java:214)
at redis.clients.jedis.BinaryClient.close(BinaryClient.java:947)
at redis.clients.jedis.Jedis.close(Jedis.java:3412)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:117)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:836)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:434)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
at redis.clients.util.Pool.getResource(Pool.java:49)'
Run Code Online (Sandbox Code Playgroud)
这就是我的代码
@Bean
public Jedis getJedis()
{
Jedis jedis = new Jedis(this.redisHost, nteger.valueOf(this.redisPort));
jedis.auth(this.redisPassword);
return jedis;
}
Run Code Online (Sandbox Code Playgroud)
spring-boot Application.properties 文件
# DATA REDIS …Run Code Online (Sandbox Code Playgroud) 我目前正在将 Redis (3.2.100) 与 Spring data redis (1.8.9) 和 Jedis 连接器一起使用。当我对现有实体使用 save() 函数时,Redis 会删除我的实体并重新创建该实体。
就我而言,我需要保留这个现有实体并且仅更新实体的属性。(我有另一个线程同时读取同一实体)
在Spring文档(https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis.repositories.partial-updates)中,我发现了部分更新功能。不幸的是,文档中的示例使用了 RedisTemplate 的 update() 方法。但这种方法不存在。
那么你使用过 Spring-data-redis 部分更新吗?
还有另一种方法可以更新实体redis而不需要删除之前吗?
谢谢
我正在使用spring-data-redis,spring-session和 Spring Boot 连接到我的 Redis 实例。但是,我不想spring-data-redis连接到 DB 0(默认),而是连接到另一个本地数据库(比如 DB 1)。这是我想要存储会话的地方。spring-data-redis 可以实现吗?
我将 spring boot (无关)与 spring-data-redis:jar:2.0.9 结合使用,它使用 lettuce 连接到我的 REDIS。我使用的哈希结构包含大约 100 个键。在这些键下,我放置了一些类型也不相关的对象:
private static final String HASH_KEY_NAME = "myspecialhashes:somekey";
@Autowired
private RedisTemplate<String, MyDto> myDtoRedisTemplate;
Run Code Online (Sandbox Code Playgroud)
现在我只是将对象列表放入哈希中,使用它们的 id 作为键:
myDtoRedisTemplate.opsForHash().put(HASH_KEY_NAME, dto.getId(), dto);
Run Code Online (Sandbox Code Playgroud)
这工作得很好,并且从哈希中检索所有元素都很好,并且仅检索键
List allDtosRaw = myDtoRedisTemplate.opsForHash().values(HASH_KEY_NAME);
Run Code Online (Sandbox Code Playgroud)
另外,在列出键时:
myDtoRedisTemplate.boundHashOps(HASH_KEY_NAME).keys()
Run Code Online (Sandbox Code Playgroud)
看起来不错,返回的密钥集以以下内容开头:
(java.util.LinkedHashSet<E>) [fakeservicetest:dummy3:write, fakesingle:dummy:sub1:write, ....
Run Code Online (Sandbox Code Playgroud)
由于有很多键,我希望能够使用 HSCAN 过滤以令牌开头的对象列表,而不是获取所有键并在我的 Java 应用程序中过滤它们。所以,这就是我如何进行 HSCAN 来获取所有以“fake”开头的哈希条目
List filteredDtosRaw = new LinkedList<>();
ScanOptions scanOptions = ScanOptions.scanOptions().match("fake*").count(10000).build();
Cursor cursor = myDtoRedisTemplate.boundHashOps(HASH_KEY_NAME).scan(scanOptions);
cursor.forEachRemaining(filteredDtosRaw ::add);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这返回零结果。我尝试了各种方法来解决这个问题并取得了一些成果。最终我转向 redis 命令行来看看 REDIS 是如何看待这一切的
redis-cli HSCAN "myspecialhashes:somekey" 0 MATCH "fake*" COUNT 1000
Run Code Online (Sandbox Code Playgroud)
确实是零结果。接下来的事情是查看其中的所有键并查看哈希中实际包含的内容
redis-cli …Run Code Online (Sandbox Code Playgroud) 所以,今天遇到了一个有趣的问题,弄乱了 spring-boot-starter-data-rest。我的用例是在 Redis 上存储一些具有一定过期时间的数据,以便在该时间过去后它将被驱逐。我已配置好所有内容,并且可以在本地 redis 上正常运行,没有任何问题。当尝试使用安全连接 (SSL) 在 AWS 上使用它时,它会成为一个问题,并且是一个示例错误:
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
at com.springbootapp.config.Application.main(Application.java:16)
... 6 more
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: ERR Unsupported CONFIG parameter: notify-keyspace-events; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: notify-keyspace-events
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:44)
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:210)
at org.springframework.data.redis.connection.jedis.JedisConnection.setConfig(JedisConnection.java:633)
at org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.configure(ConfigureNotifyKeyspaceEventsAction.java:63)
at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration$EnableRedisKeyspaceNotificationsInitializer.afterPropertiesSet(RedisHttpSessionConfiguration.java:167) …Run Code Online (Sandbox Code Playgroud) 我正在将一些微服务从 SpringBoot1.5 移植到 2.1。
我们正在使用 spring-data-redis。似乎默认的内部移动从 jedis 到 lettuce。
问题是我们现在观察到一些奇怪的行为,当我们保存一个对象然后检索它时,有一个微小的差异:
空列表属性将替换为 null。
这是一个例子:
//repo
public interface TestRepository extends CrudRepository<Test, String> {}
...
//object
@RedisHash(timeToLive = 60)
public static class Test{
@Id private String id;
int age;
List<String> friends;
}
...
//saving then retreiving
Test test = new Test("1", 15, Collections.emptyList());
System.out.println(test);
testRepository.save(test);
Test testGet = testRepository.findById("1").get();
System.out.println(testGet);
Run Code Online (Sandbox Code Playgroud)
发生的事情是这样的:
//before
{
"id": "1",
"age": 15,
"friends": []
}
//after
{
"id": "1",
"age": 15
}
Run Code Online (Sandbox Code Playgroud)
空列表friends消失了。这种新行为会在许多地方影响我们的代码,导致 NullPointerException 等。
显然,有多个可用的序列化器,但这似乎没有任何效果。任何想法? …
ZSETs 用于在 redis 中存储排序集。我用于spring-data-redisZSetOperations。
所有范围方法都返回Set<>而不是SortedSet<>,我想知道其背后的原因是什么?
另外,由于这些不返回排序集,所有反向范围操作是否都会变得多余,因为无论如何都不会维护顺序,并且我们不能只获取索引处的任何元素?
我可能会遗漏一些非常明显的东西,请原谅我在这件事上的天真。
我的用例:
我正在存储带有日期的值作为分数。我想获取两个日期之间的所有值,检查计数并使用最近的日期。
我本来打算做类似的事情:
// get reverse sorted values, with most recent date being on top
SrotedSet<String> values = zSetOperations.rangeByScore(key, this.getBeginDate(), this.getEndDate());
// check values size and do values.get(0) i.e. the most recent date.
Run Code Online (Sandbox Code Playgroud)
但返回类型rangeByScore为Set,因此没有“get”操作。
我必须获得分数范围,然后自己对它们进行排序。
或者我必须转换为数组并获取元素。
或者做类似的事情values.iterator().next()
我正在使用 Spring Data Redis 存储库来检索和持久化到 Redis 中。但我需要 id 由 2 个字段组成。如何才能实现这一目标?有没有相当于@EmbeddedId的方法?
我是反应式编程的新手。我需要连接到 Redis 来保存和获取一些数据。Redis 实例存在于云中。我使用 Lettuce Connection 工厂来建立连接。
与redis建立连接时,请求失败。这是我的 Redis 配置类:
package com.sap.slh.tax.attributes.determination.springwebfluxdemo.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableAsync;
import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;
import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;
import com.sap.slh.tax.attributes.determination.springwebfluxdemo.util.JsonUtil;
@Configuration
@EnableAsync
public class RedisConfig {
private static final Logger log = LoggerFactory.getLogger(RedisConfig.class);
@Value("${vcap.services.redis.credentials.hostname:10.11.241.101}")
private String host;
@Value("${vcap.services.redis.credentials.port:36516}")
private int port;
@Value("$vcap.services.redis.credentials.password:123456788")
private String password;
@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() { …Run Code Online (Sandbox Code Playgroud) redis lettuce spring-data-redis reactive spring-data-redis-reactive
当redis通过spring boot()作为缓存技术时<artifactId>spring-boot-starter-data-redis</artifactId>,我看到文件中很少有像TTL这样的属性可以设置application.properties。前任:
spring.cache.cache-names=cache1,cache2
spring.cache.redis.time-to-live=600000
Run Code Online (Sandbox Code Playgroud)
以及附录 A 中的更多片段。常见应用程序属性
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何设置缓存逐出策略,例如 - 最不常用或最近最后使用等。
我必须如何以及在哪里提供此配置详细信息?