Заводской метод ‘RedisConnectionFactory’ выдал исключение; Имя хоста не должно быть нулевым или пустым

#spring-data-redis

#весна-данные-redis

Вопрос:

я реализовал конфигурацию redis в своем проекте Spring boot с использованием gradle. Но когда я запускаю проект, я получаю сообщение об ошибке, в котором говорится, что инициализация компонента не удалась.

Ниже приведен мой файл конфигурации.

 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 project. В maven он работал нормально, но в gradle выдает мне ту же ошибку.

Пожалуйста, помогите мне и заранее спасибо.

Ответ №1:

Вы пробовали удалить spring.redis.url и использовать spring.redis.вместо этого хост? Я сделал это и исправил ошибку.