Spring Kafka SSL setup in Spring boot application.yml

后端 未结 2 1663
慢半拍i
慢半拍i 2021-01-03 10:18

I am trying to setup a Spring Boot Application with a Kafka Client to use SSL. I have my keystore.jks and truststore.jks stored on a filesystem(on a docker container) becau

相关标签:
2条回答
  • 2021-01-03 11:02

    According to discussion and to enable kafka ssl configuration, first need to enable and set ssl properties in consumerFactory

    @Bean
    public ConsumerFactory<String, ReportingTask> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonSerializable.class);
        props.put(ConsumerConfig.CLIENT_ID_CONFIG, clientId);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxRecords);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, offSet);
        if (sslEnabled) {
            props.put("security.protocol", "SSL");
            props.put("ssl.truststore.location", trustStoreLocation);
            props.put("ssl.truststore.password", trustStorePassword);
    
            props.put("ssl.key.password", keyStorePassword);
            props.put("ssl.keystore.password", keyStorePassword);
            props.put("ssl.keystore.location", keyStoreLocation);
        }
        return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), new JsonDeserializer<>(Task.class));
    }
    

    And copy the certificates into docker container

    COPY ssl/stage/* /var/lib/kafka/stage/
    
    0 讨论(0)
  • 2021-01-03 11:05

    If anyone is still looking at this, try prepending file:// to the file path:

    truststorelocation: "file:///tmp/kafka.client.keystore.jks"
    

    The error is complaining about the lack of a URL - adding a protocol (file://) makes the path a URL (speaking very basically)

    0 讨论(0)
提交回复
热议问题