Spring Cloud Eureka Tutorial
What is Eureka?
- A service registry provided by Netflix, integrated with the Spring framework.
- Used for deploying microservices.
Use of Eureka
- In microservice architecture, we develop separate services and expose APIs as service endpoints.
- Services communicate internally via REST APIs.
- Avoids tight coupling of clients with individual service URLs.
Microservice Communication Model
- Diagram Explanation:
- Service 1, Service 2, and Service 3 can be accessed by a client using their respective URLs.
- Example:
http://localhost:8081/service1
, http://localhost:8082/service2
, etc.
- Challenge: Clients need to know multiple service URLs, which can be cumbersome.
- Solution: Use a service registry (Eureka) to dynamically resolve service URLs.
How Eureka Works
- Client requests service through the Eureka server instead of directly hitting the service URL.
- Eureka server holds the registry of services and their endpoints.
- Eureka forwards requests to the correct microservice based on service name and URL.
Creating a Eureka Server
- Project Setup:
- Create a Spring Boot project named "Amazon Server Eureka server".
- Add dependencies: Dev Tools, Web, Eureka Server.
- Server Configuration:
- Annotate class with
@EnableEurekaServer
.
- Create a YAML configuration file:
- Set
registerWithEureka: false
- Set
server.port: 8761
- Set
spring.application.name: Amazon Server
- Run Application: Check Eureka dashboard at
http://localhost:8761
.
Developing a Microservice
- Create Payment Service:
- Create another Spring Boot project named "Payment Service".
- Add dependencies: Eureka Discovery, Dev Tools.
- Annotate class with
@EnableEurekaClient
.
- Expose API:
- Create a Controller with a GET method to handle payment requests.
- Use
@RestController
and @GetMapping
for defining the endpoint.
- Register with Eureka:
- Create a YAML configuration file for this payment service:
- Set
eureka.client.registerWithEureka: true
- Set
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
- Specify a unique service name.
Creating the Client Application
- Project Setup:
- Create a Spring Boot project named "Amazon Shopping".
- Add dependencies: Web, Eureka Discovery.
- Annotate class with
@EnableEurekaClient
.
- Invoke Payment Service:
- Create a Controller to handle incoming requests.
- Use
RestTemplate
to call the payment service through Eureka.
- Example of service invocation using the registered service name instead of the actual URL.
- YAML Configuration for Client:
- Set
eureka.client.service-url.defaultZone
and unique service name.
Testing the Implementation
- Postman: Use Postman to send requests to the client service, which in turn accesses the payment service through Eureka.
- Response Verification: Check responses and timings to confirm dynamic routing through Eureka.
Conclusion
- The tutorial demonstrated setting up a Eureka server, registering microservices, and invoking them through a client using Eureka for service discovery.
- Potential for creating more complex use cases and scaling microservices dynamically.
For additional resources, refer to the provided blog and video links. Subscribe for updates and more tutorials.