Skip to content

Commit

Permalink
chore(track): add kafka event externalization
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-ju authored and denniseffing committed Nov 10, 2023
1 parent c7eb072 commit d6a920c
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 17 deletions.
24 changes: 13 additions & 11 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ The goal is to provide infrastructure configurations which...

Each habitcentric service runs with the same ports across all deployment options.

| Service | Port |
|-------------------|-------|
| gateway | 9000 |
| habit | 9001 |
| habit-postgres | 10001 |
| track | 9002 |
| track-postgres | 10002 |
| report | 9003 |
| ui | 9004 |
| keycloak | 8080 |
| keycloak-postgres | 10003 |
| Service | Port |
|-------------------|---------------|
| gateway | 9000 |
| habit | 9001 |
| habit-postgres | 10001 |
| track | 9002 |
| track-postgres | 10002 |
| report | 9003 |
| ui | 9004 |
| keycloak | 8080 |
| keycloak-postgres | 10003 |
| kafka | 11001 - 11003 |
| kafka-ui | 11001 - 11003 |
32 changes: 32 additions & 0 deletions infrastructure/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ services:
- track-db
environment:
DB_HOST: track-db
KAFKA_BOOTSTRAP_SERVERS: kafka:11001
networks:
- habitcentric-net
track-db:
Expand All @@ -111,6 +112,37 @@ services:
TRACKING_SERVICE_HOST: http://localhost:9000/
networks:
- habitcentric-net
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=track,kafka-ui
- KAFKA_CLIENT_PASSWORDS=track,kafka-ui
networks:
- habitcentric-net
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";'
networks:
- habitcentric-net

volumes:
auth-keycloak-db-data:
networks:
Expand Down
17 changes: 17 additions & 0 deletions infrastructure/istio/config/23-mtls-authz-policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ spec:
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: hc-kafka-authz
namespace: hc-kafka
spec:
action: ALLOW
selector:
matchLabels:
app.kubernetes.io/name: kafka
app.kubernetes.io/instance: hc-kafka
rules:
- from:
- source:
principals:
- cluster.local/ns/hc-track/sa/track
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: keycloak-authz
namespace: hc-keycloak
Expand Down
20 changes: 20 additions & 0 deletions infrastructure/kubernetes/helmfile.d/00-namespaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ releases:
"kuma.io/mesh": "default"
{{- end }}
spec:
- apiVersion: v1
kind: Namespace
metadata:
name: hc-kafka
{{- if eq .Environment.Name "istio" }}
labels:
"istio-injection": "enabled"
{{- end }}
{{- if eq .Environment.Name "linkerd" }}
annotations:
"linkerd.io/inject": "enabled"
"config.linkerd.io/trace-collector": "linkerd-collector.linkerd:55678"
"config.alpha.linkerd.io/trace-collector-service-account": "linkerd-collector"
{{- end }}
{{- if eq .Environment.Name "kuma" }}
annotations:
"kuma.io/sidecar-injection": "enabled"
"kuma.io/mesh": "default"
{{- end }}
spec:
- apiVersion: v1
kind: Namespace
metadata:
Expand Down
16 changes: 16 additions & 0 deletions infrastructure/kubernetes/helmfile.d/15-kafka.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bases:
- environments.yaml

repositories:
- name: bitnami-archive-full-index
url: https://raw.githubusercontent.com/bitnami/charts/archive-full-index/bitnami

releases:
- name: kafka
namespace: hc-kafka
labels:
service: kafka
chart: bitnami-archive-full-index/kafka
version: 25.3.1
values:
- ./values/kafka-values.yaml.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
controller:
replicaCount: 1
kraft:
# generated with: uuidgen | tr -d '-' | base64 | cut -b 1-22
clusterId: "ZDQ2MzI3MWEwNmIwNGMyMT"
sasl:
interbroker:
password: "kafka"
controller:
password: "kafka"
client:
users:
- "track"
passwords:
- "track"
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ image:
pullPolicy: Always

## habitcentric track 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
1 change: 1 addition & 0 deletions services/track/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ dependencies {
// Spring Modulith
implementation "org.springframework.modulith:spring-modulith-starter-core"
implementation "org.springframework.modulith:spring-modulith-starter-jpa"
implementation "org.springframework.modulith:spring-modulith-events-kafka"
intTestImplementation "org.springframework.modulith:spring-modulith-starter-test"

implementation 'io.micrometer:micrometer-tracing-bridge-otel'
Expand Down
30 changes: 29 additions & 1 deletion services/track/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,35 @@ services:
image: bitnami/postgresql:15
restart: always
ports:
- 10002:10002
- "10002:10002"
environment:
POSTGRESQL_PASSWORD: postgres
POSTGRESQL_PORT_NUMBER: 10002

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=track,kafka-ui
- KAFKA_CLIENT_PASSWORDS=track,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 @@ -26,7 +26,7 @@ void shouldPublishHabitTrackingCreatedEventWhenHabitTrackingIsSaved(Scenario sce
() ->
habitTrackingController.putHabitTrackingRecords(
"userId", 1L, Set.of(LocalDate.parse("2023-09-29"))))
.andWaitForEventOfType(HabitTracking.Created.class)
.andWaitForEventOfType(HabitTracking.DateTracked.class)
.toArriveAndVerify(
event -> {
assertThat(event.habitId()).isEqualTo(1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.modulith.events.Externalized;

@Entity
@AllArgsConstructor
Expand All @@ -34,7 +35,7 @@ public class HabitTracking extends AbstractAggregateRoot<HabitTracking> {
public HabitTracking(String userId, Long habitId, LocalDate trackDate) {
this.id = new HabitTracking.Id(userId, habitId, trackDate);

registerEvent(new HabitTracking.Created(userId, habitId, trackDate));
registerEvent(new DateTracked(userId, habitId, trackDate));
}

@Embeddable
Expand All @@ -55,5 +56,10 @@ public static class Id implements Serializable {
@NotNull private LocalDate trackDate;
}

public record Created(String userId, Long habitId, LocalDate trackDate) {}
@Externalized("habit-tracking-events::#{#this.getId()}")
public record DateTracked(String userId, Long habitId, LocalDate trackDate) {
public String getId() {
return userId + "-" + habitId;
}
}
}
9 changes: 9 additions & 0 deletions services/track/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ spring:
lob:
# https://github.com/spring-projects/spring-boot/issues/12007#issuecomment-370774241
non_contextual_creation: true

kafka:
client-id: track
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:track}" password="${KAFKA_PASSWORD:track}";'

server:
port: 9002
error:
Expand Down
7 changes: 7 additions & 0 deletions services/track/track-request.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PUT localhost:9002/track/users/12345/habits/1
Content-Type: application/json

[
"2021-01-01",
"2021-01-02"
]

0 comments on commit d6a920c

Please sign in to comment.