Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
|
8c8cdb0d07 | |
|
c6278b7244 | |
|
08410e2b80 | |
|
cca8187441 | |
|
735819c4ed | |
|
c75b76e351 | |
|
a5d6289f87 |
|
@ -0,0 +1,102 @@
|
|||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>ru.ldeloff</groupId>
|
||||
<artifactId>KafkaStream</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>KafkaStream</name>
|
||||
<description>KafkaStream</description>
|
||||
<properties>
|
||||
<java.version>22</java.version>
|
||||
<spring-cloud.version>2023.0.1</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.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.resilience4j</groupId>
|
||||
<artifactId>resilience4j-spring-boot2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</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-binder</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
package ru.ldeloff.kafkastream;
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class KafkaStreamApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(KafkaStreamApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package ru.ldeloff.kafkastream.config;
|
||||
|
||||
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
|
||||
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class CircuitBreakerConfig {
|
||||
private final CircuitBreakerRegistry circuitBreakerRegistry;
|
||||
|
||||
public CircuitBreakerConfig(CircuitBreakerRegistry circuitBreakerRegistry){
|
||||
this.circuitBreakerRegistry = circuitBreakerRegistry;
|
||||
}
|
||||
|
||||
public CircuitBreaker circuitBreaker(String cbName) {
|
||||
return circuitBreakerRegistry.circuitBreaker(cbName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package ru.ldeloff.kafkastream.config;
|
||||
|
||||
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
|
||||
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent;
|
||||
import org.springframework.cloud.stream.binding.BindingsLifecycleController;
|
||||
import org.springframework.cloud.stream.endpoint.BindingsEndpoint;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class CircuitBreakerImpl {
|
||||
private final BindingsEndpoint bindingsEndpoint;
|
||||
|
||||
protected CircuitBreakerImpl(BindingsEndpoint bindingsEndpoint,
|
||||
CircuitBreakerConfig circuitBreakerConfig) {
|
||||
this.bindingsEndpoint = bindingsEndpoint;
|
||||
CircuitBreaker circuitBreaker = circuitBreakerConfig.circuitBreaker("consumerCircuitBreaker");
|
||||
circuitBreaker.getEventPublisher().onStateTransition(this::onStateChangeEvent);
|
||||
}
|
||||
|
||||
private void onStateChangeEvent(CircuitBreakerOnStateTransitionEvent event) {
|
||||
switch (event.getStateTransition().getToState()) {
|
||||
case OPEN:
|
||||
System.out.println("consumerAutoTestCircuitBreaker1 open");
|
||||
bindingsEndpoint.changeState("consumerAutoTestCircuitBreaker1-in-0", BindingsLifecycleController.State.STOPPED);
|
||||
break;
|
||||
case CLOSED:
|
||||
System.out.println("consumerAutoTestCircuitBreaker1 closed");
|
||||
case HALF_OPEN:
|
||||
System.out.println("consumerAutoTestCircuitBreaker1 half_open");
|
||||
bindingsEndpoint.changeState("consumerAutoTestCircuitBreaker1-in-0", BindingsLifecycleController.State.STARTED);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package ru.ldeloff.kafkastream.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ldeloff.kafkastream.model.Model;
|
||||
import ru.ldeloff.kafkastream.service.ModelService;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class KafkaConsumerConfig {
|
||||
private final ModelService modelService;
|
||||
|
||||
@Bean
|
||||
public Consumer<Model> consumerAuto1() {
|
||||
return message -> {
|
||||
// System.out.println("Consume auto 1: " + message);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<Model> consumerManual1() {
|
||||
return message -> {
|
||||
System.out.println("Consume manual 1: " + message);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<Model> consumerAutoTestCircuitBreaker1() {
|
||||
return message -> {
|
||||
System.out.println("Consume Auto CB 1: " + message);
|
||||
try {
|
||||
modelService.sendModel(message);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getClass());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<Model> consumerAuto2() {
|
||||
return message -> {
|
||||
// System.out.println("Consume auto 2: " + message);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<Model> consumerManual2() {
|
||||
return message -> {
|
||||
System.out.println("Consume manual 2: " + message);
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package ru.ldeloff.kafkastream.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.kafka.support.KafkaHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import ru.ldeloff.kafkastream.model.Model;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class KafkaProducerConfig {
|
||||
|
||||
private int i = 0;
|
||||
private final StreamBridge streamBridge;
|
||||
|
||||
// Периодически отправляет сообщения
|
||||
@Bean
|
||||
public Supplier<Message<Model>> producerAuto1() {
|
||||
return () -> {
|
||||
Model message = new Model(++i);
|
||||
// System.out.println("Produce auto 1:" + message);
|
||||
return MessageBuilder.withPayload(message)
|
||||
.setHeader(KafkaHeaders.KEY, String.valueOf(message.getId()))
|
||||
.build();
|
||||
};
|
||||
}
|
||||
|
||||
// Ручная отправка
|
||||
public void sendMessage1(Model message) {
|
||||
System.out.println("Produce manual (kafka 1): " + message);
|
||||
streamBridge.send("producerManual1-out-0",
|
||||
MessageBuilder.withPayload(message)
|
||||
.setHeader(KafkaHeaders.KEY, String.valueOf(message.getId()))
|
||||
.build());
|
||||
}
|
||||
|
||||
// Для Circuit Breaker
|
||||
@Bean
|
||||
public Supplier<Message<Model>> producerAutoTestCircuitBreaker1() {
|
||||
return () -> {
|
||||
Model message = new Model(++i);
|
||||
System.out.println("Produce auto (CB) 1:" + message);
|
||||
return MessageBuilder.withPayload(message)
|
||||
.setHeader(KafkaHeaders.KEY, String.valueOf(message.getId()))
|
||||
.build();
|
||||
};
|
||||
}
|
||||
|
||||
/* Кафка 2 */
|
||||
@Bean
|
||||
public Supplier<Model> producerAuto2() {
|
||||
return () -> {
|
||||
Model message = new Model(++i);
|
||||
// System.out.println("Produce auto 2:" + message);
|
||||
return message;
|
||||
};
|
||||
}
|
||||
|
||||
public void sendMessage2(Model message) {
|
||||
System.out.println("Produce manual (kafka 2): " + message);
|
||||
streamBridge.send("producerManual2-out-0", message);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package ru.ldeloff.kafkastream.config;
|
||||
|
||||
import org.springframework.cloud.stream.binding.BindingsLifecycleController;
|
||||
import org.springframework.cloud.stream.binding.InputBindingLifecycle;
|
||||
import org.springframework.cloud.stream.binding.OutputBindingLifecycle;
|
||||
import org.springframework.cloud.stream.endpoint.BindingsEndpoint;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class KafkaStreamConfig {
|
||||
|
||||
@Bean
|
||||
public BindingsEndpoint bindingsEndpoint(List<InputBindingLifecycle> inputBindings,
|
||||
List<OutputBindingLifecycle> outputBindings) {
|
||||
return new BindingsEndpoint(new BindingsLifecycleController(inputBindings, outputBindings));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package ru.ldeloff.kafkastream.config;
|
||||
|
||||
import io.netty.channel.ChannelOption;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig {
|
||||
|
||||
@Bean
|
||||
public WebClient webClient() {
|
||||
return WebClient.builder()
|
||||
.baseUrl("http://localhost:3000")
|
||||
.clientConnector(new ReactorClientHttpConnector(
|
||||
HttpClient.create()
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
|
||||
.wiretap(true)
|
||||
)
|
||||
)
|
||||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package ru.ldeloff.kafkastream.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cloud.stream.binding.BindingsLifecycleController;
|
||||
import org.springframework.cloud.stream.endpoint.BindingsEndpoint;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ldeloff.kafkastream.config.KafkaProducerConfig;
|
||||
import ru.ldeloff.kafkastream.model.Model;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class Controller {
|
||||
int i = 0;
|
||||
private final KafkaProducerConfig kafkaProducerConfig;
|
||||
private final BindingsEndpoint bindingsEndpoint;
|
||||
|
||||
// Ручная отправка сообщений
|
||||
@GetMapping(value = "/test1")
|
||||
public String test1() {
|
||||
kafkaProducerConfig.sendMessage1(new Model(++i));
|
||||
return "test OK, i = " + i;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/test2")
|
||||
public String test2() {
|
||||
kafkaProducerConfig.sendMessage2(new Model(++i));
|
||||
return "test OK, i = " + i;
|
||||
}
|
||||
|
||||
// Переключатели состояний consumerAutoTestCircuitBreaker1-in-0
|
||||
@GetMapping(value = "/test3")
|
||||
public String test3() {
|
||||
bindingsEndpoint.changeState("consumerAutoTestCircuitBreaker1-in-0", BindingsLifecycleController.State.STOPPED);
|
||||
return "test OK";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/test4")
|
||||
public String test4() {
|
||||
bindingsEndpoint.changeState("consumerAutoTestCircuitBreaker1-in-0", BindingsLifecycleController.State.STARTED);
|
||||
return "test OK";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package ru.ldeloff.kafkastream.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class Model {
|
||||
private int id;
|
||||
private long time;
|
||||
public Model(int id) {
|
||||
this.id = id;
|
||||
this.time = System.currentTimeMillis();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package ru.ldeloff.kafkastream.service;
|
||||
|
||||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
|
||||
import io.github.resilience4j.retry.annotation.Retry;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import ru.ldeloff.kafkastream.model.Model;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ModelService {
|
||||
private final WebClient webClient;
|
||||
|
||||
@CircuitBreaker(name = "consumerCircuitBreaker")
|
||||
@Retry(name = "consumerCircuitBreaker")
|
||||
public void sendModel(Model model) {
|
||||
System.out.println("Sending Model: " + model);
|
||||
Model model1 = webClient.post()
|
||||
.uri("/models")
|
||||
.bodyValue(BodyInserters.fromValue(model))
|
||||
.retrieve()
|
||||
.bodyToMono(Model.class).block();
|
||||
System.out.println(model1);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
spring:
|
||||
application:
|
||||
name: DemoKafkaStream
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
cloud:
|
||||
function:
|
||||
definition: producerAuto1;consumerAuto1;producerManual1;consumerManual1;producerAuto2;consumerAuto2;producerManual2;consumerManual2
|
||||
definition: producerAuto1;consumerAuto1;producerManual1;consumerManual1;producerAuto2;consumerAuto2;producerManual2;consumerManual2;producerAutoTestCircuitBreaker1;consumerAutoTestCircuitBreaker1
|
||||
stream:
|
||||
bindings:
|
||||
producerAuto1-out-0:
|
||||
|
@ -21,6 +23,14 @@ spring:
|
|||
binder: kafka-1
|
||||
destination: kafka_1_topic_manual
|
||||
|
||||
producerAutoTestCircuitBreaker1-out-0:
|
||||
binder: kafka-1
|
||||
destination: kafka_1_topic_auto_CB
|
||||
consumerAutoTestCircuitBreaker1-in-0:
|
||||
binder: kafka-1
|
||||
destination: kafka_1_topic_auto_CB
|
||||
group: consumerAutoTestCircuitBreaker
|
||||
|
||||
producerAuto2-out-0:
|
||||
binder: kafka-2
|
||||
destination: kafka_2_topic_auto
|
||||
|
@ -59,3 +69,50 @@ spring:
|
|||
binder:
|
||||
brokers:
|
||||
- 192.168.10.101:39092
|
||||
|
||||
#Resilience4j circuit breaker config
|
||||
resilience4j.circuitbreaker:
|
||||
instances:
|
||||
consumerCircuitBreaker:
|
||||
slidingWindowType: COUNT_BASED
|
||||
minimumNumberOfCalls: 1
|
||||
permittedNumberOfCallsInHalfOpenState: 10
|
||||
failureRateThreshold: 50
|
||||
automaticTransitionFromOpenToHalfOpenEnabled: true
|
||||
slidingWindowSize: 10
|
||||
waitDurationInOpenState: 10s
|
||||
registerHealthIndicator: true
|
||||
recordExceptions: org.springframework.web.reactive.function.client.WebClientRequestException
|
||||
ignoreExceptions:
|
||||
|
||||
#Resilience4j retry config
|
||||
resilience4j.retry:
|
||||
instances:
|
||||
consumerCircuitBreaker:
|
||||
maxAttempts: 3
|
||||
waitDuration: 500
|
||||
retryExceptions: org.springframework.web.reactive.function.client.WebClientRequestException
|
||||
ignoreExceptions:
|
||||
metrics:
|
||||
enabled: true
|
||||
legacy:
|
||||
enabled: true
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: bindings,health
|
||||
|
||||
health:
|
||||
binders:
|
||||
enabled: true
|
||||
circuitbreakers:
|
||||
enabled: true
|
||||
ratelimiters:
|
||||
enabled:true
|
||||
|
||||
endpoint:
|
||||
health:
|
||||
show-details: ALWAYS
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
package ru.ldeloff.demokafkastream;
|
||||
package ru.ldeloff.kafkastream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoKafkaStreamApplicationTests {
|
||||
class KafkaStreamApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
|
@ -0,0 +1 @@
|
|||
Пример проекта с kafka streams и паттерном circuit breaker
|
|
@ -0,0 +1,59 @@
|
|||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>ru.ldeloff</groupId>
|
||||
<artifactId>Server</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Server</name>
|
||||
<description>Server</description>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
package ru.ldeloff.server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package ru.ldeloff.server.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ldeloff.server.model.Model;
|
||||
|
||||
@RestController
|
||||
public class ModelController {
|
||||
@PostMapping(value = "/models")
|
||||
public Model test2(Model model) {
|
||||
int id = model.getId();
|
||||
model.setId(id + 1000000);
|
||||
return model;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.demokafkastream;
|
||||
package ru.ldeloff.server.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
|
@ -0,0 +1,6 @@
|
|||
server:
|
||||
port: 3000
|
||||
spring:
|
||||
application:
|
||||
name: Server
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package ru.ldeloff.server;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ServerApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
69
pom.xml
69
pom.xml
|
@ -13,74 +13,5 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>DemoKafkaStream</name>
|
||||
<description>DemoKafkaStream</description>
|
||||
<properties>
|
||||
<java.version>22</java.version>
|
||||
<spring-cloud.version>2023.0.1</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.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</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-binder</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,108 +0,0 @@
|
|||
package ru.ldeloff.demokafkastream;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.kafka.support.KafkaHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@RestController
|
||||
@SpringBootApplication
|
||||
public class DemoKafkaStreamApplication {
|
||||
|
||||
private int i = 0;
|
||||
|
||||
@Autowired
|
||||
private StreamBridge streamBridge;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoKafkaStreamApplication.class, args);
|
||||
}
|
||||
|
||||
/* Кафка 1 */
|
||||
// Консьюмер
|
||||
@Bean
|
||||
public Consumer<Model> consumerAuto1() {
|
||||
return message -> {
|
||||
// System.out.println("Consume auto 1: " + message);
|
||||
};
|
||||
}
|
||||
|
||||
// Продюсер (сам периодически отправляет сообщения)
|
||||
@Bean
|
||||
public Supplier<Message<Model>> producerAuto1() {
|
||||
return () -> {
|
||||
Model message = new Model(++i);
|
||||
// System.out.println("Produce auto 1:" + message);
|
||||
return MessageBuilder.withPayload(message)
|
||||
.setHeader(KafkaHeaders.KEY, String.valueOf(message.getId()))
|
||||
.build();
|
||||
};
|
||||
}
|
||||
|
||||
// Вручную отправим сообщение
|
||||
@GetMapping(value = "/test1")
|
||||
public String test1() {
|
||||
Model message = new Model(++i);
|
||||
System.out.println("Produce manual (kafka 1): " + message);
|
||||
streamBridge.send("producerManual1-out-0",
|
||||
MessageBuilder.withPayload(message)
|
||||
.setHeader(KafkaHeaders.KEY, String.valueOf(message.getId()))
|
||||
.build());
|
||||
return "test OK";
|
||||
}
|
||||
|
||||
// Консьюмер
|
||||
@Bean
|
||||
public Consumer<Model> consumerManual1() {
|
||||
return message -> {
|
||||
System.out.println("Consume manual 1: " + message);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/* Кафка 2 */
|
||||
// Продюсер (сам периодически отправляет сообщения)
|
||||
@Bean
|
||||
public Supplier<Model> producerAuto2() {
|
||||
return () -> {
|
||||
Model message = new Model(++i);
|
||||
// System.out.println("Produce auto 2:" + message);
|
||||
return message;
|
||||
};
|
||||
}
|
||||
|
||||
// Консьюмер
|
||||
@Bean
|
||||
public Consumer<Model> consumerAuto2() {
|
||||
return message -> {
|
||||
// System.out.println("Consume auto 2: " + message);
|
||||
};
|
||||
}
|
||||
|
||||
// Вручную отправим сообщение
|
||||
@GetMapping(value = "/test2")
|
||||
public String test2() {
|
||||
Model message = new Model(++i);
|
||||
System.out.println("Produce manual (kafka 2): " + message);
|
||||
streamBridge.send("producerManual2-out-0", message);
|
||||
return "test OK";
|
||||
}
|
||||
|
||||
// Консьюмер
|
||||
@Bean
|
||||
public Consumer<Model> consumerManual2() {
|
||||
return message -> {
|
||||
System.out.println("Consume manual 2: " + message);
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue