我最近将我的春季启动版本提升到了1.4.0.现在我在下面看到以下警告.我使用spring-mongodb和spring-redis(严格用于缓存).这是我应该关注的吗?
找到多个Spring Data模块,进入严格的存储库配置模式!
Spring Data Redis - 无法安全地识别存储库的存储分配
严格的存储库配置模式究竟意味着什么?
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.0.RELEASE)
2016-09-11 00:03:11.234 INFO 24766 --- [ …Run Code Online (Sandbox Code Playgroud) 我正在使用用于缓存的spring-boot spring-data-redis 1.8.9.RELEASE RedisCacheManager实现CacheManager.我想要查看的一个指标是缓存命中/未命中率.为了实现这一点,我正在提取通过redis服务器公开的keyspace_hits和keyspace_misses,也可以通过redis_cli查看INFO STATS.问题是RedisCacheManager从不注册缓存未命中,即即使存在缓存"未命中",keyspace_misses也不会增加.
调试代码,我看到spring-data-redis实际上EXISTS在检索之前检查redis中的密钥.我看到了这种方法的意义,但是当EXISTS对redis服务器执行时,它没有注册缓存未命中.
有没有办法使用RedisCacheManager并注册缓存未命中?我知道我可以使用其他redis对象来实现这一目标,但我想知道是否可以使用标准的CacheManager实现来完成它?
编辑
理想的解决方案不会增加大量开销,我无法编辑redis服务器的配置.
RedisCacheManager从缓存中检索元素时使用的代码.通知Boolean exists:
public RedisCacheElement get(final RedisCacheKey cacheKey) {
Assert.notNull(cacheKey, "CacheKey must not be null!");
Boolean exists = (Boolean)this.redisOperations.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(cacheKey.getKeyBytes());
}
});
return !exists ? null : new RedisCacheElement(cacheKey, this.fromStoreValue(this.lookup(cacheKey)));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将通过MONITOR高速缓存未命中在redis 上执行这些命令.再次注意,EXISTS根据代码执行:
执行上述命令后,keyspace_misses即使存在高速缓存未命中,也不会递增:
我使用Spring Boot 2和Spring Data,Spring-Data-Elastisearch和Spring-data-Redis(用于http会话).当我启动应用程序.我收到了
2017-10-29 17:38:33.376 INFO 18625 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2017-10-29 17:38:33.451 INFO 18625 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2017-10-29 17:38:33.461 INFO 18625 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2017-10-29 17:38:33.768 INFO 18625 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! …Run Code Online (Sandbox Code Playgroud) spring-data spring-data-jpa spring-data-redis spring-boot spring-data-elasticsearch
我使用连接池创建连接工厂的配置.我有一个连接池.大部分代码都是从Spring中复制的RedisAutoConfiguration,我因特殊原因而禁用了它.
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class JedisConfiguration implements RedisConfiguration {
@Bean
@Scope("prototype")
@Override
public RedisConnectionFactory connectionFactory(RedisProperties redisProperties) {
return createFactory(redisProperties);
}
private static JedisConnectionFactory applyProperties(RedisProperties properties, JedisConnectionFactory factory) {
factory.setHostName(properties.getHost());
factory.setPort(properties.getPort());
factory.setDatabase(properties.getDatabase());
return factory;
}
private static JedisPoolConfig jedisPoolConfig(RedisProperties properties) {
return Optional.ofNullable(properties.getPool())
.map(props -> {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(props.getMaxActive());
config.setMaxIdle(props.getMaxIdle());
config.setMinIdle(props.getMinIdle());
config.setMaxWaitMillis(props.getMaxWait());
return config;
})
.orElseGet(JedisPoolConfig::new);
}
public static JedisConnectionFactory createFactory(RedisProperties properties) {
return applyProperties(properties, new JedisConnectionFactory(jedisPoolConfig(properties)));
}
}
Run Code Online (Sandbox Code Playgroud)
我有字符串键"A","B","C" …
我正在玩Spring Boot 2 webflux.我正在尝试使用它ReactiveSortingRepository来简化redis操作.
public interface DataProfileRepository extends ReactiveSortingRepository<DataProfileDTO, String> {
}
Run Code Online (Sandbox Code Playgroud)
只需使用此界面即可
Mono<DataProfileDTO> tmp = this.dataProfileRepository.findById(id);
Run Code Online (Sandbox Code Playgroud)
例外:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.tradeshift.dgps.dto.DataProfileDTO] to type [reactor.core.publisher.Mono<?>]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.data.repository.util.ReactiveWrapperConverters.toWrapper(ReactiveWrapperConverters.java:197) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:104) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:587) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
Run Code Online (Sandbox Code Playgroud)
被扔了.
此存储库的行为与reactor不匹配,我可以在调试模式中看到,实际DataProfileDTO是从redis获取的.尝试时失败了:
GENERIC_CONVERSION_SERVICE.convert(reactiveObject, targetWrapperType);
Run Code Online (Sandbox Code Playgroud)
在 ReactiveWrapperConverters.toWrapper
我去谷歌搜索,似乎Spring Data Redis 2.0没有提到反应式存储库支持.我想知道我的代码或Spring Data Redis 2.0中是否有任何错误,我还不支持ReactiveCrudRepository.
我使用spring boot webflux+ project reactor+lettuce连接和非阻塞方式查询Redis的。我已经配置了ReactiveRedisTemplate带LettuceConnectionFactory。spring 文档指出,使用管道的唯一方法ReactiveRedisTemplate是使用该execute(<RedisCallback>)方法。在 non-reactive 中RedisTemplate,我看到有一种executePipelined(<RedisCallback>)方法可以在执行回调之前打开/关闭管道。但是在ReactiveRedisTemplate.execute方法的情况下,它使用 aLettuceReactiveRedisConnection并且既Spring ReactiveRedisConnection没有Lettuce也没有没有对管道的引用。
所以我的问题是,是否可以在使用Spring ReactiveRedisTemplate+时流水线化您的命令ReactiveLettuceConnection?
我也注意到,使用ReactiveRedisTemplate.execute了RedisCallback具有多个Redis命令的执行速度比打电话只是单独的命令慢。
带有 ReactiveRedisTemplate 的管道示例代码:
reactiveRedisTemplate.execute(connection -> keys.flatMap(key ->
connection.hashCommands()
.hGetAll(ByteBuffer.wrap(key.getBytes()))))
.map(Map.Entry::getValue)
.map(ByteUtils::getBytes)
.map(b -> {
try {
return mapper.readValue(b, Value.class);
} catch (IOException e1) {
return null;
}
})
.collectList();
Run Code Online (Sandbox Code Playgroud)
没有管道的代码:
keys.flatMap(key -> reactiveRedisTemplate.opsForHash().entries(key))
.map(Map.Entry::getValue) …Run Code Online (Sandbox Code Playgroud) reactive-programming redis lettuce spring-data-redis project-reactor
我在Spring应用程序中启用了缓存,并使用Redis来实现此目的.但是,每当发生连接失败时,应用程序就会停止工作,而我认为最好跳过缓存并继续执行正常的执行流程.
那么,有没有人知道如何在Spring中优雅地做到这一点?
这是我得到的例外.
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) 我有一个 Spring Boot 2.5.4 应用程序,我想在其中添加 Redis 并通过 Spring Data Redis 访问它。我当前的配置如下所示:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/>
</parent>
<groupId>com.application</groupId>
<artifactId>ApiGateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ApiGateway</name>
<description>ApiGateway</description>
<properties>
<java.version>15</java.version>
<spring-boot-starter-redis.version>2.5.0</spring-boot-starter-redis.version>
<redis.version>3.1.0</redis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring-boot-starter-redis.version}</version>
</dependency>
<!-- OTHER DEPENDENCIES -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cognitoidp</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<!-- OTHER DEPENDENCIES --> …Run Code Online (Sandbox Code Playgroud) 当我尝试从Spring Data Redis注入实现CrudRepository的存储库时,我得到NoSuchBeanDefinitionException.
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[bluh.bluh.repository.XxxRepository]的限定bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
但是配置在那里,它用@EnableRedisRepositories("bluh.bluh.repository")注释.
@Configuration
@EnableRedisRepositories
public class ApplicationConfig {
@Bean
RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
@Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
return template;
}
}
Run Code Online (Sandbox Code Playgroud)
存储库界面如下所示:
import org.springframework.data.repository.CrudRepository;
public interface XxxRepository extends CrudRepository<String, String> { }
Run Code Online (Sandbox Code Playgroud)
我经历过http://docs.spring.io/spring-data/redis/docs/current/reference/html/,对我来说没什么新鲜的.我想知道我错过了什么,我会感激任何投入.
我使用Spring Data Redis 1.7.2.RELEASE,Spring Boot 1.3.6.RELEASE
我正在使用Spring数据Redis并拥有以下存储库:
public interface MyClassRepository extends CrudRepository<MyClass, String> {
}
Run Code Online (Sandbox Code Playgroud)
当我调用findAll(Iterable< String> ids)方法时,返回正确的数据:
final List<String> ids = Lists.newArrayList("id1", "id2");
final Iterable<MyClass> mappingIterable = mappingRepository.findAll(ids);
Run Code Online (Sandbox Code Playgroud)
但是,调用findAll()不返回数据,而是返回Redis null中每个id当前值的值:
final Iterable<MyClass> mappingIterable = mappingRepository.findAll();
Run Code Online (Sandbox Code Playgroud)
返回:
[null,null]
java ×5
spring-boot ×5
redis ×4
spring ×3
crud ×1
jedis ×1
lettuce ×1
reactive ×1
spring-cache ×1
spring-data ×1