Skip to content

Commit

Permalink
feat : Using SB AutoConfiguration, enable observability and documenta…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
rajadilipkolli committed Jan 6, 2024
1 parent aee7131 commit 418c44f
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 122 deletions.
141 changes: 77 additions & 64 deletions kafka-sample/spring-modulith-outbox-pattern/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,65 +1,78 @@
version: '3.8'
services:

postgresql:
image: postgres:16.1-alpine
environment:
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=appdb
ports:
- "5432:5432"

pgadmin4:
container_name: pgadmin4
image: dpage/pgadmin4
extra_hosts: [ 'host.docker.internal:host-gateway' ]
environment:
- PGADMIN_DEFAULT_EMAIL=ap
- PGADMIN_DEFAULT_PASSWORD=admin
ports:
- "5050:80"
depends_on:
- postgresql
restart: unless-stopped

broker:
image: confluentinc/cp-kafka:7.5.3
hostname: broker
ports:
- "9092:9092"
healthcheck:
test: [ "CMD-SHELL", "nc -z localhost 9092" ]
interval: 30s
timeout: 5s
retries: 3
environment:
KAFKA_BROKER_ID: 1
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL_HOST:PLAINTEXT,CONTROLLER:PLAINTEXT
#Use two listeners with different names, it will force Kafka to communicate with itself via internal
#listener when KAFKA_INTER_BROKER_LISTENER_NAME is set, otherwise Kafka will try to use the advertised listener
KAFKA_LISTENERS: INTERNAL://broker:29092,EXTERNAL_HOST://0.0.0.0:9092,CONTROLLER://broker:9094
KAFKA_ADVERTISED_LISTENERS: INTERNAL://broker:29092,EXTERNAL_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS: 32
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_NODE_ID: 1
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@broker:9094
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
KAFKA_LOG_FLUSH_INTERVAL_MESSAGES: 9223372036854775807
CLUSTER_ID: 4L6g3nShT-eMCtK--X86sw

kafdrop:
image: obsidiandynamics/kafdrop
restart: "no"
ports:
- "9000:9000"
environment:
KAFKA_BROKERCONNECT: "broker:29092"
depends_on:
broker:
version: '3.8'
services:

postgresql:
image: postgres:16.1-alpine
environment:
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=appdb
ports:
- "5432:5432"

pgadmin4:
container_name: pgadmin4
image: dpage/pgadmin4
extra_hosts: [ 'host.docker.internal:host-gateway' ]
environment:
- [email protected]
- PGADMIN_DEFAULT_PASSWORD=admin
ports:
- "5050:80"
depends_on:
- postgresql
restart: unless-stopped

zipkin-server:
image: openzipkin/zipkin
container_name: zipkin-server
extra_hosts: [ 'host.docker.internal:host-gateway' ]
deploy:
resources:
limits:
memory: 512m
expose:
- "9411"
ports:
- "9411:9411"

broker:
image: confluentinc/cp-kafka:7.5.3
hostname: broker
ports:
- "9092:9092"
healthcheck:
test: [ "CMD-SHELL", "nc -z localhost 9092" ]
interval: 30s
timeout: 5s
retries: 3
environment:
KAFKA_BROKER_ID: 1
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL_HOST:PLAINTEXT,CONTROLLER:PLAINTEXT
#Use two listeners with different names, it will force Kafka to communicate with itself via internal
#listener when KAFKA_INTER_BROKER_LISTENER_NAME is set, otherwise Kafka will try to use the advertised listener
KAFKA_LISTENERS: INTERNAL://broker:29092,EXTERNAL_HOST://0.0.0.0:9092,CONTROLLER://broker:9094
KAFKA_ADVERTISED_LISTENERS: INTERNAL://broker:29092,EXTERNAL_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS: 32
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_NODE_ID: 1
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@broker:9094
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
KAFKA_LOG_FLUSH_INTERVAL_MESSAGES: 9223372036854775807
CLUSTER_ID: 4L6g3nShT-eMCtK--X86sw

kafdrop:
image: obsidiandynamics/kafdrop
restart: "no"
ports:
- "9000:9000"
environment:
KAFKA_BROKERCONNECT: "broker:29092"
depends_on:
broker:
condition: service_healthy
23 changes: 17 additions & 6 deletions kafka-sample/spring-modulith-outbox-pattern/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,33 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>

<!-- Moduliths Starts -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-core</artifactId>
<artifactId>spring-modulith-events-kafka</artifactId>
</dependency>
<!-- Moduliths Ends -->

