Skip to content

Commit

Permalink
Merge pull request #7 from guoshiqiufeng/dev
Browse files Browse the repository at this point in the history
0.4.0
  • Loading branch information
guoshiqiufeng authored Nov 21, 2024
2 parents 280d11e + 448507d commit 290a29c
Show file tree
Hide file tree
Showing 18 changed files with 466 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ https://guoshiqiufeng.github.io/spring-cloud-stream-redis/

### 开发框架

- Spring Cloud Stream 4.1.3
- Spring Boot
- Spring Cloud Stream 4
- Spring Boot 3

### 功能

Expand All @@ -39,7 +39,7 @@ https://guoshiqiufeng.github.io/spring-cloud-stream-redis/
<dependency>
<groupId>io.github.guoshiqiufeng.cloud</groupId>
<artifactId>spring-cloud-stream-dependencies</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<type>import</type>
</dependency>
</dependencies>
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ https://guoshiqiufeng.github.io/spring-cloud-stream-redis/en/

### Development Framework

- Spring Cloud Stream 4.1.3
- Spring Boot
- Spring Cloud Stream 4
- Spring Boot 3

### Features

Expand All @@ -41,7 +41,7 @@ https://guoshiqiufeng.github.io/spring-cloud-stream-redis/en/
<dependency>
<groupId>io.github.guoshiqiufeng.cloud</groupId>
<artifactId>spring-cloud-stream-dependencies</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<type>import</type>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (c) 2023-2024, fubluesky ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.guoshiqiufeng.cloud.stream.binder.redis.support.converter;

