簡體   English   中英

為什么需要創建一個Kafka使用者才能連接到Schema Registry?

[英]Why do I need to create a Kafka Consumer to connect to Schema Registry?

前記:我對Kafka還是陌生的。

我試圖從架構注冊表中獲取所有架構,但是我無法僅通過架構注冊表客戶端來實現。 只有在此之前我實例化KafkaConsumer,它才起作用。

不明白為什么。 這是代碼(使用了使用者)。

ConsumerConfig只是具有所有必需配置的一類。 包括架構注冊表URL。

Consumer<String, String>  consumer = new KafkaConsumer<String, String>(ConsumerConfig.get());
CachedSchemaRegistryClient client = new CachedSchemaRegistryClient(ConsumerConfig.getSchemaRegistryURL(), 30);
Collection<String> listOfSubjects = client.getAllSubjects();
consumer.close();

沒有消費者,我得到:

io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException:由於輸入結束,沒有內容要映射

對於消費者,一切正常。 我想知道是否有人可以解釋為什么會發生這種情況,我是否沒有理由讓我需要通過使用者連接到實際的Kafka集群才能訪問另一個端點上的Schema Registry。

您根本不必創建KafkaConsumer實例。 兩者都是完全獨立的。

如果只想從SchemaRegistry獲取所有主題和架構,則只需創建CachedSchemaRegistryClient實例並調用相關操作即可。

這是一個工作示例:

 private final static Map<String, Schema> schemas = new ConcurrentHashMap<>();
 protected static SchemaRegistryClient schemaRegistryClient;

 public static void main(String[] args) {
       String registryUrl = "http://localhost:8081";
        try {
            schemaRegistryClient = new CachedSchemaRegistryClient(registryUrl, 30);
            System.out.println(schemaRegistryClient);
            Collection<String> subjects = schemaRegistryClient.getAllSubjects();
            System.out.println(subjects);
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM