Heim  >  Artikel  >  Java  >  Erstellen Sie Ihr erstes Microservice-System mit Spring Boot: Ein Leitfaden für Anfänger

Erstellen Sie Ihr erstes Microservice-System mit Spring Boot: Ein Leitfaden für Anfänger

WBOY
WBOYOriginal
2024-08-12 22:47:32727Durchsuche

Building Your First Microservice System with Spring Boot: A Beginners Guide

Einführung

In diesem Leitfaden gehen wir Schritt für Schritt durch die Erstellung eines einfachen, aber umfassenden Microservices-Systems mit Spring Boot. Wir behandeln die Grundlagen von Microservices, richten die erforderliche Umgebung ein und implementieren zwei Microservices: OrderService und InventoryService. Darüber hinaus integrieren wir die Serviceerkennung mithilfe von Eureka und einem API-Gateway, um das Routing zwischen den Services zu verwalten.

Was ist ein Microservice?

Microservices sind ein Softwarearchitekturstil, bei dem eine Anwendung als Sammlung kleiner, unabhängiger Dienste erstellt wird, die zusammenarbeiten. Jeder Dienst ist in sich geschlossen und kommuniziert über klar definierte APIs mit anderen, wodurch das System flexibler, skalierbarer und einfacher zu verwalten ist.

Systemarchitektur

Die Architektur unseres Systems wird aus zwei Microservices bestehen: OrderService und InventoryService. Der OrderService verwendet eine relationale Datenbank (MySQL) zum Speichern von Bestelldetails, während der InventoryService eine NoSQL-Datenbank (MongoDB) zum Verwalten von Bestandsdaten verwendet. Wir werden auch Service Discovery mit Eureka implementieren und ein API-Gateway für die Weiterleitung von Anfragen verwenden.

Projekt-Setup

Bevor wir beginnen, stellen Sie sicher, dass Sie die folgenden Tools installiert haben:

  • IDE: IntelliJ IDEA (bevorzugt) oder Eclipse
  • JDK: Version 17 oder höher
  • Build-Tool: Maven
  • Datenbanken: MySQL und MongoDB

Microservice 1: Bestellservice

Schritt 1: Initialisieren Sie das Projekt

  1. Gehe zu Spring Initializr.
  2. Geben Sie die Projektdetails ein:
    • Projekt: Maven-Projekt
    • Sprache: Java
    • Spring Boot: 2.5.7 (oder eine kompatible Version)
    • Gruppe: com.ordersystem
    • Artefakt: Bestellservice
    • Name: Bestellservice
    • Paketname: com.ordersystem.orderservice
    • Verpackung: Glas
    • Java: 17
  3. Fügen Sie die folgenden Abhängigkeiten hinzu:
    • Frühlingsnetz
    • Spring Data JPA
    • MySQL-Treiber
    • Lombok
  4. Klicken Sie auf Generieren, um das Projekt herunterzuladen. Extrahieren Sie die heruntergeladene ZIP-Datei und öffnen Sie sie in Ihrer IDE.

Schritt 2: Konfigurieren Sie die Anwendung

Öffnen Sie die Datei application.properties in src/main/resources und fügen Sie die folgende Konfiguration hinzu:

spring.datasource.url=jdbc:mysql://localhost:3306/orderservice
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
server.port=8081

Schritt 3: Implementieren Sie das Modell

Erstellen Sie die Order-Entitätsklasse in src/main/java/com/ordersystem/orderservice/model/Order.java:

package com.ordersystem.orderservice.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "orders")
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String product;
    private int quantity;
    private double price;
}

Schritt 4: Erstellen Sie das Repository

Erstellen Sie die OrderRepository-Schnittstelle in src/main/java/com/ordersystem/orderservice/repository/OrderRepository.java:

package com.ordersystem.orderservice.repository;

import com.ordersystem.orderservice.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;

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

Schritt 5: Implementieren Sie den Dienst

