How does microservice architecture work in Java?

 Microservices architecture is a software architecture style that splits an application into a set of small, independently deployed services, each with its own data storage and business logic, and can communicate with each other over the network. Java is one of the programming languages ​​commonly used to develop microservices.

  Next, I will explain in detail how to create a simple Java microservice and provide specific code demonstrations.

  We will create two microservices: one is a user service, responsible for managing user information; the other is an order service, responsible for managing order information. The two services will be interconnected via HTTP communication.

  1. User service

  1. Create a Spring Boot project for user service

  First, we need to create a user service project using Spring Boot. You can use Spring Initializer (https://start.spring.io/) to generate project infrastructure.

  2. Create user entity class

  Create a user entity class to represent user information:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String email;
    // Getters and setters
}

  3. Create user controller

  Create a controller class to handle user-related HTTP requests:

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userRepository.findById(id).orElse(null);
    }
    
    @PostMapping("/")
    public User createUser(@RequestBody User user) {
        return userRepository.save(user);
    }
    
    // Other CRUD operations
}

  4. Create a user repository

  Create a user repository interface for interacting with the database:

public interface UserRepository extends JpaRepository<User, Long> {
}

  2. Order service

  1. Create a Spring Boot project for order service

  Similarly, create an order service project using Spring Initializer.

1695693785587_How does the microservice architecture work in Java.jpg

  2. Create order entity class

  Create an order entity class to represent order information. Here's a simple example:

@Entity
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Long userId;
    private BigDecimal amount;
    // Getters and setters
}

  3. Create an order controller

  Create a controller class to handle order-related HTTP requests:

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderRepository orderRepository;

    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable Long id) {
        return orderRepository.findById(id).orElse(null);
    }
    
    @PostMapping("/")
    public Order createOrder(@RequestBody Order order) {
        return orderRepository.save(order);
    }
    
    // Other CRUD operations
}

  4. Create an order repository

  Create an order repository interface for interacting with the database:

public interface OrderRepository extends JpaRepository<Order, Long> {
}

  Microservice communication

  In order for these two microservices to communicate with each other, we can use HTTP REST API. In the order service, we can use tools such as Feign or RestTemplate to call the API of the user service.

  Call the user service using Feign):

@FeignClient(name = "user-service")
public interface UserServiceClient {
    @GetMapping("/users/{id}")
    User getUserById(@PathVariable Long id);
}

  Then, in the business logic of the order service, we can inject and use UserServiceClient to call the API of the user service.

  This is just a very simple example. The actual microservice architecture may involve more complex issues, such as service registration and discovery, load balancing, security, etc. But hopefully this example helps us understand how to create and interconnect microservices in Java.

Guess you like

Origin blog.csdn.net/Blue92120/article/details/133298793