-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from nawaphonOHM/event-sourcing/main
use restTemplate
- Loading branch information
Showing
18 changed files
with
378 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...awaphon/microservices/event_sourcing/consumer/configurations/ConnectionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...aphon/microservices/event_sourcing/consumer/configurations/RestTemplateConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
60 changes: 58 additions & 2 deletions
60
.../main/java/nawaphon/microservices/event_sourcing/consumer/controllers/MainController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../consumer/src/main/java/nawaphon/microservices/event_sourcing/consumer/pojo/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...mer/src/main/java/nawaphon/microservices/event_sourcing/consumer/pojo/CustomerDetail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...er/src/main/java/nawaphon/microservices/event_sourcing/consumer/pojo/ResponseMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...icroservices/event_sourcing/consumer/utils/CustomerDetailsParameterizedTypeReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...aphon/microservices/event_sourcing/consumer/utils/CustomerParameterizedTypeReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...java/nawaphon/microservices/event_sourcing/producer/components/FakeDatabaseComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.