Erstellen Sie die OrderService-Klasse in src/main/java/com/ordersystem/orderservice/service/OrderService.java:

package com.ordersystem.orderservice.service;

import com.ordersystem.orderservice.model.Order;
import com.ordersystem.orderservice.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;

    public List<Order> getAllOrders() {
        return orderRepository.findAll();
    }

    public Order getOrderById(Long id) {
        return orderRepository.findById(id).orElse(null);
    }

    public Order createOrder(Order order) {
        return orderRepository.save(order);
    }

    public void deleteOrder(Long id) {
        orderRepository.deleteById(id);
    }
}

Schritt 6: Erstellen Sie den Controller

Erstellen Sie die OrderController-Klasse in src/main/java/com/ordersystem/orderservice/controller/OrderController.java:

package com.ordersystem.orderservice.controller;

import com.ordersystem.orderservice.model.Order;
import com.ordersystem.orderservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping
    public List<Order> getAllOrders() {
        return orderService.getAllOrders();
    }

    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable Long id) {
        return orderService.getOrderById(id);
    }

    @PostMapping
    public Order createOrder(@RequestBody Order order) {
        return orderService.createOrder(order);
    }

    @DeleteMapping("/{id}")
    public void deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
    }
}

Microservice 2: Inventardienst

Schritt 1: Initialisieren Sie das Projekt

  1. Gehe zu Spring Initializr.
  2. Geben Sie die Projektdetails ein:
    • Projekt: Maven-Projekt
    • Sprache: Java
    • Spring Boot: 2.5.7 (oder eine kompatible Version)
    • Gruppe: com.ordersystem
    • Artefakt: Inventarisierungsdienst
    • Name: Inventarisierungsdienst
    • Paketname: com.ordersystem.inventoryservice
    • Verpackung: Glas
    • Java: 17
  3. Fügen Sie die folgenden Abhängigkeiten hinzu:
    • Frühlingsnetz
    • Spring Data MongoDB
    • Lombok
  4. Klicken Sie auf Generieren, um das Projekt herunterzuladen. Extrahieren Sie die heruntergeladene ZIP-Datei und öffnen Sie sie in Ihrer IDE.

Schritt 2: Konfigurieren Sie die Anwendung

Öffnen Sie die Datei application.properties in src/main/resources und fügen Sie die folgende Konfiguration hinzu:

spring.data.mongodb.uri=mongodb://localhost:27017/inventoryservice
server.port=8082

Schritt 3: Implementieren Sie das Modell

Erstellen Sie die InventoryItem-Entitätsklasse in src/main/java/com/ordersystem/inventoryservice/model/InventoryItem.java:

package com.ordersystem.inventoryservice.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "inventory")
public class InventoryItem {
    @Id
    private String id;
    private String product;
    private int quantity;
}

Schritt 4: Erstellen Sie das Repository

Erstellen Sie die InventoryRepository-Schnittstelle in src/main/java/com/ordersystem/inventoryservice/repository/InventoryRepository.java:

package com.ordersystem.inventoryservice.repository;

import com.ordersystem.inventoryservice.model.InventoryItem;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface InventoryRepository extends MongoRepository<InventoryItem, String> {
}

Step 5: Implement the Service

Create the InventoryService class in src/main/java/com/ordersystem/inventoryservice/service/InventoryService.java:

package com.ordersystem.inventoryservice.service;

import com.ordersystem.inventoryservice.model.InventoryItem;
import com.ordersystem.inventoryservice.repository.InventoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InventoryService {
    @Autowired
    private InventoryRepository inventoryRepository;

    public List<InventoryItem> getAllItems() {
        return inventoryRepository.findAll();
    }

    public InventoryItem getItemById(String id) {
        return inventoryRepository.findById(id).orElse(null);
    }

    public InventoryItem createItem(InventoryItem item) {
        return inventoryRepository.save(item);
    }

    public void deleteItem(String id) {
        inventoryRepository.deleteById(id);
    }
}

