Microservices with Spring Boot, Docker and Kubernetes Part 1

Microservice over Monolithic

Monolithic Architecture

Microservice Architecture

Microservices using Spring Boot

Architectural Diagram

API Gateway

Service Discovery Server

Microservice

Project Setup using Spring Initializr

API Gateway

API Gateway Project Setup

Service Discovery Server

Service Discovery Server Project Setup

Payment, Order and Customer Services

Microservices Project Setup

Coding

Service Discovery Server

@SpringBootApplication
@EnableEurekaServer
public class ServiceDiscoveryServerApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceDiscoveryServerApplication.class, args);
}

}
spring:
application:
name: service-discovery-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone:
http://${eureka.instance.hostname}:${server.port}/eureka/
Intellij Gradle bootRun
./gradlew bootRun
Eureka server started at localhost:8761

API Gateway

spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment-service
uri: lb://payment-service
predicates:
- Path=/api/payment**
- id: order-service
uri: lb://order-service
predicates:
- Path=/api/order**
- id: customer-service
uri: lb://customer-service
predicates:
- Path=/api/customer**
application:
name: api-gateway
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka}
@SpringBootApplication
@EnableDiscoveryClient

public class ApiGatewayApplication {

public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}

@Autowired
private DiscoveryClient discoveryClient;

}

Payment, Order and Customer Services

spring:
application:
name: payment-service
server:
port: 8081
forward-headers-strategy: framework
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka}
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentServiceApplication {

public static void main(String[] args) {
SpringApplication.run(PaymentServiceApplication.class, args);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

REST API Request

@Autowired
private RestTemplate restTemplate;

public Customer getCustomerDetails() {
OrderList orders = restTemplate.getForObject(
"http://order-service/api/order", OrderList.class);
PaymentList payments = restTemplate.getForObject(
"http://payment-service/api/payment", PaymentList.class);
return new Customer(0, orders, payments);
}

An undergraduate Computer Science student at National University of Singapore interested in Backend Development