refactor: enforce kafka mq sink type

This commit is contained in:
lingniu
2026-07-01 10:38:17 +08:00
parent e2b6ab63d6
commit 303b2eb47e
3 changed files with 21 additions and 4 deletions

View File

@@ -23,8 +23,7 @@ import java.util.Properties;
* <li>{@code lingniu.ingest.sink.mq.enabled=true}(默认 true—— <b>总开关</b>
* 设为 false 时本模块完全不装配ingest-core 的 DisruptorEventBus 仍然运行但
* 没有 Kafka sink。
* <li>{@code lingniu.ingest.sink.mq.type=kafka}(默认 kafka—— 后端选择,预留
* rocketmq/pulsar 等扩展。
* <li>{@code lingniu.ingest.sink.mq.type=kafka}(默认 kafka—— 唯一生产 MQ 后端。
* </ol>
*
* <p>Producer 和 Consumer 是两个独立开关:{@code sink.mq.enabled=true} 只表示可以创建

View File

@@ -5,6 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ConfigurationProperties(prefix = "lingniu.ingest.sink.mq")
@@ -18,7 +19,7 @@ public class SinkMqProperties {
*/
private boolean enabled = true;
/** MQ 后端类型。目前仅支持 kafka预留 rocketmq/pulsar 等。 */
/** MQ 后端类型。生产链路只允许 kafka。 */
private String type = "kafka";
/** Kafka bootstrap servers生产环境应通过环境变量覆盖不建议使用默认开发地址。 */
private String bootstrapServers = "114.55.58.251:9092";
@@ -35,7 +36,13 @@ public class SinkMqProperties {
public boolean isEnabled() { return enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public void setType(String type) {
String value = type == null || type.isBlank() ? "kafka" : type.trim().toLowerCase(Locale.ROOT);
if (!"kafka".equals(value)) {
throw new IllegalStateException("sink.mq.type supports only kafka; configured value: " + type);
}
this.type = value;
}
public String getBootstrapServers() { return bootstrapServers; }
public void setBootstrapServers(String bootstrapServers) { this.bootstrapServers = bootstrapServers; }
public String getCompressionType() { return compressionType; }

View File

@@ -3,6 +3,7 @@ package com.lingniu.ingest.sink.mq;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class SinkMqPropertiesTest {
@@ -15,4 +16,14 @@ class SinkMqPropertiesTest {
props.setBootstrapServers("kafka.internal:9092");
assertThat(props.getBootstrapServers()).isEqualTo("kafka.internal:9092");
}
@Test
void rejectsUnsupportedMqTypeInsteadOfSilentlyDisablingKafkaBeans() {
SinkMqProperties props = new SinkMqProperties();
assertThatThrownBy(() -> props.setType("rocketmq"))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("sink.mq.type supports only kafka")
.hasMessageContaining("rocketmq");
}
}