kafka研究学习-1: 阿里云安装配置kafka, 本地idea方式AdminClient访问

假定阿里云外网ip为: 123.123.123.123

1. zookeeper配置文件 zoo.cfg

cat conf/zoo.cfg |grep -v "#"

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/install/zookeeper-3.5.8/data
clientPort=2181

改动之处只有 dataDir

2. kafka配置文件 server.properties

cat config/server.properties |grep -Ev "^$|#"

broker.id=0
listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://123.123.123.123:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/opt/install/kafka_2.11-2.4.1/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=123.123.123.123:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0

改动之处:
listeners=PLAINTEXT://0.0.0.0:9092 允许其他ip访问
advertised.listeners=PLAINTEXT://123.123.123.123:9092 外网通过ip+9092端口也可访问
log.dirs
zookeeper.connect 对外的外网ip+端口

3. AdminClient
    private static void listTopics() throws Exception {
        AdminClient adminClient = getAdminClient();
        // 1. 无参列出
//        adminClient.listTopics().names().get().stream().forEach(s->log.info("topic: {}", s));
        ListTopicsOptions options = new ListTopicsOptions();
        options.listInternal(true);
        adminClient.listTopics(options).names().get().stream().forEach(s -> log.info("topic: {}", s));
    }
    /**
     * 获取 AdminClient 对象
     *
     * @return
     */
    private static AdminClient getAdminClient() {
        Properties props = new Properties();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "123.123.123.123:9092");

        return AdminClient.create(props);
    }

你可能感兴趣的:(kafka)