- 
                Notifications
    
You must be signed in to change notification settings  - Fork 6
 
Add reactive application with database and Kafka #290
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
  
     Merged
                    Changes from 9 commits
      Commits
    
    
            Show all changes
          
          
            17 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      591214c
              
                add tests
              
              
                marijarin 15a751c
              
                add autowired filter
              
              
                marijarin 8b178fc
              
                fix tests and flows
              
              
                 1144530
              
                fix mono code
              
              
                 d345b63
              
                lower branch coverage for reactive app to 0.5
              
              
                 bf5d896
              
                merge from master
              
              
                 2b8facf
              
                fix after merge master
              
              
                 0df0dc4
              
                delete some log details
              
              
                 8f8e4ed
              
                Merge branch 'master' into issue-146
              
              
                mfvanek 6061fc4
              
                done with Mono.fromCallable()
              
              
                 80ce50e
              
                Merge branch 'master' into issue-146
              
              
                mfvanek bacffa9
              
                3 versions to change headers but not working as expected
              
              
                 6bca264
              
                Merge branch 'issue-146' of https://github.com/mfvanek/spring-boot-op…
              
              
                 438291e
              
                Fix reactive chain
              
              
                mfvanek cdf8491
              
                Fix tests
              
              
                mfvanek 922c0ca
              
                Add BlockHound
              
              
                mfvanek 2991434
              
                Polish
              
              
                mfvanek File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| plugins { | ||
                
      
                  mfvanek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" | ||
| } | ||
| rootProject.name = "spring-boot-open-telemetry-demo" | ||
| 
     | 
||
| include("spring-boot-3-demo-app") | ||
| include("common-internal-bom") | ||
| include("db-migrations") | ||
| include("spring-boot-3-demo-app-kotlin") | ||
| include("spring-boot-3-demo-app-reactive") | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| plugins { | ||
| id("sb-ot-demo.java-conventions") | ||
| id("sb-ot-demo.forbidden-apis") | ||
| id("sb-ot-demo.docker") | ||
| alias(libs.plugins.spring.boot.v3) | ||
| id("io.freefair.lombok") | ||
| } | ||
| 
     | 
