-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
rangiffler-auth/src/main/java/com/rangiffler/config/KafkaAuthProducerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.rangiffler.config; | ||
|
||
import com.rangiffler.model.UserJson; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.kafka.KafkaProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.kafka.config.TopicBuilder; | ||
import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.core.ProducerFactory; | ||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.apache.kafka.clients.producer.ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG; | ||
import static org.apache.kafka.clients.producer.ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG; | ||
|
||
@Configuration | ||
public class KafkaAuthProducerConfiguration { | ||
private final KafkaProperties kafkaProperties; | ||
|
||
@Autowired | ||
public KafkaAuthProducerConfiguration(KafkaProperties kafkaProperties) { | ||
this.kafkaProperties = kafkaProperties; | ||
} | ||
|
||
@Bean | ||
public Map<String, Object> producerConfiguration() { | ||
Map<String, Object> properties = new HashMap<>(kafkaProperties.buildProducerProperties()); | ||
properties.put(KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
properties.put(VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); | ||
return properties; | ||
} | ||
|
||
@Bean | ||
public ProducerFactory<String, UserJson> producerFactory() { | ||
return new DefaultKafkaProducerFactory<>(producerConfiguration()); | ||
} | ||
|
||
@Bean | ||
public KafkaTemplate<String, UserJson> kafkaTemplate() { | ||
return new KafkaTemplate<>(producerFactory()); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public NewTopic topic() { | ||
return TopicBuilder.name("users") | ||
.partitions(10) | ||
.replicas(1) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...fler-userdata/src/main/java/com/rangiffler/config/KafkaUserdataConsumerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.rangiffler.config; | ||
|
||
import com.rangiffler.model.UserJson; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.kafka.KafkaProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
import org.springframework.kafka.support.serializer.JsonDeserializer; | ||
|
||
@Configuration | ||
public class KafkaUserdataConsumerConfiguration { | ||
private final KafkaProperties kafkaProperties; | ||
|
||
@Autowired | ||
public KafkaUserdataConsumerConfiguration(KafkaProperties kafkaProperties) { | ||
this.kafkaProperties = kafkaProperties; | ||
} | ||
|
||
@Bean | ||
public ConsumerFactory<String, UserJson> consumerFactory() { | ||
final JsonDeserializer<UserJson> jsonDeserializer = new JsonDeserializer<>(); | ||
jsonDeserializer.addTrustedPackages("*"); | ||
return new DefaultKafkaConsumerFactory<>(kafkaProperties.buildConsumerProperties(), new StringDeserializer(), jsonDeserializer); | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, UserJson> kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, UserJson> concurrentKafkaListenerContainerFactory | ||
= new ConcurrentKafkaListenerContainerFactory<>(); | ||
concurrentKafkaListenerContainerFactory.setConsumerFactory(consumerFactory()); | ||
return concurrentKafkaListenerContainerFactory; | ||
} | ||
} |