在本指南中,我們將逐步介紹如何使用 Spring Boot 創建一個簡單而全面的微服務系統。我們將介紹微服務的基礎知識,設定所需的環境,並實作兩個微服務:OrderService 和 InventoryService。此外,我們將使用 Eureka 和 API 閘道整合服務發現來管理服務之間的路由。
微服務是一種軟體架構風格,其中應用程式被建構為協同工作的小型獨立服務的集合。每個服務都是獨立的,並透過明確定義的 API 與其他服務進行通信,使系統更加靈活、可擴展且更易於管理。
我們系統的架構將由兩個微服務組成:OrderService 和 InventoryService。 OrderService 將使用關聯式資料庫 (MySQL) 來儲存訂單詳細信息,而 InventoryService 將使用 NoSQL 資料庫 (MongoDB) 來管理庫存資料。我們還將使用 Eureka 實作服務發現,並使用 API 閘道來路由請求。
在開始之前,請確保您已安裝以下工具:
開啟 src/main/resources 中的 application.properties 文件,新增以下設定:
在src/main/java/com/ordersystem/orderservice/model/Order.java建立Order實體類別:
在 src/main/java/com/ordersystem/orderservice/repository/OrderRepository.java 中建立 OrderRepository 介面:
在 src/main/java/com/ordersystem/orderservice/service/OrderService.java 中建立 OrderService 類別:
在 src/main/java/com/ordersystem/orderservice/controller/OrderController.java 中建立 OrderController 類別:
開啟 src/main/resources 中的 application.properties 文件,新增以下設定:
在src/main/java/com/ordersystem/inventoryservice/model/InventoryItem.java中建立InventoryItem實體類別:
在 src/main/java/com/ordersystem/inventoryservice/repository/InventoryRepository.java 中建立 InventoryRepository 介面:
package com.ordersystem.inventoryservice.repository; import com.ordersystem.inventoryservice.model.InventoryItem; import org.springframework.data.mongodb.repository.MongoRepository; public interface InventoryRepository extends MongoRepository{ }
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 ListgetAllItems() { 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); } }
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 ListgetAllItems() { 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); } }
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
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); } }
Add the Eureka client dependency to both OrderService and InventoryService:
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
Add Eureka client configuration to the application.properties files:
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ spring.application.name=order-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ spring.application.name=inventory-service
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/
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); } }
Use Postman or any other API client to test the endpoints through the API Gateway:
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.
以上是使用 Spring Boot 建立您的第一個微服務系統:初學者指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!