Skip to content

Commit

Permalink
chore(habit): add kafka event externalization
Browse files Browse the repository at this point in the history
  • Loading branch information
denniseffing committed Jan 3, 2024
1 parent 2fc2a82 commit 1b7423f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
5 changes: 3 additions & 2 deletions infrastructure/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ services:
- habit-db
environment:
DB_HOST: habit-db
KAFKA_BOOTSTRAP_SERVERS: kafka:11001
networks:
- habitcentric-net
habit-db:
Expand Down Expand Up @@ -124,8 +125,8 @@ services:
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:11001,EXTERNAL://localhost:11003
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:11002
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CLIENT_USERS=track,kafka-ui
- KAFKA_CLIENT_PASSWORDS=track,kafka-ui
- KAFKA_CLIENT_USERS=habit,track,kafka-ui
- KAFKA_CLIENT_PASSWORDS=habit,track,kafka-ui
networks:
- habitcentric-net
kafka-ui:
Expand Down
1 change: 1 addition & 0 deletions infrastructure/istio/config/23-mtls-authz-policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ spec:
- from:
- source:
principals:
- cluster.local/ns/hc-habit/sa/habit
- cluster.local/ns/hc-track/sa/track
---
apiVersion: security.istio.io/v1beta1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ image:

## habitcentric habit service configuration

{{- if (eq .Environment.Name "traefik-mesh") }}
extraEnv:
- name: KAFKA_BOOTSTRAP_SERVERS
value: kafka.hc-kafka.svc.cluster.local:9092
{{- if (eq .Environment.Name "traefik-mesh") }}
- name: MANAGEMENT_ZIPKIN_TRACING_ENDPOINT
value: http://jaeger-collector.traefik-mesh.svc.cluster.local:9411
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ sasl:
password: "kafka"
client:
users:
- "habit"
- "track"
passwords:
- "habit"
- "track"

externalAccess:
Expand Down
28 changes: 28 additions & 0 deletions services/habit/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@ services:
environment:
POSTGRESQL_PASSWORD: postgres
POSTGRESQL_PORT_NUMBER: 10001

kafka:
image: 'bitnami/kafka:latest'
ports:
- "11003:11003"
environment:
- KAFKA_CFG_NODE_ID=0
- KAFKA_CFG_PROCESS_ROLES=controller,broker
- KAFKA_CFG_LISTENERS=PLAINTEXT://:11001,CONTROLLER://:11002,EXTERNAL://:11003
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:SASL_PLAINTEXT,EXTERNAL:SASL_PLAINTEXT
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:11001,EXTERNAL://localhost:11003
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:11002
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CLIENT_USERS=habit,kafka-ui
- KAFKA_CLIENT_PASSWORDS=habit,kafka-ui

kafka-ui:
container_name: kafka-ui
image: provectuslabs/kafka-ui:latest
ports:
- "11004:11004"
environment:
SERVER_PORT: 11004
DYNAMIC_CONFIG_ENABLED: 'true'
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:11001
KAFKA_CLUSTERS_0_PROPERTIES_SECURITY_PROTOCOL: SASL_PLAINTEXT
KAFKA_CLUSTERS_0_PROPERTIES_SASL_MECHANISM: PLAIN
KAFKA_CLUSTERS_0_PROPERTIES_SASL_JAAS_CONFIG: 'org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka-ui" password="kafka-ui";'
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.modulith.events.Externalized;

@Entity
@Builder
Expand Down Expand Up @@ -116,12 +117,14 @@ public static class ModificationRequest {
@NotNull @Valid private Schedule schedule;
}

@Externalized("habit-events::#{#this.habitId}")
public record HabitCreated(
UUID habitId, String userId, String name, Schedule.Frequency frequency, Integer repetitions) {
public String getId() {
return habitId.toString();
}
}
UUID habitId,
String userId,
String name,
Schedule.Frequency frequency,
Integer repetitions) {}

@Externalized("habit-events::#{#this.habitId}")
public record HabitDeleted(UUID habitId) {}
}
11 changes: 11 additions & 0 deletions services/habit/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ spring:
liquibase:
change-log: db/changelog/db.changelog-master.yaml
default-schema: hc_habit

kafka:
client-id: habit
bootstrap-servers: ${KAFKA_BOOTSTRAP_SERVERS:localhost:11003}
properties:
"security.protocol": SASL_PLAINTEXT
"sasl.mechanism": PLAIN
"sasl.jaas.config": 'org.apache.kafka.common.security.plain.PlainLoginModule required username="${KAFKA_USER:habit}" password="${KAFKA_PASSWORD:habit}";'
producer:
properties:
"spring.json.type.mapping": "habit-created:de.codecentric.hc.habit.habits.Habit.HabitCreated,habit-deleted:de.codecentric.hc.habit.habits.Habit.HabitDeleted"
logging:
level:
liquibase.executor: WARN
Expand Down

0 comments on commit 1b7423f

Please sign in to comment.