ip696:
J'essaie configurer apache kafka au démarrage du printemps application. J'ai lu cette documentation et suivez les étapes:
1) ajouter ces lignes à aplication.yaml
:
spring:
kafka:
bootstrap-servers: kafka_host:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringDeserializer
value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
2) Je Créer un nouveau sujet:
@Bean
public NewTopic responseTopic() {
return new NewTopic("new-topic", 5, (short) 1);
}
Et maintenant , je veux utiliser KafkaTemplate
:
private final KafkaTemplate<String, byte[]> kafkaTemplate;
public KafkaEventBus(KafkaTemplate<String, byte[]> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
Mais Intellij IDE: Faits saillants
Pour résoudre ce problème, je besoin de créer haricot:
@Bean
public KafkaTemplate<String, byte[]> myMessageKafkaTemplate() {
return new KafkaTemplate<>(greetingProducerFactory());
}
Et passer au constructeur propirties greetingProducerFactory()
:
@Bean
public ProducerFactory<String, byte[]> greetingProducerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka_hist4:9092");
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
Mais alors quel est le point de mise en application.yam l si je besoin de créer manuelle ProducerFactory?
Gary Russell:
Je pense que vous pouvez ignorer en toute sécurité l'avertissement de IDEA; Je n'ai pas de problèmes de câblage dans le modèle de démarrage avec différents types génériques ...
@SpringBootApplication
public class So55280173Application {
public static void main(String[] args) {
SpringApplication.run(So55280173Application.class, args);
}
@Bean
public ApplicationRunner runner(KafkaTemplate<String, String> template, Foo foo) {
return args -> {
template.send("so55280173", "foo");
if (foo.template == template) {
System.out.println("they are the same");
}
};
}
@Bean
public NewTopic topic() {
return new NewTopic("so55280173", 1, (short) 1);
}
}
@Component
class Foo {
final KafkaTemplate<String, String> template;
@Autowired
Foo(KafkaTemplate<String, String> template) {
this.template = template;
}
}
et
they are the same