one line of code at a time

Microservices with Spring Boot (Gateway, Discovery, Config server) 본문

개발공부

Microservices with Spring Boot (Gateway, Discovery, Config server)

oloc 2024. 8. 11. 05:51

Microservice Architecture is a software development approach that structures an application as a collection of loosely coupled small and independently deployables services.

 

Each service is designed to perform a specific function and communicates with other serivces through well-defined APIs or message broker.

 

This architecture style promotes modularity, flexibility, and scalability as each service can be developed, deployed, and maintained independently of the others.

 

Advantage

It allows faster release cycle and improved fault tolerance as a failure in one service does not necessarily affect the entire application. It also fosters the use of diverse technologies and programming languages for different services depending on their specific requirements and functionality. 

 

Component

(출처: Microservices tutorial with Spring boot 3 by Bouali Ali)

 

When user send the request, first entry point is API Gateway. It is responsible of intercepting and receiving all the request and redirect them to the correct service.

 

Of course we need micro services. Each microservice is communicating with database running on docker. 

 

Trace all the request of your application using Zipkin.

 

Eureka (Discovery server) and Config (configuration) server. When microservice starts, it needs to register itself to discovery server. 


Let's dive right into the project!

 

1. API Gateway
2. Discovery Server (Eureka)
3. Config Server
4. Microservices
5. Integrate Zipkin

 

https://start.spring.io/

 

1. API Gateway 에서 필요한 dependencies: Gateway, Eureka Discovery Client, Config Client, Spring Boot Actuator

2. Discovery Server: Eureka Server, Config Client, Spring Boot Actuator

3. Config Server: Config Server

4. Student Microservice: Lombok, PostgreSQL Driver, Spring Data JPA, Spring Web, Config Client, Eureka Discovery Client, Spring Boot Actuator

(School Microservice has the same dependencies)


Spring Eureka: http://localhost:8761/

 

discovery application을 실행시키고, student application 도 실행시키면 이렇게 뜬다.

 

schools 도 실행시키면 2개의 microservice가 뜨는 걸 볼 수 있다.

 

 

Communication between microservices

students와 schools 마이크로서비스 간 통신을 하기 위해 Open Feign이라는 패키지를 gradle에 추가해준다.

 implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

 

처음에는 마이크로서비스 간에 direct로 Feign Client를 활용해서 데이터를 주고 받는데

나중에는 코드를 수정해서 Gateway를 통해서 통신을 할 수 있도록 구현했다.

 

Zipkin

zipkin을 통해서 로그를 trace 할 수 있다.

 

참고