import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.github.guoshiqiufeng.cloud.stream.binder.redis.properties;

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -30,19 +31,19 @@ public class RedisBinderConfigurationPropertiesTest {
void testDefaultProperties() {
RedisProperties redisProperties = new RedisProperties();
RedisBinderConfigurationProperties properties = new RedisBinderConfigurationProperties(redisProperties);

assertEquals("localhost", properties.getConfiguration().getHost());
assertEquals(6379, properties.getConfiguration().getPort());
}

@Test
@Test
void testCustomProperties() {
RedisProperties redisProperties = new RedisProperties();
redisProperties.setHost("redis.example.com");
redisProperties.setPort(6380);

RedisBinderConfigurationProperties properties = new RedisBinderConfigurationProperties(redisProperties);

assertEquals("redis.example.com", properties.getConfiguration().getHost());
assertEquals(6380, properties.getConfiguration().getPort());
}
Expand Down
2 changes: 1 addition & 1 deletion binders/spring-cloud-stream-binder-redis/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dependencies {
api project(":binders:spring-cloud-stream-binder-redis-core")

annotationProcessor "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-actuator"
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
annotationProcessor "org.springframework.boot:spring-boot-autoconfigure"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.cloud.stream.config.ProducerMessageHandlerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.integration.redis.inbound.RedisStoreMessageSource;

Expand All @@ -41,7 +42,7 @@
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(Binder.class)
// @Import({RedisBinderHealthIndicatorConfiguration.class})
@Import({RedisBinderHealthIndicatorConfiguration.class})
@EnableConfigurationProperties({RedisProperties.class, RedisExtendedBindingProperties.class})
public class RedisBinderConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023-2024, fubluesky ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.guoshiqiufeng.cloud.stream.binder.redis.config;

import io.github.guoshiqiufeng.cloud.stream.binder.redis.health.RedisBinderHealth;
import io.github.guoshiqiufeng.cloud.stream.binder.redis.health.RedisBinderHealthIndicator;
import io.github.guoshiqiufeng.cloud.stream.binder.redis.properties.RedisBinderConfigurationProperties;
import io.github.guoshiqiufeng.cloud.stream.binder.redis.utils.RedisConnectionFactoryUtil;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
* Configuration for Redis binder health indicators.
*
* @author yanghq
* @version 1.0
* @since 2024/11/17 10:55
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.boot.actuate.health.HealthIndicator")
@ConditionalOnEnabledHealthIndicator("binders")
@ConditionalOnMissingBean(RedisBinderHealth.class)
public class RedisBinderHealthIndicatorConfiguration {

@Bean
public RedisBinderHealthIndicator redisBinderHealthIndicator(RedisBinderConfigurationProperties configurationProperties) {
RedisConnectionFactory connectionFactory = RedisConnectionFactoryUtil.getRedisConnectionFactory(
configurationProperties.getConfiguration());
return new RedisBinderHealthIndicator(connectionFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.guoshiqiufeng.cloud.stream.binder.redis.health;

import org.springframework.boot.actuate.health.HealthIndicator;

/**
* Health indicator for Redis binder.
* @author yanghq
* @version 1.0
* @since 2024/11/18 15:07
*/
public interface RedisBinderHealth extends HealthIndicator {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023-2024, fubluesky ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.guoshiqiufeng.cloud.stream.binder.redis.health;

import org.springframework.boot.actuate.data.redis.RedisHealthIndicator;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
* Health indicator for Redis binder.
*
* @author yanghq
* @version 1.0
* @since 2024/11/17 10:00
*/
public class RedisBinderHealthIndicator extends RedisHealthIndicator {

public RedisBinderHealthIndicator(RedisConnectionFactory connectionFactory) {
super(connectionFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2023-2024, fubluesky ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.guoshiqiufeng.cloud.stream.binder.redis.health;

import io.github.guoshiqiufeng.cloud.stream.binder.redis.RedisContainerTest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author yanghq
* @version 1.0
* @since 2024/11/17 17:04
*/
class RedisBinderHealthIndicatorIntegrationTest implements RedisContainerTest {

@Test
void shouldReportHealthWithRealRedis() {
RedisConnectionFactory connectionFactory = RedisContainerTest.connectionFactory();

RedisBinderHealthIndicator healthIndicator = new RedisBinderHealthIndicator(connectionFactory);
Health health = healthIndicator.health();

assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails())
.containsKey("version")
.containsKey("mode");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2023-2024, fubluesky ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.guoshiqiufeng.cloud.stream.binder.redis.health;

import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisServerCommands;

import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

/**
* @author yanghq
* @version 1.0
* @since 2024/11/17 17:04
*/
class RedisBinderHealthIndicatorTest {

private static final String REDIS_PONG_RESPONSE = "PONG";
private static final String REDIS_VERSION_KEY = "redis_version";

@Test
void healthUp() {
RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class);
RedisConnection redisConnection = mock(RedisConnection.class);
RedisServerCommands serverCommands = mock(RedisServerCommands.class);

when(connectionFactory.getConnection()).thenReturn(redisConnection);
when(redisConnection.ping()).thenReturn(REDIS_PONG_RESPONSE);
when(redisConnection.serverCommands()).thenReturn(serverCommands);

// Mock Redis info command response
Properties serverInfo = new Properties();
serverInfo.setProperty(REDIS_VERSION_KEY, "7.0.2");
when(serverCommands.info()).thenReturn(serverInfo);

RedisBinderHealthIndicator healthIndicator = new RedisBinderHealthIndicator(connectionFactory);
Health health = healthIndicator.health();

assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails())
.containsEntry("version", "7.0.2");

verify(redisConnection).close();
}

@Test
void healthDown() {
RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class);
RedisConnection redisConnection = mock(RedisConnection.class);
RedisServerCommands serverCommands = mock(RedisServerCommands.class);

when(connectionFactory.getConnection()).thenReturn(redisConnection);
when(redisConnection.ping()).thenReturn("ERROR");
when(redisConnection.serverCommands()).thenReturn(serverCommands);
when(serverCommands.info()).thenThrow(new RuntimeException("Redis is down"));

RedisBinderHealthIndicator healthIndicator = new RedisBinderHealthIndicator(connectionFactory);
Health health = healthIndicator.health();

assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails())
.containsKey("error")
.containsValue("java.lang.RuntimeException: Redis is down");

verify(redisConnection).close();
}

@Test
void healthDownOnException() {
RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class);
when(connectionFactory.getConnection())
.thenThrow(new RuntimeException("Connection failed"));

RedisBinderHealthIndicator healthIndicator = new RedisBinderHealthIndicator(connectionFactory);
Health health = healthIndicator.health();

assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails())
.containsKey("error")
.containsValue("java.lang.RuntimeException: Connection failed");
}
}
Loading

0 comments on commit 290a29c

Please sign in to comment.