Skip to content

Commit

Permalink
Merge pull request #44 from nawaphonOHM/event-sourcing/main
Browse files Browse the repository at this point in the history
use restTemplate
  • Loading branch information
nawaphonOHM authored Mar 10, 2024
2 parents 0f3b618 + b1d12c4 commit 2519c57
Show file tree
Hide file tree
Showing 18 changed files with 378 additions and 18 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/docker-build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ jobs:
uses: docker/build-push-action@v5
with:
context: ./event_sourcing/consumer
file: ./event_sourcing/consumer/Dockerfile
file: ./messaging/consumer/Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/microservices-lab:event_sourcing.consumer
tags: ${{ secrets.DOCKER_USERNAME }}/microservices-lab:messaging.consumer

- name: Build event_sourcing producer image
uses: docker/build-push-action@v5
with:
context: ./event_sourcing/producer
file: ./event_sourcing/producer/Dockerfile
file: ./messaging/producer/Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/microservices-lab:event_sourcing.producer
tags: ${{ secrets.DOCKER_USERNAME }}/microservices-lab:messaging.producer
4 changes: 2 additions & 2 deletions messaging/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
depends_on: [ zookeeper ]

producer_0:
image: nawaphon2539/microservices-lab:event-sourcing.producer
image: nawaphon2539/microservices-lab:messaging.producer
env_file:
- producer/producer-vars.env
ports:
Expand All @@ -32,7 +32,7 @@ services:
depends_on: [ kafka_0 ]

