Spring Boot反应式Redis 2连接工厂导致运行状况检查失败

时间:2019-04-14 00:47:40

标签: spring spring-boot redis

将Reactive Redis与Webflux一起使用时,有2个Redis连接工厂。有反应性的和无反应性的。非活动状态显示为"DOWN"状态,导致其余的运行状况检查失败。

为什么有2个连接工厂?

build.gradle.kts

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-redis-reactive:2.1.4.RELEASE")

    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("io.jsonwebtoken:jjwt:0.9.1")
    implementation("org.json:json:20180813")
    runtimeOnly("org.springframework.boot:spring-boot-devtools")

    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.security:spring-security-test")
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
}

RedisConfig.kt

@Configuration
@EnableConfigurationProperties
class RedisConfig(@Value("\${redis.host}") private val redisHost: String,
                  @Value("\${redis.port}") private val redisPort: Int) {

    @Bean
    fun reactiveRedisConnectionFactory(): ReactiveRedisConnectionFactory {
        return LettuceConnectionFactory(redisHost, redisPort)
    }

    @Bean
    fun keyCommands(reactiveRedisConnectionFactory: ReactiveRedisConnectionFactory): ReactiveKeyCommands {
        return reactiveRedisConnectionFactory.reactiveConnection.keyCommands()
    }

    @Bean
    fun stringCommands(reactiveRedisConnectionFactory: ReactiveRedisConnectionFactory): ReactiveStringCommands {
        return reactiveRedisConnectionFactory.reactiveConnection.stringCommands()
    }

}

本地主机:8080 /执行器/运行状况

{
    "status": "DOWN",
    "details": {
        "mongo": {
            "status": "UP",
            "details": {
                "version": "4.0.8"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 101241290752,
                "free": 96678289408,
                "threshold": 10485760
            }
        },
        "redis": {
            "status": "DOWN",
            "details": {
                "reactiveRedisConnectionFactory": {
                    "status": "UP",
                    "details": {
                        "version": "5.0.4"
                    }
                },
                "redisConnectionFactory": {
                    "status": "DOWN",
                    "details": {
                        "error": "org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379"
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不确定使用反应式Redis时为什么会有两个连接工厂,但是,我确实找到了一种临时解决方法来使运行状况检查通过。

我正在使用自己的属性来配置Redis redis.hostredis.port

通过切换为使用spring.redis.hostspring.redis.port,它还将同时配置第二个非反应连接工厂。由于它们现在都可以连接到Redis,因此运行状况检查将通过。

{
    "status": "UP",
    "details": {
        "mongo": {
            "status": "UP",
            "details": {
                "version": "4.0.4"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 499763888128,
                "free": 381951664128,
                "threshold": 10485760
            }
        },
        "redis": {
            "status": "UP",
            "details": {
                "reactiveRedisConnectionFactory": {
                    "status": "UP",
                    "details": {
                        "version": "5.0.4"
                    }
                },
                "redisConnectionFactory": {
                    "status": "UP",
                    "details": {
                        "version": "5.0.4"
                    }
                }
            }
        }
    }
}