|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.streamnative.examples.kafka; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Properties; |
| 20 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 21 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 22 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 23 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 24 | + |
| 25 | +/** |
| 26 | + * A token authentication example of Kafka consumer. |
| 27 | + */ |
| 28 | +public class TokenConsumer { |
| 29 | + public static void main(String[] args) throws IOException { |
| 30 | + // 1. Get the configured parameters from token.properties |
| 31 | + final Properties tokenProps = new Properties(); |
| 32 | + tokenProps.load(TokenProducer.class.getClassLoader().getResourceAsStream("token.properties")); |
| 33 | + final String bootstrapServers = tokenProps.getProperty("bootstrap.servers"); |
| 34 | + final String topic = tokenProps.getProperty("topic"); |
| 35 | + final String group = tokenProps.getProperty("group"); |
| 36 | + final String namespace = tokenProps.getProperty("namespace"); |
| 37 | + final String token = tokenProps.getProperty("token"); |
| 38 | + |
| 39 | + // 2. Create a consumer with token authentication, which is equivalent to SASL/PLAIN mechanism in Kafka |
| 40 | + final Properties props = new Properties(); |
| 41 | + props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| 42 | + props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 43 | + props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 44 | + props.put(ConsumerConfig.GROUP_ID_CONFIG, group); |
| 45 | + props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 46 | + props.put("security.protocol", "SASL_PLAINTEXT"); |
| 47 | + props.put("sasl.mechanism", "PLAIN"); |
| 48 | + props.put("sasl.jaas.config", String.format( |
| 49 | + "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"%s\" password=\"%s\";", |
| 50 | + namespace, token)); |
| 51 | + final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); |
| 52 | + consumer.subscribe(Collections.singleton(topic)); |
| 53 | + |
| 54 | + // 3. Consume some messages and quit immediately |
| 55 | + boolean running = true; |
| 56 | + while (running) { |
| 57 | + final ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1)); |
| 58 | + if (!records.isEmpty()) { |
| 59 | + records.forEach(record -> System.out.println("Receive record: " + record.value() + " from " |
| 60 | + + record.topic() + "-" + record.partition() + "@" + record.offset())); |
| 61 | + running = false; |
| 62 | + } |
| 63 | + } |
| 64 | + consumer.close(); |
| 65 | + } |
| 66 | +} |
0 commit comments