Spring Boot, a popular Java-based framework, simplifies building microservices by providing a robust platform with embedded servers, automatic configuration, and minimal XML. Multiple microservices can be developed, deployed, and scaled independently when built with Spring Boot. Let's explore a practical example of creating multiple microservices using Spring Boot.

First, let's set up our project structure. We'll create three microservices: 'user-service', 'catalog-service', and 'order-service'. Each will have its own database and responsibilities, promoting loose coupling and high cohesion.

Setting up the Project
For this example, we'll use Spring Initializr (https://start.spring.io/) to generate our projects. Choose 'Spring Web' as a dependency for all services, and add 'Spring Data JPA' and the appropriate database driver (e.g., 'MySQL Driver') based on your preferred database.

Configure your IDE to manage multiple modules, adding the generated projects as modules within a parent project. This will allow easy dependency management between services.
Application Configuration

In each service's 'application.properties' file, configure the database connection and other necessary properties. You can use Placeholders like ${database.url} to ensure consistency across environments. Also, configure the server port to avoid conflicts (e.g., 'order-service.port=8082').
```properties spring.datasource.url=jdbc:mysql://localhost(user-service)/user_db spring.datasource.username=user_service_user spring.datasource.password=(user-service-password) spring.jpa.hibernate.ddl-auto=update order-service.port=8082 ```
Domain and DTO Modeling

Define your domain models (e.g., User, Product, Order) in each service's core package. Create DTOs (Data Transfer Objects) for communication between services using tools like MapStruct or dozer for object mapping. This promotes encapsulation and ensures that services communicate using well-defined, independent objects.
For instance, in 'user-service', create a 'UserDTO' with fields like id, name, email, and password:
```java public class UserDTO { private Long id; private String name; private String email; private String password; // Getters and setters } ```

Implementing the Services
Next, create RESTful controllers and repositories for each domain model. Use annotations like @RestController and @Repository to declare them as such. Implement the necessary CRUD operations in each service.









In 'order-service', you might have an 'OrderController' to handle order-related operations:
```java
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderRepository orderRepository;
@GetMapping("/{id}")
public ResponseEntity
Inter-service Communication
For communication between services, use RESTful APIs or gRPC. For this example, we'll use REST. In 'order-service', you might need data from 'user-service' or 'catalog-service'. Use the @FeignClient annotation to create a Feign client for each dependent service.
```java @FeignClient(name = "user-service", url = "${user-service.url}") public interface UserClient { @GetMapping("/user/{id}") UserDTO getUser(@PathVariable Long id); } ```
Microservices Discovery and Load Balancing
To enable service discovery and load balancing, add 'Spring Cloud Netflix Eureka' as a dependency to all services. Configure EurekaServer in one of the services or use a dedicated EurekaServer. In each service, configure the 'eureka.client.serviceUrl.defaultZone' property to point to the EurekaServer.
With this setup, services can register and discover each other dynamically, enabling load balancing and fault tolerance.
Deployment
Build and package each microservice as a standalone JAR file. Deploy them to your preferred environment, ensuring each service is accessible via its network address and port.
Consider using containers (e.g., Docker) and orchestration tools (e.g., Kubernetes) for consistent deployment across environments and easier service management.
With these steps, you've created a practical example of multiple microservices using Spring Boot. Each service is independent, with its own database and responsibilities, yet communicates seamlessly with others when needed. To explore further, consider adding security, monitoring, and centralized logging across your microservices.