spring-data-redis - 工厂方法“redisConnectionFactory”抛出异常;主机名不能为 null 或为空
问题描述
我已经使用 gradle 在我的 spring boot 项目中实现了 redis 配置。但是当我运行该项目时,我收到错误消息说 bean 初始化失败。
以下是我的配置文件。
package com.nsdlegov.vsp.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisNewConfig {
@Autowired
private Environment environment;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(environment.getProperty("spring.redis.master"), 6379);
return new LettuceConnectionFactory(serverConfig);
}
@Bean
public RedisTemplate<String,SchemeMaster> redisTemplateSchemeMaster(){
RedisTemplate<String,SchemeMaster> template= new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
template.setEnableTransactionSupport(true);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(SchemeMaster.class));
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(SchemeMaster.class));
template.setEnableTransactionSupport(true);
return template;
}
@Bean
public RedisTemplate<String,PincodeMaster> redisTemplatePincodeMaster() {
RedisTemplate<String, PincodeMaster> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
template.setEnableTransactionSupport(true);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(PincodeMaster.class));
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(PincodeMaster.class));
return template;
}
@Bean
public RedisTemplate<String,IfscMaster> redisTemplateIfscMaster() {
RedisTemplate<String, IfscMaster> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
template.setEnableTransactionSupport(true);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(IfscMaster.class));
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(IfscMaster.class));
return template;
}
}
这是我的 build.gradle 文件:
buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
ext['springCloudVersion'] = 'Finchley.SR1'
repositories {
mavenCentral()
}
dependencies {
compile 'io.lettuce:lettuce-core:5.1.1.RELEASE'
compile 'org.springframework.data:spring-data-redis:2.1.1.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web:2.1.2.RELEASE'
compile 'org.postgresql:postgresql:42.2.5'
compile 'javax.activation:activation:1.1.1'
compile 'org.glassfish.jaxb:jaxb-runtime:2.3.0.1'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.1.2.RELEASE'
compile 'org.hibernate:hibernate-core:5.3.7.Final'
compile 'org.projectlombok:lombok:1.18.4'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version:'2.0.1.RELEASE'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
group = 'com.nsdlegov.vsp'
version = '0.0.1-SNAPSHOT'
description = 'vsp-redis-service'
sourceCompatibility = '1.8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
我收到以下错误:
aused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [com/nsdlegov/vsp/config/RedisNewConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method 'redisConnectionFactory' threw exception; nested exception is java.lang.IllegalArgumentException: Host name must not be null or empty!
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$128/46941357.getObject(Unknown Source) ~[na:na]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1237) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 22 common frames omitted
首先,该项目在 Maven 中。但最近我已将其转换为 gradle 项目。在 Maven 中它运行良好,但在 gradle 中它给了我同样的错误。
请帮助我,并提前感谢。
解决方案
您是否尝试过删除 spring.redis.url 并改用 spring.redis.host ?我已经完成并修复了错误。
推荐阅读
- php - How to pass data to view only if it is defined?
- c++ - Boost.Spirit.Qi - 对原始数据类型进行边界检查
- azure-devops - VSTS 历史查询
- docker - 通过命令行安装 julia 包
- sql - 如何从表名查询中获取选择计数(x)
- abcpdf - 使用 ABCPDF 将另一个文档绘制为带圆角的图像
- react-native - 在反应原生应用程序中使用 styleguidist
- docker - .NET Core API 502 带有 Nginx 代理的 Docker 上的错误网关
- javascript - 知道表单 POST 目标是否生成错误的任何方法
- cmake - CMake:add_subdirectory 两次作为共享项目