/protocol-buffers-spring-cloud-stream

Protocol Buffers base messaging for Spring Cloud Stream

Primary LanguageJava

protocol-buffers-spring-cloud-stream

Protocol Buffers base messaging for Spring Cloud Stream

Set up

Add dependency for your project.

repositories {
    // ...
    maven {url 'https://dl.bintray.com/disc99/maven'}
    // ...
}

dependencies {
    // ...
    compile "io.disc99:protocol-buffers-spring-cloud-stream:${BUILD_VERSION}"
    // ...
}

How to use

Usage is the same as normal Protocol Buffers and Spring Cloud Stream.

The only difference is to specify content-type: application/x-protobuf for application.yaml.

proto file

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.example.task";

message TaskCreated {
    string id = 1;
    string name = 2;
}

Source Application

spring:
  cloud:
    stream:
      bindings:
        output:
          destination: tasks
          content-type: application/x-protobuf # for Protocol Buffers
          producer:
            partitionKeyExpression: "1"
@EnableBinding(Source.class)
@AllArgsConstructor
class Publisher {

    Source source;

    void create() {
        TaskCreated task = // ...
        Message<TaskCreated> message = new GenericMessage<>(task);
        source.output().send(message);
    }
}

Sink Application

spring:
  cloud:
    stream:
      bindings:
        input:
          destination: tasks
          content-type: application/x-protobuf # for Protocol Buffers
@EnableBinding(Sink.class)
class Subscriber {

    @StreamListener(Sink.INPUT)
    void handle(TaskCreated message) {
        System.out.println(message);
    }
}

License

(The MIT License)

Copyright (c) 2018 @disc99