mirror of
https://github.com/wahyd4/spring-cloud-stream-samples.git
synced 2026-08-09 05:16:18 +10:00
Refactor kafka jaas (multi binder) sample application
This commit is contained in:
+2
-3
@@ -3,7 +3,6 @@
|
||||
This is a sample application that demonstrates how to connect to multi kafka clusters with security enabled using multiple binders.
|
||||
This application uses two Kafka clusters both of them are enabled with security (JAAS - SASL/PLAINTEXT).
|
||||
|
||||
|
||||
## Here are the detailed instructions for setting up your cluster.
|
||||
|
||||
If you already have two clusters with security (sasl/plaintext) enabled, you can skip this section. However, it may still benefit to go over these instructions in order to avoid any inconsistencies with your environment.
|
||||
@@ -20,7 +19,7 @@ If you already have two clusters with security (sasl/plaintext) enabled, you can
|
||||
KafkaServer {
|
||||
org.apache.kafka.common.security.plain.PlainLoginModule required
|
||||
username="admin"
|
||||
password="admin-secret";
|
||||
password="admin-secret"
|
||||
user_admin="admin-secret";
|
||||
};
|
||||
|
||||
@@ -99,7 +98,7 @@ $ ./<PATH-TO-CLUSTER-1>/bin/kafka-server-start.sh config/server.properties
|
||||
KafkaServer {
|
||||
org.apache.kafka.common.security.plain.PlainLoginModule required
|
||||
username="admin"
|
||||
password="admin-secret";
|
||||
password="admin-secret"
|
||||
user_admin="admin-secret";
|
||||
};
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>kafka-multi-binder-jaas</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>kafka-multi-binder-jaas</name>
|
||||
<description>Demo project with multi kafka cluster/binder with JAAS</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring-cloud.version>Hoxton.BUILD-SNAPSHOT</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot-local</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/libs-milestone-local</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot-local</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/libs-milestone-local</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-releases</id>
|
||||
<name>Spring Releases</name>
|
||||
<url>https://repo.spring.io/libs-release-local</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
||||
+11
-33
@@ -16,15 +16,11 @@
|
||||
|
||||
package multibinder.kafka.jaas;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MultiBinderKafkaJaasSample {
|
||||
@@ -33,35 +29,17 @@ public class MultiBinderKafkaJaasSample {
|
||||
SpringApplication.run(MultiBinderKafkaJaasSample.class, args);
|
||||
}
|
||||
|
||||
@EnableBinding(CustomProcessor.class)
|
||||
static class Foo {
|
||||
static class Foo {
|
||||
|
||||
@StreamListener("input")
|
||||
@SendTo("output")
|
||||
public String receive(String foo) {
|
||||
return foo;
|
||||
@Bean
|
||||
public Function<String, String> receive() {
|
||||
return foo -> foo;
|
||||
}
|
||||
|
||||
@StreamListener("input1")
|
||||
@SendTo("output1")
|
||||
public String receive1(String foo) {
|
||||
return foo;
|
||||
@Bean
|
||||
public Function<String, String> receive1() {
|
||||
return foo -> foo;
|
||||
}
|
||||
}
|
||||
|
||||
interface CustomProcessor {
|
||||
|
||||
@Input("input")
|
||||
SubscribableChannel input();
|
||||
|
||||
@Output("output")
|
||||
MessageChannel output();
|
||||
|
||||
@Input("input1")
|
||||
SubscribableChannel input1();
|
||||
|
||||
@Output("output1")
|
||||
MessageChannel output1();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-4
@@ -1,15 +1,16 @@
|
||||
spring.cloud.stream:
|
||||
function.definition: receive;receive1
|
||||
bindings:
|
||||
input:
|
||||
receive-in-0:
|
||||
destination: kafka1-in
|
||||
binder: kafka1
|
||||
output:
|
||||
receive-out-0:
|
||||
destination: kafka2-out
|
||||
binder: kafka2
|
||||
input1:
|
||||
receive1-in-0:
|
||||
destination: kafka2-in
|
||||
binder: kafka2
|
||||
output1:
|
||||
receive1-out-0:
|
||||
destination: kafka1-out
|
||||
binder: kafka1
|
||||
binders:
|
||||
@@ -1,46 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>kafka-multibinder-jaas</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>kafka-multibinder-jaas</name>
|
||||
<description>Demo project with multi kafka cluster/binder with JAAS</description>
|
||||
|
||||
<parent>
|
||||
<groupId>io.spring.cloud.stream.sample</groupId>
|
||||
<artifactId>spring-cloud-stream-samples-parent</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -28,7 +28,7 @@ Assuming you are running two dockerized Kafka clusters as above.
|
||||
|
||||
Issue the following commands:
|
||||
|
||||
`docker exec -it kafka-multib-inder-1 /opt/kafka/bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic dataIn`
|
||||
`docker exec -it kafka-multi-binder-1 /opt/kafka/bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic dataIn`
|
||||
|
||||
On another terminal:
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ version: '3'
|
||||
services:
|
||||
kafka1:
|
||||
image: wurstmeister/kafka
|
||||
container_name: kafka-multib-inder-1
|
||||
container_name: kafka-multi-binder-1
|
||||
ports:
|
||||
- "9092:9092"
|
||||
environment:
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<modules>
|
||||
<module>multi-binder-kafka-rabbit</module>
|
||||
<module>multi-binder-two-kafka-clusters</module>
|
||||
<module>kafka-multibinder-jaas</module>
|
||||
<module>kafka-multi-binder-jaas</module>
|
||||
<module>multi-binder-kafka-streams</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user