Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use restTemplate #44

Merged
merged 44 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e07162f
Add Customer POJO
nawaphonOHM Mar 9, 2024
19c670b
Add CustomerDetail POJO
nawaphonOHM Mar 9, 2024
60f850e
Implement FakeDatabaseComponent to mock data
nawaphonOHM Mar 9, 2024
4f25054
Add new Customer POJO
nawaphonOHM Mar 9, 2024
f5842b7
Also included are getter methods for first and last names.
nawaphonOHM Mar 9, 2024
27c1fb2
Implement FakeDatabaseComponent in MainController
nawaphonOHM Mar 9, 2024
ad59494
Add Logger to FakeDatabaseComponent and simplify code blocks
nawaphonOHM Mar 9, 2024
6b67d21
The commit message doesn't match the code diff provided. Based on thi…
nawaphonOHM Mar 9, 2024
6e75003
Add 'detailsId' field to 'Customer' class
nawaphonOHM Mar 9, 2024
81bf6a3
Add 'getCustomer' method to MainController
nawaphonOHM Mar 9, 2024
35998b6
Add 'getCustomerDetail' method to MainController
nawaphonOHM Mar 9, 2024
eac2132
Update the way FakeDatabaseComponent adds customers
nawaphonOHM Mar 9, 2024
6b01f6c
Add new ResponseMessage POJO class
nawaphonOHM Mar 9, 2024
3f723df
Add RestTemplateConfiguration class
nawaphonOHM Mar 9, 2024
6046a5f
Enable configuration properties scan in EventSourcingServiceConsumer
nawaphonOHM Mar 9, 2024
ae2b5ab
Add ConnectionConfiguration file in EventSourcingServiceConsumer
nawaphonOHM Mar 9, 2024
9c185f4
Update application properties for friend-service connection
nawaphonOHM Mar 9, 2024
afa84e2
Add 'ConfigurationPropertiesScan' annotation in ConnectionConfiguration
nawaphonOHM Mar 9, 2024
d2adf13
Inject RestTemplate into MainController
nawaphonOHM Mar 9, 2024
4989aaf
Add friendIp dependency to MainController
nawaphonOHM Mar 9, 2024
a7697b0
Add new endpoints to MainController
nawaphonOHM Mar 9, 2024
c0d16cd
update imports
nawaphonOHM Mar 9, 2024
fb833ee
Add FRIEND_IP error check in entrypoint.sh
nawaphonOHM Mar 10, 2024
8d25230
Add FRIEND_IP variable to consumer-vars.env
nawaphonOHM Mar 10, 2024
f848dfc
Update producer and consumer images in compose.yaml
nawaphonOHM Mar 10, 2024
ee65636
Add ObjectMapper to FakeDatabaseComponent
nawaphonOHM Mar 10, 2024
133d25b
Improve logging in FakeDatabaseComponent with serialization
nawaphonOHM Mar 10, 2024
1fd4cf7
Update 'FRIEND_IP' variable in consumer environment
nawaphonOHM Mar 10, 2024
e20cea4
Add default logging level to application properties
nawaphonOHM Mar 10, 2024
9f858d6
Fix customer UUID comparison in MainController
nawaphonOHM Mar 10, 2024
2829106
Update friend service IP port in consumer vars file
nawaphonOHM Mar 10, 2024
dac3d55
Add CustomerDetailsParameterizedTypeReference class
nawaphonOHM Mar 10, 2024
dddbfa5
Add CustomerParameterizedTypeReference class
nawaphonOHM Mar 10, 2024
af86d4e
Improve API call in MainController with ParameterizedTypeReference
nawaphonOHM Mar 10, 2024
5c94551
Add constructor to CustomerDetailsParameterizedTypeReference class
nawaphonOHM Mar 10, 2024
0adc00a
Add constructor to CustomerParameterizedTypeReference class
nawaphonOHM Mar 10, 2024
a722280
Add ObjectMapper to MainController constructor
nawaphonOHM Mar 10, 2024
83a833a
Refactor getCustomer method in MainController
nawaphonOHM Mar 10, 2024
1790e60
Import Jackson libraries into MainController
nawaphonOHM Mar 10, 2024
96b495c
Update FRIEND_IP in consumer-vars.env
nawaphonOHM Mar 10, 2024
54c7cfc
Add LOG_LEVEL to consumer-vars.env
nawaphonOHM Mar 10, 2024
4376fb4
Replace @Controller with @RestController in MainController
nawaphonOHM Mar 10, 2024
ffcbf91
Update annotation in MainController from @Controller to @RestController
nawaphonOHM Mar 10, 2024
b1d12c4
Update Dockerfile paths in Docker build action
nawaphonOHM Mar 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading