Java源码示例:org.apache.kafka.common.network.ListenerName
示例1
@Before
public void setUp() throws Exception {
zookeeper = new EmbeddedZookeeper();
zkConnect = String.format("localhost:%d", zookeeper.port());
configs = new Vector<>();
servers = new Vector<>();
for (int i = 0; i < numBrokers; i++) {
KafkaConfig config = getKafkaConfig(i);
configs.add(config);
KafkaServer server = TestUtils.createServer(config, Time.SYSTEM);
servers.add(server);
}
String[] serverUrls = new String[servers.size()];
ListenerName listenerType = ListenerName.forSecurityProtocol(getSecurityProtocol());
for (int i = 0; i < servers.size(); i++) {
serverUrls[i] =
Utils.formatAddress(
servers.get(i).config().advertisedListeners().head().host(),
servers.get(i).boundPort(listenerType)
);
}
bootstrapServers = Utils.join(serverUrls, ",");
}
示例2
@Before
public void setUp() throws Exception {
zookeeper = new EmbeddedZookeeper();
zkConnect = String.format("localhost:%d", zookeeper.port());
configs = new Vector<>();
servers = new Vector<>();
for (int i = 0; i < numBrokers; i++) {
KafkaConfig config = getKafkaConfig(i);
configs.add(config);
KafkaServer server = TestUtils.createServer(config, Time.SYSTEM);
servers.add(server);
}
String[] serverUrls = new String[servers.size()];
ListenerName listenerType = ListenerName.forSecurityProtocol(getSecurityProtocol());
for (int i = 0; i < servers.size(); i++) {
serverUrls[i] =
Utils.formatAddress(
servers.get(i).config().advertisedListeners().head().host(),
servers.get(i).boundPort(listenerType)
);
}
bootstrapServers = Utils.join(serverUrls, ",");
}
示例3
public static String getBrokers(String zkUrl, SecurityProtocol securityProtocol) {
ZkUtils zkUtils = getZkUtils(zkUrl);
Seq<Broker> brokersSeq = zkUtils.getAllBrokersInCluster();
Broker[] brokers = new Broker[brokersSeq.size()];
brokersSeq.copyToArray(brokers);
String brokersStr = Arrays.stream(brokers)
.map(b -> b.brokerEndPoint(
ListenerName.forSecurityProtocol(securityProtocol)).connectionString())
.reduce(null, (a, b) -> (a == null) ? b : a + "," + b);
return brokersStr;
}
示例4
public List<BrokerInfo> fetchAllBrokers(){
List<BrokerInfo> result = new ArrayList<>();
Seq<Broker> brokers = zkUtils.getAllBrokersInCluster();
Iterator<Broker> iterator = brokers.toList().iterator();
while(iterator.hasNext()){
Broker broker = iterator.next();
Node node = broker.getNode(ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT)).get();
result.add(new BrokerInfo(node.idString(), node.host(), node.port()));
}
return result;
}
示例5
@Override
public void configure(Map<String, ?> configs) {
RangerBasePlugin me = rangerPlugin;
if (me == null) {
synchronized(RangerKafkaAuthorizer.class) {
me = rangerPlugin;
if (me == null) {
try {
// Possible to override JAAS configuration which is used by Ranger, otherwise
// SASL_PLAINTEXT is used, which force Kafka to use 'sasl_plaintext.KafkaServer',
// if it's not defined, then it reverts to 'KafkaServer' configuration.
final Object jaasContext = configs.get("ranger.jaas.context");
final String listenerName = (jaasContext instanceof String
&& StringUtils.isNotEmpty((String) jaasContext)) ? (String) jaasContext
: SecurityProtocol.SASL_PLAINTEXT.name();
final String saslMechanism = SaslConfigs.GSSAPI_MECHANISM;
JaasContext context = JaasContext.loadServerContext(new ListenerName(listenerName), saslMechanism, configs);
LoginManager loginManager = LoginManager.acquireLoginManager(context, saslMechanism, KerberosLogin.class, configs);
Subject subject = loginManager.subject();
UserGroupInformation ugi = MiscUtil
.createUGIFromSubject(subject);
if (ugi != null) {
MiscUtil.setUGILoginUser(ugi, subject);
}
logger.info("LoginUser=" + MiscUtil.getUGILoginUser());
} catch (Throwable t) {
logger.error("Error getting principal.", t);
}
me = rangerPlugin = new RangerBasePlugin("kafka", "kafka");
}
}
}
logger.info("Calling plugin.init()");
rangerPlugin.init();
auditHandler = new RangerKafkaAuditHandler();
rangerPlugin.setResultProcessor(auditHandler);
}
示例6
public int getKafkaServerPort(int index) {
return kafkaServer.get(index).socketServer()
.boundPort(ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT));
}
示例7
/**
* This broker's `metadata.broker.list` value. Example: `127.0.0.1:9092`.
*
* You can use this to tell Kafka producers and consumers how to connect to this instance.
*
* This version returns the port of the first listener.
* @return the broker list
*/
public String brokerList() {
final ListenerName listenerName = kafka.config().advertisedListeners().apply(0).listenerName();
return kafka.config().hostName() + ":" + kafka.boundPort(listenerName);
}
示例8
/**
* The broker's `metadata.broker.list` value. Example: `127.0.0.1:9092`.
*
* You can use this to tell Kafka producers and consumers how to connect to this instance.
*
* @param securityProtocol the security protocol the returned broker list should use.
* @return the broker list
*/
public String brokerList(final SecurityProtocol securityProtocol) {
return kafka.config().hostName() + ":"
+ kafka.boundPort(new ListenerName(securityProtocol.toString()));
}