|
| 1 | +/* |
| 2 | + * Copyright 2023 JBoss Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.apicurio.registry.examples.avro.bean; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Date; |
| 22 | +import java.util.Properties; |
| 23 | + |
| 24 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 25 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 26 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 27 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 28 | +import org.apache.kafka.clients.producer.Producer; |
| 29 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 30 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 31 | +import org.apache.kafka.common.config.SaslConfigs; |
| 32 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 33 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 34 | + |
| 35 | +import io.apicurio.registry.serde.SerdeConfig; |
| 36 | +import io.apicurio.registry.serde.avro.AvroKafkaDeserializer; |
| 37 | +import io.apicurio.registry.serde.avro.AvroKafkaSerdeConfig; |
| 38 | +import io.apicurio.registry.serde.avro.AvroKafkaSerializer; |
| 39 | +import io.apicurio.registry.serde.avro.ReflectAvroDatumProvider; |
| 40 | + |
| 41 | +/** |
| 42 | + * This example demonstrates how to use the Apicurio Registry in a very simple publish/subscribe |
| 43 | + * scenario with Avro as the serialization type. The following aspects are demonstrated: |
| 44 | + * |
| 45 | + * <ol> |
| 46 | + * <li>Configuring a Kafka Serializer for use with Apicurio Registry</li> |
| 47 | + * <li>Configuring a Kafka Deserializer for use with Apicurio Registry</li> |
| 48 | + * <li>Auto-register the Avro schema in the registry (registered by the producer)</li> |
| 49 | + * <li>Data sent as a {@link GreetingBean}</li> |
| 50 | + * </ol> |
| 51 | + * |
| 52 | + * Pre-requisites: |
| 53 | + * |
| 54 | + * <ul> |
| 55 | + * <li>Kafka must be running on localhost:9092</li> |
| 56 | + * <li>Apicurio Registry must be running on localhost:8080</li> |
| 57 | + * </ul> |
| 58 | + * |
| 59 | + * @author eric.wittmann@gmail.com |
| 60 | + * @author carles.arnal@redhat.com |
| 61 | + */ |
| 62 | +public class AvroBeanExample { |
| 63 | + |
| 64 | + private static final String REGISTRY_URL = "http://localhost:8080/apis/registry/v2"; |
| 65 | + private static final String SERVERS = "localhost:9092"; |
| 66 | + private static final String TOPIC_NAME = AvroBeanExample.class.getSimpleName(); |
| 67 | + private static final String SUBJECT_NAME = "Greeting"; |
| 68 | + |
| 69 | + |
| 70 | + public static final void main(String [] args) throws Exception { |
| 71 | + System.out.println("Starting example " + AvroBeanExample.class.getSimpleName()); |
| 72 | + String topicName = TOPIC_NAME; |
| 73 | + String subjectName = SUBJECT_NAME; |
| 74 | + |
| 75 | + // Create the producer. |
| 76 | + Producer<Object, Object> producer = createKafkaProducer(); |
| 77 | + // Produce 5 messages. |
| 78 | + int producedMessages = 0; |
| 79 | + try { |
| 80 | + System.out.println("Producing (5) messages."); |
| 81 | + for (int idx = 0; idx < 5; idx++) { |
| 82 | + GreetingBean greeting = new GreetingBean(); |
| 83 | + greeting.setMessage("Hello (" + producedMessages++ + ")!"); |
| 84 | + greeting.setTime(System.currentTimeMillis()); |
| 85 | + |
| 86 | + |
| 87 | + // Send/produce the message on the Kafka Producer |
| 88 | + ProducerRecord<Object, Object> producedRecord = new ProducerRecord<>(topicName, subjectName, greeting); |
| 89 | + producer.send(producedRecord); |
| 90 | + |
| 91 | + Thread.sleep(100); |
| 92 | + } |
| 93 | + System.out.println("Messages successfully produced."); |
| 94 | + } finally { |
| 95 | + System.out.println("Closing the producer."); |
| 96 | + producer.flush(); |
| 97 | + producer.close(); |
| 98 | + } |
| 99 | + |
| 100 | + // Create the consumer |
| 101 | + System.out.println("Creating the consumer."); |
| 102 | + KafkaConsumer<Long, GreetingBean> consumer = createKafkaConsumer(); |
| 103 | + |
| 104 | + // Subscribe to the topic |
| 105 | + System.out.println("Subscribing to topic " + topicName); |
| 106 | + consumer.subscribe(Collections.singletonList(topicName)); |
| 107 | + |
| 108 | + // Consume the 5 messages. |
| 109 | + try { |
| 110 | + int messageCount = 0; |
| 111 | + System.out.println("Consuming (5) messages."); |
| 112 | + while (messageCount < 5) { |
| 113 | + final ConsumerRecords<Long, GreetingBean> records = consumer.poll(Duration.ofSeconds(1)); |
| 114 | + messageCount += records.count(); |
| 115 | + if (records.count() == 0) { |
| 116 | + // Do nothing - no messages waiting. |
| 117 | + System.out.println("No messages waiting..."); |
| 118 | + } else records.forEach(record -> { |
| 119 | + GreetingBean greeting = record.value(); |
| 120 | + System.out.println("Consumed a message: " + greeting.getMessage() + " @ " + new Date(greeting.getTime())); |
| 121 | + }); |
| 122 | + } |
| 123 | + } finally { |
| 124 | + consumer.close(); |
| 125 | + } |
| 126 | + |
| 127 | + System.out.println("Done (success)."); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Creates the Kafka producer. |
| 132 | + */ |
| 133 | + private static Producer<Object, Object> createKafkaProducer() { |
| 134 | + Properties props = new Properties(); |
| 135 | + |
| 136 | + // Configure kafka settings |
| 137 | + props.putIfAbsent(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, SERVERS); |
| 138 | + props.putIfAbsent(ProducerConfig.CLIENT_ID_CONFIG, "Producer-" + TOPIC_NAME); |
| 139 | + props.putIfAbsent(ProducerConfig.ACKS_CONFIG, "all"); |
| 140 | + props.putIfAbsent(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); |
| 141 | + // Use the Apicurio Registry provided Kafka Serializer for Avro |
| 142 | + props.putIfAbsent(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, AvroKafkaSerializer.class.getName()); |
| 143 | + |
| 144 | + // Configure Service Registry location |
| 145 | + props.putIfAbsent(SerdeConfig.REGISTRY_URL, REGISTRY_URL); |
| 146 | + props.putIfAbsent(SerdeConfig.AUTO_REGISTER_ARTIFACT, Boolean.TRUE); |
| 147 | + // Use Java reflection as the Avro Datum Provider - this also generates an Avro schema from the java bean |
| 148 | + props.putIfAbsent(AvroKafkaSerdeConfig.AVRO_DATUM_PROVIDER, ReflectAvroDatumProvider.class.getName()); |
| 149 | + |
| 150 | + //Just if security values are present, then we configure them. |
| 151 | + configureSecurityIfPresent(props); |
| 152 | + |
| 153 | + // Create the Kafka producer |
| 154 | + Producer<Object, Object> producer = new KafkaProducer<>(props); |
| 155 | + |
| 156 | + return producer; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Creates the Kafka consumer. |
| 161 | + */ |
| 162 | + private static KafkaConsumer<Long, GreetingBean> createKafkaConsumer() { |
| 163 | + Properties props = new Properties(); |
| 164 | + |
| 165 | + // Configure Kafka |
| 166 | + props.putIfAbsent(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, SERVERS); |
| 167 | + props.putIfAbsent(ConsumerConfig.GROUP_ID_CONFIG, "Consumer-" + TOPIC_NAME); |
| 168 | + props.putIfAbsent(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true"); |
| 169 | + props.putIfAbsent(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000"); |
| 170 | + props.putIfAbsent(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 171 | + props.putIfAbsent(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); |
| 172 | + // Use the Apicurio Registry provided Kafka Deserializer for Avro |
| 173 | + props.putIfAbsent(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, AvroKafkaDeserializer.class.getName()); |
| 174 | + |
| 175 | + // Configure Service Registry location |
| 176 | + props.putIfAbsent(SerdeConfig.REGISTRY_URL, REGISTRY_URL); |
| 177 | + // Use Java reflection as the Avro Datum Provider |
| 178 | + props.putIfAbsent(AvroKafkaSerdeConfig.AVRO_DATUM_PROVIDER, ReflectAvroDatumProvider.class.getName()); |
| 179 | + // No other configuration needed for the deserializer, because the globalId of the schema |
| 180 | + // the deserializer should use is sent as part of the payload. So the deserializer simply |
| 181 | + // extracts that globalId and uses it to look up the Schema from the registry. |
| 182 | + |
| 183 | + //Just if security values are present, then we configure them. |
| 184 | + configureSecurityIfPresent(props); |
| 185 | + |
| 186 | + // Create the Kafka Consumer |
| 187 | + KafkaConsumer<Long, GreetingBean> consumer = new KafkaConsumer<>(props); |
| 188 | + return consumer; |
| 189 | + } |
| 190 | + |
| 191 | + public static void configureSecurityIfPresent(Properties props) { |
| 192 | + final String tokenEndpoint = System.getenv(SerdeConfig.AUTH_TOKEN_ENDPOINT); |
| 193 | + if (tokenEndpoint != null) { |
| 194 | + |
| 195 | + final String authClient = System.getenv(SerdeConfig.AUTH_CLIENT_ID); |
| 196 | + final String authSecret = System.getenv(SerdeConfig.AUTH_CLIENT_SECRET); |
| 197 | + |
| 198 | + props.putIfAbsent(SerdeConfig.AUTH_CLIENT_SECRET, authSecret); |
| 199 | + props.putIfAbsent(SerdeConfig.AUTH_CLIENT_ID, authClient); |
| 200 | + props.putIfAbsent(SerdeConfig.AUTH_TOKEN_ENDPOINT, tokenEndpoint); |
| 201 | + props.putIfAbsent(SaslConfigs.SASL_MECHANISM, "OAUTHBEARER"); |
| 202 | + props.putIfAbsent(SaslConfigs.SASL_LOGIN_CALLBACK_HANDLER_CLASS, "io.strimzi.kafka.oauth.client.JaasClientOauthLoginCallbackHandler"); |
| 203 | + props.putIfAbsent("security.protocol", "SASL_SSL"); |
| 204 | + |
| 205 | + props.putIfAbsent(SaslConfigs.SASL_JAAS_CONFIG, String.format("org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required " + |
| 206 | + " oauth.client.id=\"%s\" "+ |
| 207 | + " oauth.client.secret=\"%s\" "+ |
| 208 | + " oauth.token.endpoint.uri=\"%s\" ;", authClient, authSecret, tokenEndpoint)); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments