ip696:
Trato de configuración de Apache kafka en el arranque de la primavera de aplicación. He leído esta documentación y seguir los pasos:
1) añado estas líneas a 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) Me crear nuevo tema:
@Bean
public NewTopic responseTopic() {
return new NewTopic("new-topic", 5, (short) 1);
}
Y ahora quiero utilización KafkaTemplate
:
private final KafkaTemplate<String, byte[]> kafkaTemplate;
public KafkaEventBus(KafkaTemplate<String, byte[]> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
Sino más destacado IntelliJ IDE:
Para solucionar este problema que es necesario crear frijol:
@Bean
public KafkaTemplate<String, byte[]> myMessageKafkaTemplate() {
return new KafkaTemplate<>(greetingProducerFactory());
}
Y pasar a propirties constructor 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);
}
Pero entonces ¿cuál es el punto de poner en application.yam l si es necesario crear manual de ProducerFactory?
Gary Russell:
Creo que puede pasar por alto la advertencia de IDEA; No tengo problemas de cableado en la plantilla de arranque con diferentes tipos genéricos ...
@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;
}
}
y
they are the same