||
| dependencies { | ||
| implementation(platform(project(":common-internal-bom"))) | ||
| implementation(platform(libs.springdoc.openapi)) | ||
| implementation(platform(libs.spring.boot.v3.dependencies)) | ||
| implementation(platform(libs.spring.cloud)) | ||
| 
     | 
||
| implementation("org.springframework.boot:spring-boot-starter-webflux") | ||
| implementation("org.springframework.boot:spring-boot-starter-actuator") | ||
| implementation("org.springframework.boot:spring-boot-starter-security") | ||
| implementation("io.micrometer:micrometer-registry-prometheus") | ||
| implementation("io.projectreactor:reactor-tools") | ||
| implementation("org.springdoc:springdoc-openapi-starter-webflux-ui") | ||
| 
     | 
||
| implementation("org.springframework.kafka:spring-kafka") | ||
| 
     | 
||
| implementation("io.micrometer:micrometer-tracing-bridge-otel") | ||
| implementation("io.opentelemetry:opentelemetry-exporter-otlp") | ||
| 
     | 
||
| implementation("org.springframework.boot:spring-boot-starter-jdbc") | ||
| implementation("org.postgresql:postgresql") | ||
| implementation("com.zaxxer:HikariCP") | ||
| implementation(project(":db-migrations")) { | ||
| exclude(group = "io.gitlab.arturbosch.detekt") | ||
                
      
                  mfvanek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| implementation("org.liquibase:liquibase-core") | ||
| implementation("com.github.blagerweij:liquibase-sessionlock") | ||
| implementation(libs.datasource.micrometer) | ||
| implementation(libs.logstash) | ||
| 
     | 
||
| testImplementation("org.springframework.boot:spring-boot-starter-test") | ||
| testImplementation("org.springframework.boot:spring-boot-starter-webflux") | ||
| testImplementation("org.testcontainers:postgresql") | ||
| testImplementation("org.testcontainers:kafka") | ||
| testImplementation("org.springframework.kafka:spring-kafka-test") | ||
| testImplementation("org.awaitility:awaitility") | ||
| testImplementation("io.github.mfvanek:pg-index-health-test-starter") | ||
| testImplementation("org.springframework.cloud:spring-cloud-starter-contract-stub-runner") | ||
| testImplementation("io.projectreactor:reactor-test:3.8.0-M3") | ||
                
      
                  mfvanek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| tasks { | ||
                
      
                  mfvanek marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| jacocoTestCoverageVerification { | ||
| dependsOn(jacocoTestReport) | ||
| violationRules { | ||
| rule { | ||
| limit { | ||
| counter = "BRANCH" | ||
| value = "COVEREDRATIO" | ||
| minimum = "0.50".toBigDecimal() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| val coverageExcludeList = listOf("**/*Application.class") | ||
| listOf(JacocoCoverageVerification::class, JacocoReport::class).forEach { taskType -> | ||
| tasks.withType(taskType) { | ||
| afterEvaluate { | ||
| classDirectories.setFrom( | ||
| files( | ||
| classDirectories.files.map { file -> | ||
| fileTree(file).apply { | ||
| exclude(coverageExcludeList) | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| 
     | 
||
| springBoot { | ||
| buildInfo() | ||
| } | ||
        
          
          
            19 changes: 19 additions & 0 deletions
          
          19 
        
  ...-demo-app-reactive/src/main/java/io/github/mfvanek/spring/boot3/reactive/Application.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive; | ||
| 
     | 
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| 
     | 
||
| @SpringBootApplication | ||
| public class Application { | ||
| 
     | 
||
| public static void main(String[] args) { | ||
| SpringApplication.run(Application.class, args); | ||
| } | ||
| } | 
        
          
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  ...pp-reactive/src/main/java/io/github/mfvanek/spring/boot3/reactive/config/ClockConfig.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.config; | ||
| 
     | 
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| 
     | 
||
| import java.time.Clock; | ||
| 
     | 
||
| @Configuration(proxyBeanMethods = false) | ||
| public class ClockConfig { | ||
| 
     | 
||
| @Bean | ||
| public Clock clock() { | ||
| return Clock.systemUTC(); | ||
| } | ||
| } | 
        
          
          
            37 changes: 37 additions & 0 deletions
          
          37 
        
  ...ive/src/main/java/io/github/mfvanek/spring/boot3/reactive/config/OpenTelemetryConfig.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.config; | ||
| 
     | 
||
| import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; | ||
| import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder; | ||
| import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpTracingAutoConfiguration; | ||
| import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpTracingProperties; | ||
| import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| 
     | 
||
| import java.util.Locale; | ||
| import javax.annotation.Nonnull; | ||
| 
     | 
||
| @AutoConfigureBefore(OtlpTracingAutoConfiguration.class) | ||
| @Configuration(proxyBeanMethods = false) | ||
| class OpenTelemetryConfig { | ||
| 
     | 
||
| @Bean | ||
| @ConditionalOnMissingBean(OtlpGrpcSpanExporter.class) | ||
| OtlpGrpcSpanExporter otelJaegerGrpcSpanExporter(@Nonnull final OtlpTracingProperties otlpProperties) { | ||
                
      
                  mfvanek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| final OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder() | ||
| .setEndpoint(otlpProperties.getEndpoint()) | ||
| .setTimeout(otlpProperties.getTimeout()) | ||
| .setConnectTimeout(otlpProperties.getConnectTimeout()) | ||
| .setCompression(String.valueOf(otlpProperties.getCompression()).toLowerCase(Locale.ROOT)); | ||
| otlpProperties.getHeaders().forEach(builder::addHeader); | ||
| return builder.build(); | ||
| } | ||
| } | ||
        
          
          
            28 changes: 28 additions & 0 deletions
          
          28 
        
  ...reactive/src/main/java/io/github/mfvanek/spring/boot3/reactive/config/SecurityConfig.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.config; | ||
| 
     | 
||
| import lombok.SneakyThrows; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; | ||
| import org.springframework.security.config.web.server.ServerHttpSecurity; | ||
| import org.springframework.security.web.server.SecurityWebFilterChain; | ||
| 
     | 
||
| @Configuration(proxyBeanMethods = false) | ||
| @EnableWebFluxSecurity | ||
                
      
                  mfvanek marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| public class SecurityConfig { | ||
| 
     | 
||
| @Bean | ||
| @SneakyThrows | ||
| public SecurityWebFilterChain securityFilterChain(ServerHttpSecurity http) { | ||
| http | ||
| .authorizeExchange(exchanges -> exchanges.anyExchange().permitAll()); | ||
| return http.build(); | ||
| } | ||
| } | ||
        
          
          
            27 changes: 27 additions & 0 deletions
          
          27 
        
  ...eactive/src/main/java/io/github/mfvanek/spring/boot3/reactive/config/WebClientConfig.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.config; | ||
| 
     | 
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
| 
     | 
||
| @Configuration(proxyBeanMethods = false) | ||
| public class WebClientConfig { | ||
| 
     | 
||
| @Value("${app.external-base-url}") | ||
| private String external; | ||
| 
     | 
||
| @Bean | ||
| public WebClient webClient(WebClient.Builder builder) { | ||
| return builder | ||
| .baseUrl(external) | ||
| .build(); | ||
| } | ||
| } | 
        
          
          
            21 changes: 21 additions & 0 deletions
          
          21 
        
  ...ive/src/main/java/io/github/mfvanek/spring/boot3/reactive/controllers/HomeController.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.controllers; | ||
| 
     | 
||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import reactor.core.publisher.Mono; | ||
| 
     | 
||
| @RestController | ||
| public class HomeController { | ||
| 
     | 
||
| @GetMapping("/") | ||
| public Mono<String> home() { | ||
| return Mono.just("Hello!"); | ||
| } | ||
| } | 
        
          
          
            31 changes: 31 additions & 0 deletions
          
          31 
        
  ...src/main/java/io/github/mfvanek/spring/boot3/reactive/controllers/RedirectController.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.controllers; | ||
| 
     | 
||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import reactor.core.publisher.Mono; | ||
| 
     | 
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| 
     | 
||
| @RestController | ||
| public class RedirectController { | ||
| 
     | 
||
| // http://localhost:8080/redirect | ||
| @GetMapping(path = "/redirect") | ||
| public Mono<ResponseEntity<Object>> redirectToGoogle() throws URISyntaxException { | ||
| final URI google = new URI("https://www.google.com"); | ||
| final HttpHeaders httpHeaders = new HttpHeaders(); | ||
| httpHeaders.setLocation(google); | ||
| return Mono.just(new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER)); | ||
| } | ||
| } | 
        
          
          
            59 changes: 59 additions & 0 deletions
          
          59 
        
  ...ive/src/main/java/io/github/mfvanek/spring/boot3/reactive/controllers/TimeController.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
| 
     | 
||
| package io.github.mfvanek.spring.boot3.reactive.controllers; | ||
| 
     | 
||
| import io.github.mfvanek.spring.boot3.reactive.service.KafkaSendingService; | ||
| import io.github.mfvanek.spring.boot3.reactive.service.PublicApiService; | ||
| import io.micrometer.tracing.Span; | ||
| import io.micrometer.tracing.TraceContext; | ||
| import io.micrometer.tracing.Tracer; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import reactor.core.publisher.Mono; | ||
| 
     | 
||
| import java.time.Clock; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ExecutionException; | ||
| 
     | 
||
| @Slf4j | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class TimeController { | ||
| 
     | 
||
| private final Tracer tracer; | ||
| private final Clock clock; | ||
| private final KafkaSendingService kafkaSendingService; | ||
| private final PublicApiService publicApiService; | ||
| 
     | 
||
| // http://localhost:8080/current-time | ||
| @GetMapping(path = "/current-time") | ||
| public Mono<LocalDateTime> getNow() { | ||
| log.trace("tracer {}", tracer); | ||
| final String traceId = Optional.ofNullable(tracer.currentSpan()) | ||
| .map(Span::context) | ||
| .map(TraceContext::traceId) | ||
| .orElse(null); | ||
| log.info("Called method getNow. TraceId = {}", traceId); | ||
| return publicApiService.getZonedTime() | ||
| .switchIfEmpty(Mono.just(LocalDateTime.now(clock))) | ||
| .doOnSuccess(this::sendWithKafka); | ||
| } | ||
| 
     | 
||
| private void sendWithKafka(LocalDateTime localDateTime) { | ||
| try { | ||
| kafkaSendingService.sendNotification("Current time = " + localDateTime) | ||
| .thenRun(() -> log.info("Awaiting acknowledgement from Kafka")) | ||
| .get(); | ||
                
      
                  mfvanek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } catch (InterruptedException | ExecutionException e) { | ||
| log.info("error ", e); | ||
| } | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.