Step 6: Create the Controller

Create the InventoryController class in src/main/java/com/ordersystem/inventoryservice/controller/InventoryController.java:

package com.ordersystem.inventoryservice.controller;

import com.ordersystem.inventoryservice.model.InventoryItem;
import com.ordersystem.inventoryservice.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/inventory")
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    @GetMapping
    public List<InventoryItem> getAllItems() {
        return inventoryService.getAllItems();
    }

    @GetMapping("/{id}")
    public InventoryItem getItemById(@PathVariable String id) {
        return inventoryService.getItemById(id);
    }

    @PostMapping
    public InventoryItem createItem(@RequestBody InventoryItem item) {
        return inventoryService.createItem(item);
    }

    @DeleteMapping("/{id}")
    public void deleteItem(@PathVariable String id) {
        inventoryService.delete

Item(id);
    }
}

Service Discovery with Eureka

Step 1: Initialize the Eureka Server

  1. Go to Spring Initializr.
  2. Fill in the project details:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 2.5.7 (or a compatible version)
    • Group: com.ordersystem
    • Artifact: eureka-server
    • Name: eureka-server
    • Package name: com.ordersystem.eurekaserver
    • Packaging: Jar
    • Java: 17
  3. Add the Eureka Server dependency.
  4. Click Generate to download the project. Extract the downloaded zip file and open it in your IDE.

Step 2: Configure the Eureka Server

Open the application.properties file in src/main/resources and add the following configuration:

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Step 3: Enable Eureka Server

Annotate the main application class in src/main/java/com/ordersystem/eurekaserver/EurekaServerApplication.java with @EnableEurekaServer:

package com.ordersystem.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Integrate Services with Eureka

Add the Eureka client dependency to both OrderService and InventoryService:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Add Eureka client configuration to the application.properties files:

Order Service:

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name=order-service

Inventory Service:

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name=inventory-service

API Gateway

Step 1: Initialize the API Gateway

  1. Go to Spring Initializr.
  2. Fill in the project details:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 2.5.7 (or a compatible version)
    • Group: com.ordersystem
    • Artifact: api-gateway
    • Name: api-gateway
    • Package name: com.ordersystem.apigateway
    • Packaging: Jar
    • Java: 17
  3. Add the Gateway and Eureka Discovery Client dependencies.
  4. Click Generate to download the project. Extract the downloaded zip file and open it in your IDE.

Step 2: Configure the API Gateway

Open the application.yml file in src/main/resources and add the following configuration:

server:
  port: 8080

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: order-service
          uri: lb://order-service
          predicates:
            - Path=/api/orders/**
        - id: inventory-service
          uri: lb://inventory-service
          predicates:
            - Path=/api/inventory/**

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

Step 3: Enable Discovery Client

Annotate the main application class in src/main/java/com/ordersystem/apigateway/ApiGatewayApplication.java with @EnableDiscoveryClient:

package com.ordersystem.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

Testing the Microservices

  1. Start Eureka Server: Run the Eureka Server application.
  2. Start Order Service: Run the Order Service application.
  3. Start Inventory Service: Run the Inventory Service application.
  4. Start API Gateway: Run the API Gateway application.

Use Postman or any other API client to test the endpoints through the API Gateway:

  • Create Order: POST http://localhost:8080/api/orders
  • Get Orders: GET http://localhost:8080/api/orders
  • Create Inventory Item: POST http://localhost:8080/api/inventory
  • Get Inventory Items: GET http://localhost:8080/api/inventory

Conclusion

In this guide, we've built a simple microservices system using Spring Boot. We created two microservices (OrderService and InventoryService), integrated service discovery with Eureka, and set up an API Gateway for routing requests. This architecture allows for scalable and maintainable microservices that can be easily extended in the future.

Das obige ist der detaillierte Inhalt vonErstellen Sie Ihr erstes Microservice-System mit Spring Boot: Ein Leitfaden für Anfänger. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn