Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

original version of spring-cloud-stream-dapr #1

Merged
merged 11 commits into from
Nov 23, 2022
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# IDE generated files and directories
*.iml
.idea/
.vs/
.vscode/

# Output folders
**/target/

# Compiled class file
*.class

# Proto meta
*.pb.meta

# Log file
*.log
/syslog.txt

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Eclipse
.classpath
.project
.settings

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Some other generated folders/files

# macOS
.DS_Store
5 changes: 4 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down Expand Up @@ -186,7 +187,9 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2022 The Dapr Authors.

and others that have contributed code to the public domain.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
MregXN marked this conversation as resolved.
Show resolved Hide resolved
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.dapr.spring</groupId>
<artifactId>spring-cloud-dapr-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>3.1.3</version>
<relativePath />
</parent>

<properties>
<java.version>1.8</java.version>
<dapr-java-sdk.version>1.6.0</dapr-java-sdk.version>
</properties>
<modules>
<module>spring-cloud-stream-dapr</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk</artifactId>
<version>${dapr-java-sdk.version}</version>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk-springboot</artifactId>
<version>${dapr-java-sdk.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
</plugins>
</build>
</project>
54 changes: 54 additions & 0 deletions spring-cloud-stream-dapr/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.dapr.spring</groupId>
<artifactId>spring-cloud-stream-dapr-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>io.dapr.spring</groupId>
<artifactId>spring-cloud-dapr-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath />
</parent>

<properties>
<spring-cloud-stream.version>3.2.4</spring-cloud-stream.version>
<grpc-netty-shaded.version>1.42.2</grpc-netty-shaded.version>
<grpc-server-spring-boot-starter.version>2.13.1.RELEASE</grpc-server-spring-boot-starter.version>
</properties>
<modules>
<module>spring-cloud-starter-stream-dapr</module>
<module>spring-cloud-stream-binder-dapr</module>
<module>samples</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc-netty-shaded.version}</version>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>${grpc-server-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>spring-cloud-stream-binder-dapr</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
</plugins>
</build>
</project>
84 changes: 84 additions & 0 deletions spring-cloud-stream-dapr/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# How to run demo

## preparation

Before running this demo, please ensure that you have the following software installed:

- [jdk 8](https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html)
- [maven 3.8.*](https://maven.apache.org/download.cgi)
- [docker](https://docs.docker.com/get-docker/) / [docker-compose](https://github.com/docker/compose)
- [dapr cli](https://docs.dapr.io/getting-started/install-dapr-cli/)

### dapr install

```bash
dapr init
```

## Start kafka

In this demo, we will use kafka as component for pubsub.

start a single kafka cluster by docker-compose

```bash
$ mkdir kafka
$ cd kafka
$ git clone https://github.com/conduktor/kafka-stack-docker-compose.git
$ cd kafka-stack-docker-compose
$ docker-compose -f zk-single-kafka-single.yml up
......
kafka1 | [2022-08-05 05:53:59,001] INFO KafkaConfig values:
kafka1 | advertised.listeners = INTERNAL://kafka1:19092,EXTERNAL://127.0.0.1:9092,DOCKER://host.docker.internal:29092
......
```

Check it by:

```bash
# check if kafka is running with docker
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
340a4e85beca confluentinc/cp-kafka:7.2.0 "/etc/confluent/dock…" 5 minutes ago Up 5 minutes 0.0.0.0:9092->9092/tcp, 0.0.0.0:9999->9999/tcp, 0.0.0.0:29092->29092/tcp kafka1
48e7c08f2ff6 confluentinc/cp-zookeeper:7.2.0 "/etc/confluent/dock…" 5 minutes ago Up 5 minutes 2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp zoo1

# verify kafka 9092 port
$ nc -zv 127.0.0.1 9092
```

## Start dapr sidecar

```bash
# start dapr runtime for grpc
cd spring-cloud-stream-dapr/samples/kafka
dapr run --app-port 3500 --app-id app1 --app-protocol grpc --dapr-http-port 3501 --dapr-grpc-port 50001 --components-path=./components
```

Check the log to see if dapr runtime start successfully.


## start sample app kafka

Find class `SampleApplication` in package `io.dapr.spring.cloud.stream.sample.kafka` and run it with your IDE. Then you will see logs like this:

```bash
......
Started SampleApplication in 1.63 seconds (JVM running for 1.874)

......
Sending message, sequence 29
succeed to send event GenericMessage [payload=byte[13], headers={contentType=application/json, id=308b580d-d684-cdcc-1281-3399708a3acd, timestamp=1662516457376}]to kafka-pubsub/topic1

Message received : GenericMessage [payload=event body 29, headers={contentType=application/json, id=4dde7c71-65ca-10d0-d26d-69cbbc45abdb, timestamp=1662516457381}]

Sending message, sequence 30
succeed to send event GenericMessage [payload=byte[13], headers={contentType=application/json, id=a9305d7a-86af-4ceb-b363-a56924670d65, timestamp=1662516458382}]to kafka-pubsub/topic1

Message received : GenericMessage [payload=event body 30, headers={contentType=application/json, id=1b351355-fdaa-373d-f184-4305c972f5c2, timestamp=1662516458389}]

Sending message, sequence 31
succeed to send event GenericMessage [payload=byte[13], headers={contentType=application/json, id=c2b0cb09-ddea-8c2a-bb9c-c3d0987c0a23, timestamp=1662516459389}]to kafka-pubsub/topic1

Message received : GenericMessage [payload=event body 31, headers={contentType=application/json, id=420cf68c-6218-01bb-3b1c-c5c1433c5b05, timestamp=1662516459394}]
......
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright 2021 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: kafka-pubsub
spec:
type: pubsub.kafka
initTimeout: 1m
version: v1
metadata:
# Kafka broker connection setting
- name: brokers
value: localhost:9092
# consumer configuration: consumer group
- name: consumerGroup
value: group1
- name: authType # Required.
value: "none"
22 changes: 22 additions & 0 deletions spring-cloud-stream-dapr/samples/kafka/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.dapr.spring</groupId>
<artifactId>spring-cloud-stream-dapr-samples-kafka</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
<relativePath />
</parent>

<dependencies>
<dependency>
<groupId>io.dapr.spring</groupId>
<artifactId>spring-cloud-starter-stream-dapr</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2022 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.dapr.spring.cloud.stream.sample.kafka;

import java.util.function.Consumer;
import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

/**
* The {@link SampleApplication} is main Application to run. Implement publish
* and subscribe to message broker.
*/
@SpringBootApplication
public class SampleApplication {
private static Logger logger = LoggerFactory.getLogger(SampleApplication.class);

public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}

private int i = 0;

/**
* Generate messages for publish.
* @return A method returning Message<T> to publish.
*/
@Bean
public Supplier<Message<String>> supply() {
return () -> {
logger.info("Sending message, sequence " + i++);
return MessageBuilder.withPayload("event body").build();
};
}

/**
* Method to deal with suscribed messages.
*/
@Bean
public Consumer<Message<String>> consume() {
return message -> {
logger.info("Message received : {}", message);
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
grpc:
server:
port: 3500

spring:
cloud:
stream:
function:
definition: supply;consume
bindings:
supply-out-0:
destination: topic1
consume-in-0:
destination: topic1
dapr:
bindings:
supply-out-0:
producer:
pubsubName: kafka-pubsub
consume-in-0:
consumer:
pubsubName: kafka-pubsub
Loading