<!-- Observability Starts -->
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jpa</artifactId>
<artifactId>spring-modulith-starter-insight</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events-kafka</artifactId>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<!-- Observability Ends -->

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
= Spring Modulith Outbox pattern
:modulith-docs: ../../../target/spring-modulith-docs

== Overview

plantuml::{modulith-docs}/components.puml[format="svg"]

== Orders

plantuml::{modulith-docs}/module-order.puml[format="svg"]
include::{modulith-docs}/module-order.adoc[]

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.outboxpattern.config.logging;
package com.example.outboxpattern.config;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.outboxpattern.config.logging;
package com.example.outboxpattern.config;

import com.example.outboxpattern.utils.AppConstants;
import org.aspectj.lang.JoinPoint;
Expand Down Expand Up @@ -32,8 +32,8 @@ public void springBeanPointcut() {
// pointcut definition
}

@Pointcut("@within(com.example.outboxpattern.config.logging.Loggable) || "
+ "@annotation(com.example.outboxpattern.config.logging.Loggable)")
@Pointcut(
"@within(com.example.outboxpattern.config.Loggable) || @annotation(com.example.outboxpattern.config.Loggable)")
public void applicationPackagePointcut() {
// pointcut definition
}
Expand All @@ -60,17 +60,17 @@ public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {

@Around("applicationPackagePointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
if (log.isTraceEnabled()) {
log.trace(
if (log.isDebugEnabled()) {
log.debug(
"Enter: {}.{}()",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName());
}
long start = System.currentTimeMillis();
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
if (log.isTraceEnabled()) {
log.trace(
if (log.isDebugEnabled()) {
log.debug(
"Exit: {}.{}(). Time taken: {} millis",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.outboxpattern.order.internal;

import com.example.outboxpattern.config.Loggable;
import com.example.outboxpattern.order.OrderResponse;
import com.example.outboxpattern.order.internal.query.FindOrdersQuery;
import com.example.outboxpattern.order.internal.request.OrderRequest;
Expand All @@ -24,6 +25,7 @@
@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
@Loggable
public class OrderController {

private final OrderService orderService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.outboxpattern.order.internal;

import com.example.outboxpattern.config.Loggable;
import com.example.outboxpattern.order.OrderResponse;
import com.example.outboxpattern.order.internal.request.OrderRequest;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
@Loggable
class OrderMapper {

Order toEntity(OrderRequest orderRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.outboxpattern.order.internal;

import com.example.outboxpattern.config.Loggable;
import com.example.outboxpattern.order.OrderResponse;
import com.example.outboxpattern.order.internal.query.FindOrdersQuery;
import com.example.outboxpattern.order.internal.request.OrderRequest;
Expand All @@ -18,6 +19,7 @@
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Loggable
class OrderService {

private final OrderRepository orderRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ spring.jmx.enabled=false
spring.mvc.problemdetails.enabled=true

################ Actuator #####################
management.endpoints.web.exposure.include=configprops,env,health,info,logfile,loggers,metrics,prometheus
management.endpoints.web.exposure.include=configprops,env,health,info,logfile,loggers,metrics,prometheus,modulith,applicationmodules
management.endpoint.health.show-details=always

################ Database #####################
spring.jpa.show-sql=false
spring.jpa.open-in-view=false
spring.data.jpa.repositories.bootstrap-mode=deferred
#spring.data.jpa.repositories.bootstrap-mode=deferred
spring.datasource.hikari.auto-commit=false
spring.jpa.hibernate.ddl-auto=none
#spring.jpa.properties.hibernate.format_sql=true
Expand All @@ -34,4 +34,6 @@ spring.kafka.producer.properties.spring.json.add.type.headers=false

spring.modulith.republish-outstanding-events-on-restart=true
#spring.modulith.events.externalization.enabled=true
spring.testcontainers.beans.startup=parallel
spring.testcontainers.beans.startup=parallel

management.tracing.sampling.probability=1.0
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<logger name="org.springframework.modulith" level="DEBUG"/>
<logger name="org.springframework.orm.jpa" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="com.tngtech.archunit" level="WARN" />

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -28,4 +29,10 @@ KafkaContainer kafkaContainer(DynamicPropertyRegistry dynamicPropertyRegistry) {
dynamicPropertyRegistry.add("spring.kafka.producer.bootstrap-servers", kafkaContainer::getBootstrapServers);
return kafkaContainer;
}

@Bean
@ServiceConnection(name = "openzipkin/zipkin")
GenericContainer<?> zipkinContainer() {
return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin:latest")).withExposedPorts(9411);
}
}

0 comments on commit 418c44f

Please sign in to comment.