Add ConnectionDetail support to Kafka auto-configuration
Update Kafka auto-configuration so that `KafkaConnectionDetails` beans may be optionally used to provide connection details. See gh-34657 Co-Authored-By: Mortitz Halbritter <mkammerer@vmware.com> Co-Authored-By: Phillip Webb <pwebb@vmware.com>pull/34759/head
parent
d860d875b9
commit
042f0c8520
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.boot.autoconfigure.kafka;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
||||
|
||||
/**
|
||||
* Details required to establish a connection to a Kafka service.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public interface KafkaConnectionDetails extends ConnectionDetails {
|
||||
|
||||
/**
|
||||
* Returns the list of bootstrap nodes.
|
||||
* @return the list of bootstrap nodes
|
||||
*/
|
||||
List<Node> getBootstrapNodes();
|
||||
|
||||
/**
|
||||
* Returns the list of bootstrap nodes used for consumers.
|
||||
* @return the list of bootstrap nodes used for consumers
|
||||
*/
|
||||
default List<Node> getConsumerBootstrapNodes() {
|
||||
return getBootstrapNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of bootstrap nodes used for producers.
|
||||
* @return the list of bootstrap nodes used for producers
|
||||
*/
|
||||
default List<Node> getProducerBootstrapNodes() {
|
||||
return getBootstrapNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of bootstrap nodes used for the admin.
|
||||
* @return the list of bootstrap nodes used for the admin
|
||||
*/
|
||||
default List<Node> getAdminBootstrapNodes() {
|
||||
return getBootstrapNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of bootstrap nodes used for Kafka Streams.
|
||||
* @return the list of bootstrap nodes used for Kafka Streams
|
||||
*/
|
||||
default List<Node> getStreamsBootstrapNodes() {
|
||||
return getBootstrapNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* A Kafka node.
|
||||
*
|
||||
* @param host the hostname
|
||||
* @param port the port
|
||||
*/
|
||||
record Node(String host, int port) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.boot.autoconfigure.kafka;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Adapts {@link KafkaProperties} to {@link KafkaConnectionDetails}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class PropertiesKafkaConnectionDetails implements KafkaConnectionDetails {
|
||||
|
||||
private final int DEFAULT_PORT = 9092;
|
||||
|
||||
private final KafkaProperties properties;
|
||||
|
||||
PropertiesKafkaConnectionDetails(KafkaProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node> getBootstrapNodes() {
|
||||
return asNodes(this.properties.getBootstrapServers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node> getConsumerBootstrapNodes() {
|
||||
return bootstrapNodes(this.properties.getConsumer().getBootstrapServers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node> getProducerBootstrapNodes() {
|
||||
return bootstrapNodes(this.properties.getProducer().getBootstrapServers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node> getStreamsBootstrapNodes() {
|
||||
return bootstrapNodes(this.properties.getStreams().getBootstrapServers());
|
||||
}
|
||||
|
||||
private List<Node> bootstrapNodes(List<String> bootstrapServers) {
|
||||
return (bootstrapServers != null) ? asNodes(bootstrapServers) : getBootstrapNodes();
|
||||
}
|
||||
|
||||
private List<Node> asNodes(List<String> bootstrapServers) {
|
||||
return bootstrapServers.stream().map(this::asNode).toList();
|
||||
}
|
||||
|
||||
private Node asNode(String bootstrapNode) {
|
||||
int separatorIndex = bootstrapNode.indexOf(':');
|
||||
if (separatorIndex == -1) {
|
||||
return new Node(bootstrapNode, this.DEFAULT_PORT);
|
||||
}
|
||||
return new Node(bootstrapNode.substring(0, separatorIndex),
|
||||
Integer.parseInt(bootstrapNode.substring(separatorIndex + 1)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue