Change project to run spring-boot locally and kafka,zoo and mongo in docker-compose

This commit is contained in:
Diego Rubio
2021-02-04 16:49:26 +01:00
parent a7deed17c2
commit d3a92da643
71 changed files with 106 additions and 148 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
/.idea
/zk-single-kafka-single
/data0
/data1

View File

@@ -1,21 +0,0 @@
FROM maven:3.6.3-openjdk-8-slim AS build
COPY demo-command/src /usr/src/demo-cqrs/demo-command/src
COPY demo-command/pom.xml /usr/src/demo-cqrs/demo-command
COPY demo-query/src /usr/src/demo-cqrs/demo-query/src
COPY demo-query/pom.xml /usr/src/demo-cqrs/demo-query
COPY pom.xml /usr/src/demo-cqrs
RUN mvn -f /usr/src/demo-cqrs/pom.xml clean package
FROM openjdk:8-alpine AS demo-command
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
COPY --from=build /usr/src/demo-cqrs/demo-command/target/demo-command-0.0.1-SNAPSHOT.jar /usr/app/demo-command-0.0.1-SNAPSHOT.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","/usr/app/demo-command-0.0.1-SNAPSHOT.jar"]
FROM openjdk:8-alpine AS demo-query
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
COPY --from=build /usr/src/demo-cqrs/demo-query/target/demo-query-0.0.1-SNAPSHOT.jar /usr/app/demo-query-0.0.1-SNAPSHOT.jar
EXPOSE 8082
ENTRYPOINT ["java","-jar","/usr/app/demo-query-0.0.1-SNAPSHOT.jar"]

2
data0/WiredTiger Normal file
View File

@@ -0,0 +1,2 @@
WiredTiger
WiredTiger 10.0.0: (March 18, 2020)

1
data0/WiredTiger.lock Normal file
View File

@@ -0,0 +1 @@
WiredTiger lock file

6
data0/WiredTiger.turtle Normal file
View File

@@ -0,0 +1,6 @@
WiredTiger version string
WiredTiger 10.0.0: (March 18, 2020)
WiredTiger version
major=10,minor=0,patch=0
file:WiredTiger.wt
access_pattern_hint=none,allocation_size=4KB,app_metadata=,assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=,cache_resident=false,checksum=uncompressed,collator=,columns=,dictionary=0,encryption=(keyid=,name=),format=btree,huffman_value=,id=0,ignore_in_memory_cache_size=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=S,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=0,log=(enabled=true),memory_page_image_max=0,memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,value_format=S,version=(major=1,minor=1),checkpoint=(WiredTigerCheckpoint.45=(addr="018381e4539bcb7f8481e41f4fdcd18581e4d2e42e9a808080e3010fc0e23fc0",order=45,time=1612453682,size=28672,newest_start_durable_ts=0,oldest_start_ts=0,newest_txn=23,newest_stop_durable_ts=0,newest_stop_ts=-1,newest_stop_txn=-11,prepare=0,write_gen=120,run_write_gen=98)),checkpoint_backup_info=,checkpoint_lsn=(8,19072)

BIN
data0/WiredTiger.wt Normal file

Binary file not shown.

BIN
data0/WiredTigerHS.wt Normal file

Binary file not shown.

BIN
data0/_mdb_catalog.wt Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
data0/sizeStorer.wt Normal file

Binary file not shown.

BIN
data0/storage.bson Normal file

Binary file not shown.

2
data1/WiredTiger Normal file
View File

@@ -0,0 +1,2 @@
WiredTiger
WiredTiger 10.0.0: (March 18, 2020)

1
data1/WiredTiger.lock Normal file
View File

@@ -0,0 +1 @@
WiredTiger lock file

6
data1/WiredTiger.turtle Normal file
View File

@@ -0,0 +1,6 @@
WiredTiger version string
WiredTiger 10.0.0: (March 18, 2020)
WiredTiger version
major=10,minor=0,patch=0
file:WiredTiger.wt
access_pattern_hint=none,allocation_size=4KB,app_metadata=,assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=,cache_resident=false,checksum=uncompressed,collator=,columns=,dictionary=0,encryption=(keyid=,name=),format=btree,huffman_value=,id=0,ignore_in_memory_cache_size=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=S,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=0,log=(enabled=true),memory_page_image_max=0,memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,value_format=S,version=(major=1,minor=1),checkpoint=(WiredTigerCheckpoint.45=(addr="018381e4b7f568b08481e41f4fdcd18581e48de68736808080e3010fc0e23fc0",order=45,time=1612453684,size=28672,newest_start_durable_ts=0,oldest_start_ts=0,newest_txn=21,newest_stop_durable_ts=0,newest_stop_ts=-1,newest_stop_txn=-11,prepare=0,write_gen=120,run_write_gen=98)),checkpoint_backup_info=,checkpoint_lsn=(8,17920)

BIN
data1/WiredTiger.wt Normal file

Binary file not shown.

BIN
data1/WiredTigerHS.wt Normal file

Binary file not shown.

BIN
data1/_mdb_catalog.wt Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
data1/sizeStorer.wt Normal file

Binary file not shown.

BIN
data1/storage.bson Normal file

Binary file not shown.

View File

@@ -3,6 +3,13 @@
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>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
<parent>
<groupId>es.dbiosag</groupId>

View File

@@ -1,27 +1,20 @@
package es.dbiosag.command.domain.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Builder;
import lombok.Data;
import javax.persistence.*;
import org.springframework.data.annotation.Id;
@Data
@Entity
@Table(name = "PHONES")
@Builder
public class Phone {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "ID_PHONE")
private Integer id;
@Column(name = "NAME")
@JsonIgnore
private String id;
private String name;
@Column(name = "MODEL")
private String model;
@Column(name = "COLOR")
private String color;
@Column(name = "PRICE")
private Double price;
@Column(name = "CREATION_DATE")
private String creationDate;
}

View File

@@ -11,8 +11,6 @@ import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
@Log4j2
public class CreateUserService {
@@ -24,7 +22,6 @@ public class CreateUserService {
@Autowired
private KafkaPhoneCreatedEventSourcing kafkaPhoneCreatedEventSourcing;
@Transactional
public PhoneCreatedEvent create(CreatePhoneRequest request) {
log.info("Creating new phone");
val phone = phoneConverter.createPhoneRequestRequestToPhone(request);

View File

@@ -1,9 +1,9 @@
package es.dbiosag.command.infrasturcture.repository;
import es.dbiosag.command.domain.model.Phone;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PhoneRepository extends CrudRepository<Phone, Integer> {
public interface PhoneRepository extends MongoRepository<Phone, Integer> {
}

View File

@@ -1,10 +0,0 @@
DROP TABLE IF EXISTS phones;
CREATE TABLE phones (
id_phone INT AUTO_INCREMENT PRIMARY KEY,
creation_date VARCHAR(250) NOT NULL,
name VARCHAR(250) NOT NULL,
model VARCHAR(250) NOT NULL,
price VARCHAR(250) NOT NULL,
color VARCHAR(250) NOT NULL
);

View File

@@ -1,19 +1,16 @@
server:
port: 8081
spring:
datasource:
driverClassName: org.h2.Driver
password: 1234
url: jdbc:h2:mem:commanddb
username: sa
jpa:
database-platform: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
data:
mongodb:
authentication-database: examples
database: examples
username: api_user
password: NdEep0XLpMNKUmgQVa81oDCx7mrSRodh0Z79qdX3
host: localhost
port: 30000
kafka:
bootstrap-servers: kafka1:9092
bootstrap-servers: localhost:29092
consumer:
group-id: test-consumer-group
message:

View File

@@ -1,27 +1,19 @@
package es.dbiosag.query.domain.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import javax.persistence.*;
import org.springframework.data.annotation.Id;
@Data
@Entity
@Table(name = "PHONES")
public class Phone {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "ID_PHONE")
private Integer id;
@Column(name = "NAME")
@JsonIgnore
private String id;
private String name;
@Column(name = "MODEL")
private String model;
@Column(name = "COLOR")
private String color;
@Column(name = "PRICE")
private Double price;
@Column(name = "CREATION_DATE")
private String creationDate;
public Phone(){ }

View File

@@ -9,8 +9,6 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
@Log4j2
public class FindPhoneService {
@@ -21,12 +19,9 @@ public class FindPhoneService {
private PhoneConverter phoneConverter;
public PhoneResponse findByName(String name) throws PhoneNotFoundException {
Optional<Phone> phone = phoneRepository.findByName(name);
if(!phone.isPresent()) {
throw new PhoneNotFoundException(name, "Phone not found");
}
Phone phone = phoneRepository.findByName(name).orElseThrow(() -> new PhoneNotFoundException(name, "Phone not found"));
log.info("Find phone: {}", phone);
return phoneConverter.phoneToPhoneResponse(phone.get());
return phoneConverter.phoneToPhoneResponse(phone);
}
public void createPhone(Phone p) {

View File

@@ -1,13 +1,14 @@
package es.dbiosag.query.infrastructure.repository;
import es.dbiosag.query.domain.model.Phone;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface PhoneRepository extends CrudRepository<Phone, Integer> {
public interface PhoneRepository extends MongoRepository<Phone, Integer> {
Optional<Phone> findByName(String name);
}

View File

@@ -1,19 +1,16 @@
server:
port: 8082
spring:
datasource:
driverClassName: org.h2.Driver
password: 1234
url: jdbc:h2:mem:querydb
username: sa
jpa:
database-platform: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
data:
mongodb:
authentication-database: examples
database: examples
username: api_user
password: NdEep0XLpMNKUmgQVa81oDCx7mrSRodh0Z79qdX3
host: localhost
port: 30001
kafka:
bootstrap-servers: kafka1:9092
bootstrap-servers: localhost:29092
consumer:
group-id: test-consumer-group
message:

View File

@@ -1,10 +0,0 @@
DROP TABLE IF EXISTS phones;
CREATE TABLE phones (
id_phone INT AUTO_INCREMENT PRIMARY KEY,
creation_date VARCHAR(250) NOT NULL,
name VARCHAR(250) NOT NULL,
model VARCHAR(250) NOT NULL,
price VARCHAR(250) NOT NULL,
color VARCHAR(250) NOT NULL
);

View File

@@ -7,7 +7,7 @@ services:
ports:
- "2181:2181"
networks:
- kafka_net
- my_network
environment:
ZOO_MY_ID: 1
ZOO_PORT: 2181
@@ -16,6 +16,40 @@ services:
- ./zk-single-kafka-single/zoo1/data:/data
- ./zk-single-kafka-single/zoo1/datalog:/datalog
mongo-command:
image: mongo:latest
container_name: mongo-command
hostname: mongo-command
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
MONGO_INITDB_DATABASE: examples
command: ["--bind_ip_all"]
ports:
- "30000:27017"
networks:
- my_network
volumes:
- ./init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
- ./data0:/data/db
mongo-query:
image: mongo:latest
container_name: mongo-query
hostname: mongo-query
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
MONGO_INITDB_DATABASE: examples
command: ["--bind_ip_all"]
ports:
- "30001:27017"
networks:
- my_network
volumes:
- ./init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
- ./data1:/data/db
kafka1:
image: confluentinc/cp-kafka:5.5.1
hostname: kafka1
@@ -36,55 +70,13 @@ services:
depends_on:
- zoo1
networks:
- kafka_net
- my_network
healthcheck:
test: [ "CMD", "nc", "-vz", "localhost", "9092" ]
interval: 30s
timeout: 10s
retries: 3
demo-command:
build:
context: .
dockerfile: Dockerfile
image: demo-command
hostname: demo-command
ports:
- "8081:8081"
depends_on:
kafka1:
condition: service_healthy
networks:
- kafka_net
demo-query-1:
build:
context: .
dockerfile: Dockerfile
image: demo-query
hostname: demo-query-1
ports:
- "8082:8082"
depends_on:
kafka1:
condition: service_healthy
networks:
- kafka_net
# demo-query-2:
# build:
# context: demo-query
# dockerfile: Dockerfile
# image: demo-query
# hostname: demo-query-2
# ports:
# - "8083:8082"
# depends_on:
# kafka1:
# condition: service_healthy
# networks:
# - kafka_net
networks:
kafka_net:
my_network:
driver: bridge

8
init-mongo.js Normal file
View File

@@ -0,0 +1,8 @@
db.createCollection('examples');
db.createUser(
{
user: 'api_user',
pwd: 'NdEep0XLpMNKUmgQVa81oDCx7mrSRodh0Z79qdX3',
roles: [{ role: 'readWrite', db: 'examples' }],
},
);

View File

@@ -36,7 +36,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>