consumer_0:
image: nawaphon2539/microservices-lab:event-sourcing.consumer
image: nawaphon2539/microservices-lab:messaging.consumer
env_file:
- consumer/consumer-vars.env
ports:
Expand Down
4 changes: 3 additions & 1 deletion messaging/consumer/consumer-vars.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
KAFKA_BOOSTRAP_SERVERS=kafka_0:9092
TZ=Asia/Bangkok
TZ=Asia/Bangkok
FRIEND_IP=http://192.168.1.4:8080/event-sourcing/producer
LOG_LEVEL=DEBUG
9 changes: 8 additions & 1 deletion messaging/consumer/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ main() {

mandatoryEnvCheck() {
ERROR=0

if [ -z "$FRIEND_IP" ]; then
cat >&2 <<-EOE
Error: $FRIEND_IP is required.
EOE
ERROR=1
fi
}



main
main
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@SpringBootApplication
@ConfigurationPropertiesScan(basePackages = "nawaphon.microservices.event_sourcing.consumer.configurations")
public class EventSourcingServiceConsumerApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nawaphon.microservices.event_sourcing.consumer.configurations;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;


@ConfigurationProperties(prefix = "connection")
@ConfigurationPropertiesScan
public class ConnectionConfiguration {
private String friendService;

public String getFriendService() {
return friendService;
}

public void setFriendService(String friendService) {
this.friendService = friendService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nawaphon.microservices.event_sourcing.consumer.configurations;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfiguration {


@Bean
public RestTemplate restTemplate(final RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,76 @@
package nawaphon.microservices.event_sourcing.consumer.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import nawaphon.microservices.event_sourcing.consumer.pojo.Customer;
import nawaphon.microservices.event_sourcing.consumer.pojo.CustomerDetail;
import nawaphon.microservices.event_sourcing.consumer.pojo.Message;
import nawaphon.microservices.event_sourcing.consumer.pojo.ResponseMessage;
import nawaphon.microservices.event_sourcing.consumer.utils.CustomerDetailsParameterizedTypeReference;
import nawaphon.microservices.event_sourcing.consumer.utils.CustomerParameterizedTypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Controller
import java.util.UUID;

@RestController
public class MainController {

private static final Logger logger = LoggerFactory.getLogger(MainController.class);

private final RestTemplate restTemplate;
private final String friendIp;

private final ObjectMapper objectMapper;

public MainController(final RestTemplate restTemplate,
@Value("${connection.friend-service}") final String friendIp,
final ObjectMapper objectMapper) {
this.restTemplate = restTemplate;

this.friendIp = friendIp;
this.objectMapper = objectMapper;
}

@KafkaListener(topics = "Greeting", groupId = "group00")
public void sendEvent(@RequestBody final Message message) {
logger.info("I've received message: {}", message.getMessage());

}

@GetMapping("/get-customer/{uuid}")
public ResponseMessage<Customer> getCustomer(@PathVariable final UUID uuid) {
final String url = friendIp + "/get-customer/" + uuid;
final ResponseMessage<Customer> result = this.restTemplate.exchange(url,
HttpMethod.GET, null, new CustomerParameterizedTypeReference())
.getBody();

try {
logger.debug("Call {}: response: {}", url, objectMapper.writeValueAsString(result));
} catch (JsonProcessingException e) {
logger.error("Unable write log");
}

assert result != null;
return new ResponseMessage<>(HttpStatus.OK.value(), HttpStatus.OK.toString(), result.getResults());
}

@GetMapping("/get-customer-details/{uuid}")
public ResponseMessage<CustomerDetail> getCustomerDetail(@PathVariable final UUID uuid) {
final ResponseMessage<CustomerDetail> result = this.restTemplate.exchange(friendIp + "/get-customer-details/" + uuid,
HttpMethod.GET, null,
new CustomerDetailsParameterizedTypeReference()).getBody();

assert result != null;
return new ResponseMessage<>(HttpStatus.OK.value(), HttpStatus.OK.toString(), result.getResults());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nawaphon.microservices.event_sourcing.consumer.pojo;

import java.util.UUID;

public class Customer {

private final UUID id;

private final UUID detailsId;


public Customer(final UUID id, final UUID detailsId) {
this.id = id;
this.detailsId = detailsId;
}

public UUID getId() {
return id;
}

public UUID getDetailsId() {
return detailsId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nawaphon.microservices.event_sourcing.consumer.pojo;

import java.util.UUID;

public class CustomerDetail {

private final UUID customerId;

private final String firstName;

private final String lastName;


public CustomerDetail(final UUID customerId, final String firstName, final String lastName) {
this.customerId = customerId;
this.firstName = firstName;
this.lastName = lastName;
}

public UUID getCustomerId() {
return customerId;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nawaphon.microservices.event_sourcing.consumer.pojo;

public class ResponseMessage<A> {

private final Number code;
private final String message;
private final A results;

public ResponseMessage(final Number code, final String message, final A results) {
this.code = code;
this.message = message;
this.results = results;
}

public String getMessage() {
return message;
}

public A getResults() {
return results;
}

public Number getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nawaphon.microservices.event_sourcing.consumer.utils;

import nawaphon.microservices.event_sourcing.consumer.pojo.CustomerDetail;
import nawaphon.microservices.event_sourcing.consumer.pojo.ResponseMessage;
import org.springframework.core.ParameterizedTypeReference;

public class CustomerDetailsParameterizedTypeReference extends ParameterizedTypeReference<ResponseMessage<CustomerDetail>> {

public CustomerDetailsParameterizedTypeReference() {
super();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nawaphon.microservices.event_sourcing.consumer.utils;

import nawaphon.microservices.event_sourcing.consumer.pojo.Customer;
import nawaphon.microservices.event_sourcing.consumer.pojo.ResponseMessage;
import org.springframework.core.ParameterizedTypeReference;

public class CustomerParameterizedTypeReference extends ParameterizedTypeReference<ResponseMessage<Customer>> {

public CustomerParameterizedTypeReference() {
super();
}
}
2 changes: 2 additions & 0 deletions messaging/consumer/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.UUI
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=*
spring.kafka.consumer.properties.spring.json.type.mapping=message:nawaphon.microservices.event_sourcing.consumer.pojo.Message
connection.friend-service=${FRIEND_IP}
logging.level.root=${LOG_LEVEL:INFO}


Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package nawaphon.microservices.event_sourcing.producer.components;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import nawaphon.microservices.event_sourcing.producer.pojo.Customer;
import nawaphon.microservices.event_sourcing.producer.pojo.CustomerDetail;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
public class FakeDatabaseComponent {
private static final Logger logger = LogManager.getLogger(FakeDatabaseComponent.class);

private final List<Customer> customers = new ArrayList<>();
private final List<CustomerDetail> customerDetails = new ArrayList<>();

private final ObjectMapper objectMapper;

public FakeDatabaseComponent(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public List<Customer> getCustomers() {
return customers;
}

public List<CustomerDetail> getCustomerDetails() {
return customerDetails;
}

@PostConstruct
public void makeData() {
Customer customerFake;
CustomerDetail customerDetailFake;

{
customerFake = new Customer(UUID.randomUUID(), UUID.randomUUID());
customerDetailFake = new CustomerDetail(customerFake.getDetailsId(), "John", "Doe");
customers.add(customerFake);
customerDetails.add(customerDetailFake);
try {
logger.info("Customer and CustomerDetail added: {}", objectMapper.writeValueAsString(customerFake));
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to serialize value: ", e);
}
}

{
customerFake = new Customer(UUID.randomUUID(), UUID.randomUUID());
customerDetailFake = new CustomerDetail(customerFake.getDetailsId(), "Jane", "Smith");
customers.add(customerFake);
customerDetails.add(customerDetailFake);
try {
logger.info("Customer and CustomerDetail added: {}", objectMapper.writeValueAsString(customerFake));
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to serialize value: ", e);
}
}

{
customerFake = new Customer(UUID.randomUUID(), UUID.randomUUID());
customerDetailFake = new CustomerDetail(customerFake.getDetailsId(), "Bob", "Johnson");
customers.add(customerFake);
customerDetails.add(customerDetailFake);
try {
logger.info("Customer and CustomerDetail added: {}", objectMapper.writeValueAsString(customerFake));
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to serialize value:", e);
}
}
}
}
Loading

0 comments on commit 2519c57

Please sign in to comment.