简体   繁体   中英

How do I get kafka-json-schema-serializer within a Supplier function use the Jackson instead of Binary Object?

Using Spring Boot Cloud Streams Kafka Binder to produce and consume JSON via a Schema Registry via a Supplier / Consumer relationship causes it to be encoded as a Binary Object.

this code

    @Bean
    public Supplier<PaymentEvent> produceEvents() {
        return () -> {
            PaymentEvent paymentEvent = new PaymentEvent();
            paymentEvent.name = "xxxxx";
            log.info("paymentEvent.name: " + paymentEvent.name);
            return paymentEvent;
        };
    }

when using

spring:
  cloud:
    stream:
      bindings:
        produceEvents-out-0:
          destination: paymentevent-connectid
          binder: kafka
          group: paymentevent-group
      kafka:
        bindings:
          produceEvents-out-0:
            producer:
              configuration:
                key.serializer: org.apache.kafka.common.serialization.StringSerializer
                value.serializer: io.confluent.kafka.serializers.json.KafkaJsonSchemaSerializer
                schema.registry.url: http://confluent-schema-registry:8082
        binder:
          brokers: eventcluster-kafka-bootstrap:9093
          configuration:
            security.protocol: SSL
            ssl.truststore.location: classpath:/kafka/tls/eventcluster-cluster-ca-cert-ca.p12
            ssl.truststore.password: xxxxx
            ssl.truststore.type: PKCS12
            ssl.keystore.location: classpath:/kafka/tls/paymentevent.p12
            ssl.keystore.password: xxxx
            ssl.keystore.type: PKCS12

produces the following JSON Schema

i.c.k.s.client.rest.RestService          Sending POST with input {"schemaType":"JSON","schema":"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\":\"Byte []\",\"type\":\"array\",\"items\":{\"type\":\"integer\"}}"} to http://confluent-schema-registry:8082/subjects/paymentevent-connectid-value/versions?normalize=false

if I code directly

        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "eventcluster-kafka-bootstrap:9093");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
                "io.confluent.kafka.serializers.json.KafkaJsonSchemaSerializer");
        props.put("security.protocol", "SSL");
        props.put("ssl.truststore.location",
                "kafka\\tls\\eventcluster-cluster-ca-cert-ca.p12");
        props.put("ssl.trustxxxxstore.password", "x");
        props.put("ssl.truststore.type", "PKCS12");
        props.put("ssl.keystore.location",
                "kafka\\tls\\paymentevent.p12");
        props.put("ssl.keystore.password", "xxxx");
        props.put("ssl.keystore.type", "PKCS12");
        props.put("schema.registry.url", "http://127.0.0.1:8082");

        Producer<String, PaymentEvent> producer = new KafkaProducer<String, PaymentEvent>(props);

        String topic = "paymentevent-connectid";
        String key = "1";
        PaymentEvent paymentEvent = new PaymentEvent();
        paymentEvent.name = "xxxx";

        ProducerRecord<String, PaymentEvent> record = new ProducerRecord<String, PaymentEvent>(topic, key,
                paymentEvent);
        producer.send(record).get();
        producer.close();

then it becomes

io.confluent.kafka.schemaregistry.client.rest.RestService - Sending POST with input {"schemaType":"JSON","schema":"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\":\"Payment Event\",\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"name\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"string\"}]},\"signedreqattr.claims\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/SignedreqattrClaims\"}]}},\"definitions\":{\"SignedreqattrClaims\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"id_token\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/IdToken\"}]}}},\"IdToken\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"birthdate\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/Birthdate\"}]},\"address\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/Address\"}]},\"name\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/Name\"}]},\"phone_number\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/PhoneNumber\"}]},\"given_name\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/GivenName\"}]},\"family_name\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/FamilyName\"}]},\"email\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"$ref\":\"#/definitions/Email\"}]}}},\"Birthdate\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}},\"Address\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}},\"Name\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}},\"PhoneNumber\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}},\"GivenName\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}},\"FamilyName\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}},\"Email\":{\"type\":\"object\",\"additionalProperties\":false,\"properties\":{\"essential\":{\"oneOf\":[{\"type\":\"null\",\"title\":\"Not included\"},{\"type\":\"boolean\"}]}}}}}"} to http://127.0.0.1:8082/subjects/paymentevent-connectid-value/versions?normalize=false
1

seems using Supplier converts it into Binary and causes problems with the consumer.

Any ideas on what I am doing wrong?

Found I needed to add

spring:
  cloud:
    stream:
      default: 
        producer: 
          useNativeEncoding: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM