diff --git a/spring-cloud/spring-cloud-bootstrap/customer-service/pom.xml b/spring-cloud/spring-cloud-bootstrap/customer-service/pom.xml new file mode 100644 index 0000000000..3e33e27c6c --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/customer-service/pom.xml @@ -0,0 +1,84 @@ + + + + 4.0.0 + com.baeldung.customerservice + customer-service + 1.0.0-SNAPSHOT + customer-service + jar + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../../parent-boot-2 + + + + 1.8 + 1.8 + 1.8 + UTF-8 + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-json + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-configuration-processor + + + org.projectlombok + lombok + true + + + com.baeldung.orderservice + order-client + 1.0.0-SNAPSHOT + compile + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + exec + + + + + + + + diff --git a/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/Customer.java b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/Customer.java new file mode 100644 index 0000000000..7d10510cbc --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/Customer.java @@ -0,0 +1,14 @@ +package com.baeldung.customerservice; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Customer { + + private int id; + private String firstName; + private String lastName; + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/CustomerApplication.java b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/CustomerApplication.java new file mode 100644 index 0000000000..489129a3d2 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/CustomerApplication.java @@ -0,0 +1,25 @@ +package com.baeldung.customerservice; + +import com.baeldung.orderservice.client.OrderClient; +import com.baeldung.orderservice.client.OrderClientImpl; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; + +/** + * Spring Boot application starter class + */ +@SpringBootApplication +public class CustomerApplication { + public static void main(String[] args) { + SpringApplication.run(CustomerApplication.class, args); + } + + @Bean + public OrderClient getOrderClient() { + + return new OrderClientImpl(new RestTemplateBuilder()); + } + +} diff --git a/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/CustomerService.java b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/CustomerService.java new file mode 100644 index 0000000000..ab8872de1c --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/java/com/baeldung/customerservice/CustomerService.java @@ -0,0 +1,56 @@ +package com.baeldung.customerservice; + +import com.baeldung.orderservice.client.OrderClient; +import com.baeldung.orderservice.client.OrderDTO; +import com.baeldung.orderservice.client.OrderResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +@RestController +public class CustomerService { + + @Autowired + private OrderClient orderClient; + + private List customers = Arrays.asList( + + new Customer(1, "John", "Smith"), + new Customer(2, "Deny", "Dominic")); + + + @GetMapping + public List getAllCustomers() { + return customers; + } + + @GetMapping("/{id}") + public Customer getCustomerById(@PathVariable int id) { + return customers.stream() + .filter(customer -> customer.getId() == id) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } + + + @PostMapping(value = "/order") + public String sendOrder(@RequestBody Map body) { + + OrderDTO dto = new OrderDTO(); + dto.setCustomerId((Integer) body.get("customerId")); + dto.setItemId((String) body.get("itemId")); + + OrderResponse response = orderClient.order(dto); + + return response.getStatus(); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/resources/application.properties b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/resources/application.properties new file mode 100644 index 0000000000..d439ae9b76 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/customer-service/src/main/resources/application.properties @@ -0,0 +1,4 @@ +#Spring Boot server configuration +server.servlet.context-path=/customer-service +server.port=8001 + diff --git a/spring-cloud/spring-cloud-bootstrap/customer-service/src/test/resources/application.properties b/spring-cloud/spring-cloud-bootstrap/customer-service/src/test/resources/application.properties new file mode 100644 index 0000000000..1d50559005 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/customer-service/src/test/resources/application.properties @@ -0,0 +1,2 @@ +local.server.port=8001 +server.servlet.context-path=/customer-service \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-client/pom.xml b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/pom.xml new file mode 100644 index 0000000000..4c8cf742b1 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + com.baeldung.orderservice + order-client + 1.0.0-SNAPSHOT + order-client + Order service client module + http://projects.spring.io/spring-boot/ + + + com.baeldung.orderservice + order-service + 1.0.0-SNAPSHOT + + + diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderClient.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderClient.java new file mode 100644 index 0000000000..2dd6b13248 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderClient.java @@ -0,0 +1,6 @@ +package com.baeldung.orderservice.client; + +public interface OrderClient { + + OrderResponse order(OrderDTO orderDTO); +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderClientImpl.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderClientImpl.java new file mode 100644 index 0000000000..70ec77fab6 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderClientImpl.java @@ -0,0 +1,35 @@ +package com.baeldung.orderservice.client; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + + +@Component +public class OrderClientImpl implements OrderClient { + + private RestTemplate restTemplate; + + public OrderClientImpl(RestTemplateBuilder builder) { + + this.restTemplate = builder.build(); + } + + @Override + public OrderResponse order(OrderDTO orderDTO) { + + String serviceUrl = "http://localhost:8002/order-service"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity request = new HttpEntity<>(orderDTO, headers); + + OrderResponse orderResponse = restTemplate.postForObject(serviceUrl + "/create", request, OrderResponse.class); + + return orderResponse; + + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderDTO.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderDTO.java new file mode 100644 index 0000000000..c31c9f6bec --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderDTO.java @@ -0,0 +1,16 @@ +package com.baeldung.orderservice.client; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class OrderDTO { + + private int customerId; + private String itemId; + +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderResponse.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderResponse.java new file mode 100644 index 0000000000..e8d2059cbb --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/java/com/baeldung/orderservice/client/OrderResponse.java @@ -0,0 +1,15 @@ +package com.baeldung.orderservice.client; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class OrderResponse { + + private int orderId; + private String productId; + private String status; +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/resources/application.properties b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/resources/application.properties new file mode 100644 index 0000000000..53e503689f --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-client/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8002 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-server/pom.xml b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/pom.xml new file mode 100644 index 0000000000..d6c521c5df --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + com.baeldung.orderservice + order-server + 1.0.0-SNAPSHOT + + order-service + com.baeldung.orderservice + 1.0.0-SNAPSHOT + + + + com.baeldung.orderservice + order-client + 1.0.0-SNAPSHOT + compile + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/Order.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/Order.java new file mode 100644 index 0000000000..719305064f --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/Order.java @@ -0,0 +1,18 @@ +package com.baeldung.orderservice; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Order { + + private Integer id; + private Integer customerId; + private String itemId; + private String date; + +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderApplication.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderApplication.java new file mode 100644 index 0000000000..eb89959ee0 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.orderservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; + +/** + * Spring Boot application starter class + */ +@SpringBootApplication +public class OrderApplication { + public static void main(String[] args) { + SpringApplication.run(OrderApplication.class, args); + } + +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java new file mode 100644 index 0000000000..a43bbcec65 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java @@ -0,0 +1,54 @@ +package com.baeldung.orderservice; + +import com.baeldung.orderservice.client.OrderDTO; +import com.baeldung.orderservice.client.OrderResponse; +import org.apache.commons.lang.time.DateFormatUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + + +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +public class OrderService { + + + private List orders = Arrays.asList( + + new Order(1, 1, "A101", "2020/02/14"), + new Order(2, 1, "A101", "2020/02/14"), + new Order(3, 2, "A150", "2020/02/17")); + + @GetMapping + public List getAllOrders() { + return orders; + } + + @GetMapping("/{id}") + public List getOrdersByCustomer(@PathVariable int id) { + return orders.stream() + .filter(order -> order.getCustomerId() == id).collect(Collectors.toList()); + } + + @PostMapping("/create") + public OrderResponse createOrder(@RequestBody OrderDTO request) { + + int lastIndex = orders.size(); + Order order = new Order(); + order.setId(lastIndex + 1); + order.setCustomerId(request.getCustomerId()); + order.setItemId(request.getItemId()); + String date = DateFormatUtils.format(new Date(), "yyyy/MM/dd"); + order.setDate(date); + + return new OrderResponse(order.getId(), order.getItemId(), "CREATED"); + } + +} diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/resources/application.properties b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/resources/application.properties new file mode 100644 index 0000000000..f4fe5e7079 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/order-server/src/main/resources/application.properties @@ -0,0 +1,4 @@ +#Spring Boot server configuration +server.servlet.context-path=/order-service +server.port=8002 + diff --git a/spring-cloud/spring-cloud-bootstrap/order-service/pom.xml b/spring-cloud/spring-cloud-bootstrap/order-service/pom.xml new file mode 100644 index 0000000000..8f3c9ceaff --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/order-service/pom.xml @@ -0,0 +1,123 @@ + + + + 4.0.0 + com.baeldung.orderservice + order-service + 1.0.0-SNAPSHOT + order-service + pom + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../../parent-boot-2 + + + + order-client + order-server + + + + 1.8 + 2.6 + 0.0.2 + 1.8 + 1.8 + UTF-8 + com.baeldung.orderservice.OrderApplication + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + commons-lang + commons-lang + ${commons-lang.version} + + + org.projectlombok + lombok + true + + + org.qunix + structure-maven-plugin + ${structure-maven.version} + + + org.springframework.boot + spring-boot-starter-json + + + org.springframework.boot + spring-boot-configuration-processor + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + ${orderservice.mainclass} + + + + + repackage + + + exec + + + + start-application + + com.baeldung.orderservice.OrderApplication + ../order-server/target/classes + + + start + + + + + + org.qunix + structure-maven-plugin + ${structure-maven.version} + false + + + compile + + modules + + + + + + + + diff --git a/spring-cloud/spring-cloud-bootstrap/pom.xml b/spring-cloud/spring-cloud-bootstrap/pom.xml index 2447b90538..ed9b148564 100644 --- a/spring-cloud/spring-cloud-bootstrap/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/pom.xml @@ -20,6 +20,9 @@ svc-book svc-rating zipkin + customer-service + order-service + shared-dto \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/shared-dto/pom.xml b/spring-cloud/spring-cloud-bootstrap/shared-dto/pom.xml new file mode 100644 index 0000000000..59dde717b5 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/shared-dto/pom.xml @@ -0,0 +1,60 @@ + + + + + 4.0.0 + com.baeldung + shared-dto + 1.0.0-SNAPSHOT + shared-dto + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../../parent-boot-2 + + + + + UTF-8 + 1.8 + 1.8 + 0.0.2 + + + + + org.qunix + structure-maven-plugin + ${structure-maven.version} + + + org.projectlombok + lombok + true + + + + + + + + org.qunix + structure-maven-plugin + ${structure-maven.version} + false + + + compile + + files + + + + + + + + diff --git a/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/deliverydto/CustomerDTO.java b/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/deliverydto/CustomerDTO.java new file mode 100644 index 0000000000..aa6b3ceac8 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/deliverydto/CustomerDTO.java @@ -0,0 +1,16 @@ +package com.baeldung.deliverydto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CustomerDTO { + private String firstName; + private String lastName; + private String homeAddress; + private String contactNumber; + // constructor, getters, setters +} diff --git a/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/shared/CustomerDTO.java b/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/shared/CustomerDTO.java new file mode 100644 index 0000000000..905773609d --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/shared/CustomerDTO.java @@ -0,0 +1,15 @@ +package com.baeldung.shared; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CustomerDTO { + private String firstName; + private String lastName; + private String cardNumber; + // constructor, getters, setters +} diff --git a/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/shared/OrderDTO.java b/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/shared/OrderDTO.java new file mode 100644 index 0000000000..0640b84291 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/shared-dto/src/main/java/com/baeldung/shared/OrderDTO.java @@ -0,0 +1,16 @@ +package com.baeldung.shared; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class OrderDTO { + + private int customerId; + private String itemId; + +}