标签: spring-data-redis

使用 spring-data-redis 1.7.0.M1 时如何配置 redis-cluster

我使用 spring-data-redis 版本 1.7.0.M1,jedis 版本 2.8.0\n这是我的配置

\n\n

\n\n\n\n\n\n\n\n\n\n\n

\n\n
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">\n    <property name="connectionFactory" ref="redisConnectionFactory"></property>\n    <property name="keySerializer">\n        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>\n    </property>\n    <property name="hashKeySerializer">\n        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>\n    </property>\n    <property name="valueSerializer">\n        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>\n    </property>\n    <property name="hashValueSerializer">\n        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>\n    </property>\n</bean>\n
Run Code Online (Sandbox Code Playgroud)\n\n

并使用 \xe3\x80\x90redisTemplate.opsForValue().get("foo")\xe3\x80\x91 进行测试

\n\n

抛出异常

\n\n
 org.springframework.dao.InvalidDataAccessApiUsageException: MOVED 12182 192.168.1.223:7002; nested exception is redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 12182 192.168.1.223:7002\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用spring-data-redis 1.7.0.M1时如何配置redis-cluster?

\n

redis spring-data-redis redis-cluster

5
推荐指数
1
解决办法
2万
查看次数

Spring Redis 哈希操作 SCAN

我正在做Spring Redis,我把钥匙作为

redistemplate.opsForHash().put("Customer", Customer.class, List<Customers>)
Run Code Online (Sandbox Code Playgroud)

我想从 搜索List<>,

ScanOptions options = ScanOptions.scanOptions().match(pattern).count(1).build();

Cursor<Entry<Object, Object>> keys = redistemplate.opsForHash().scan("Customer", options);
Run Code Online (Sandbox Code Playgroud)

也不工作。请帮忙!!

java spring redis spring-data-redis

5
推荐指数
1
解决办法
1万
查看次数

同一个 redis 实例可以支持多个 Spring 会话吗?

我们有几个使用 Java Spring 编写的 Web 应用程序,我们正在使用spring-data-redis@EnableRedisHttpSession。我想知道春季会议的内部结构是什么。在创建新会话之前,它会检查 Redis 数据库是否有重复的会话密钥?

我查看了 spring 文档,也进行了谷歌搜索,但无法得到明确的答案。

spring-data-redis spring-session

5
推荐指数
1
解决办法
2966
查看次数

在集成测试中禁用带有 Redis 的 Spring 会话

我正在将 spring 会话与 redis 一起使用,但我想在进行测试时禁用它。我的班级有注释:

@ActiveProfiles("integrationtests")
Run Code Online (Sandbox Code Playgroud)

我的 application-integrationtests.tml 文件包含:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
Run Code Online (Sandbox Code Playgroud)

但它仍然失败:

Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Run Code Online (Sandbox Code Playgroud)

如果我打开 redis-server ,测试就会工作,但我当然不想保持这种状态;)

//更新

我一直在尝试

@SpringBootTest(classes = {Application.class})
@ActiveProfiles("integrationtests")
Run Code Online (Sandbox Code Playgroud)

和排除 Redis 的 Application.class:

@SpringBootApplication(exclude={SessionAutoConfiguration.class, RedisAutoConfiguration.class, RedisHttpSessionConfiguration.class})
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)

但它失败了:

Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]
Run Code Online (Sandbox Code Playgroud)

spring autoconfigure debug看到我已经排除了这个类,但是没有效果: …

java spring redis spring-data-redis spring-session

5
推荐指数
1
解决办法
6025
查看次数

如何自动装配 RedisTemplate&lt;String,Object&gt;

我想在 Spring Boot 中使用 RedisTemplate。我可以成功使用StringRedisTemplate,但无法使用RedisTemplate。这是代码。

\n\n
@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class RedisEntityTests {\n\n    @Autowired\n    private StringRedisTemplate stringRedisTemplate;\n\n    @Autowired\n    private RedisTemplate<String, RedisEntity> redisTemplate;\n\n    // This test case can run successfully.\n    @Test\n    public void testString() {\n        // save string\n        stringRedisTemplate.opsForValue().set("aaa", "111");\n        Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));\n    }\n\n    // This test case I got error.\n    @Test\n    public void testObject() throws Exception {\n        // save object\n        RedisEntity redisEntity = new RedisEntity("Tom", 20);\n        redisTemplate.opsForValue().set(redisEntity.getName(), redisEntity);\n\n        Assert.assertEquals(20, (redisTemplate.opsForValue().get("Tom")).getAge().longValue());\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,运行测试方法\xef\xbc\x9atestObject(),这里是错误报告\xef\xbc\x9a

\n\n
\n

2017-12-17 16:12:12.079 错误 4708 --- [ main]\n ostest.context.TestContextManager :在允许 …

java redis spring-data spring-data-redis spring-boot

5
推荐指数
1
解决办法
9921
查看次数

Spring Data Redis -- 如何在 Spring Data Redis 中为变量提供别名?

在下面的代码中,如果我想在redis数据库中将id存储为student_id,将name存储为student_name,并且我不想更改java代码中的变量名称。有没有办法为变量提供别名,就像我们在 spring data jpa 中所做的那样,我们在 @Column 注释中指定列的名称。如果有人有相同的解决方案,请帮助我。

@RedisHash("Student")
public class Student implements Serializable
{
   @Id
   private Long id;
   private String name;

   private float marksObtained;
   private String schoolName;
}
Run Code Online (Sandbox Code Playgroud)

spring redis spring-data spring-data-redis spring-boot

5
推荐指数
0
解决办法
641
查看次数

Spring Data REDIS - 具有奇怪前缀的哈希键,并且 HSCAN 无法正确返回结果

我将 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)

java redis lettuce spring-data-redis

5
推荐指数
1
解决办法
5155
查看次数

使用 ssl 在 AWS 上使用 Spring-boot-starter-data-redis 启动错误

所以,今天遇到了一个有趣的问题,弄乱了 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)

spring-data spring-data-redis spring-boot

5
推荐指数
2
解决办法
2031
查看次数

spring-data-redis,空列表属性值变为null

我正在将一些微服务从 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 等。

显然,有多个可用的序列化器,但这似乎没有任何效果。任何想法? …

serialization lettuce spring-data-redis spring-boot

5
推荐指数
1
解决办法
1480
查看次数

为什么 spring-data-redis ZSetOperations 范围操作返回 Set 而不是 SortedSet?

ZSETs 用于在 redis 中存储排序集。我用于spring-data-redisZSetOperations。

所有范围方法都返回Set<>而不是SortedSet<>,我想知道其背后的原因是什么?

请参阅: https: //github.com/spring-projects/spring-data-redis/blob/master/src/main/java/org/springframework/data/redis/core/ZSetOperations.java#L207

另外,由于这些不返回排序集,所有反向范围操作是否都会变得多余,因为无论如何都不会维护顺序,并且我们不能只获取索引处的任何元素?

我可能会遗漏一些非常明显的东西,请原谅我在这件事上的天真。


我的用例:

我正在存储带有日期的值作为分数。我想获取两个日期之间的所有值,检查计数并使用最近的日期。

我本来打算做类似的事情:

// 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)

但返回类型rangeByScoreSet,因此没有“get”操作。

  • 我必须获得分数范围,然后自己对它们进行排序。

  • 或者我必须转换为数组并获取元素。

  • 或者做类似的事情values.iterator().next()

java spring redis spring-data-redis zset

5
推荐指数
0
解决办法
1329
查看次数