परिचय: माइक्रोसर्विसेज़ के लिए Swift क्यों?
परंपरागत रूप से Swift को iOS और macOS डेवलपमेंट से जोड़ा जाता है, लेकिन Vapor और Kitura जैसे सर्वर फ्रेमवर्क के आगमन के साथ, यह भाषा माइक्रोसर्विस आर्किटेक्चर बनाने के लिए एक शक्तिशाली उपकरण बन गई है। Swift पर माइक्रोसर्विसेज़ क्लाइंट और सर्वर दोनों पर एक ही भाषा का उपयोग करने की अनुमति देती हैं, जो कोडबेस के रखरखाव को सरल बनाती है और डेवलपमेंट को गति देती है। इस लेख में, हम Swift पर माइक्रोसर्विसेज़ बनाने के प्रमुख सिद्धांतों का विश्लेषण करेंगे, Vapor, Combine और gRPC के साथ व्यावहारिक उदाहरण देखेंगे, और डिप्लॉयमेंट के मुद्दों पर चर्चा करेंगे।
मुख्य भाग
1. Swift पर माइक्रोसर्विस आर्किटेक्चर: मूल सिद्धांत
माइक्रोसर्विस आर्किटेक्चर में एप्लिकेशन को छोटी, शिथिल रूप से जुड़ी हुई सेवाओं में विभाजित किया जाता है। प्रत्येक सेवा अपने स्वयं के बिजनेस लॉजिक के लिए जिम्मेदार होती है और इसे स्वतंत्र रूप से डेवलप, डिप्लॉय और स्केल किया जा सकता है। Swift के लिए इसका अर्थ है:
- डोमेन द्वारा विभाजन (DDD): प्रत्येक माइक्रोसर्विस एक अलग bounded context का प्रतिनिधित्व करती है।
- स्वतंत्र डेटाबेस: प्रत्येक सेवा अपने स्वयं के DB (PostgreSQL, SQLite, Redis) का उपयोग करती है।
- इवेंट-संचालित संचार: क्यू (RabbitMQ, Kafka) या HTTP/gRPC के माध्यम से।
Vapor पर प्रोजेक्ट की मूल संरचना का उदाहरण:
// माइक्रोसर्विस "UserService" की फ़ोल्डर संरचना
UserService/
├── Sources/
│ ├── App/
│ │ ├── Controllers/
│ │ ├── Models/
│ │ ├── Migrations/
│ │ └── configure.swift
├── Tests/
├── Package.swift
└── Dockerfile
प्रत्येक माइक्रोसर्विस का अपना Package.swift और एंट्री पॉइंट main.swift होना चाहिए।
2. REST API के साथ Vapor पर माइक्रोसर्विस बनाना
Vapor Swift के लिए सबसे लोकप्रिय सर्वर फ्रेमवर्क है। आइए उपयोगकर्ताओं को प्रबंधित करने के लिए एक सरल माइक्रोसर्विस बनाने पर विचार करें।
2.1 प्रोजेक्ट सेटअप
Vapor Toolbox इंस्टॉल करें और एक नया प्रोजेक्ट बनाएं:
brew install vapor
vapor new UserService --template=api
2.2 मॉडल और कंट्रोलर
Fluent ORM के साथ User मॉडल को परिभाषित करें:
import Fluent
import Vapor
final class User: Model, Content {
static let schema = "users"
@ID(key: .id)
var id: UUID?
@Field(key: "name")
var name: String
@Field(key: "email")
var email: String
init() { }
init(id: UUID? = nil, name: String, email: String) {
self.id = id
self.name = name
self.email = email
}
}
CRUD ऑपरेशंस के साथ एक कंट्रोलर बनाएं:
struct UserController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let users = routes.grouped("users")
users.get(use: index)
users.post(use: create)
users.group(":userID") { user in
user.get(use: show)
user.put(use: update)
user.delete(use: delete)
}
}
// GET /users
func index(req: Request) async throws -> [User] {
try await User.query(on: req.db).all()
}
// POST /users
func create(req: Request) async throws -> User {
let user = try req.content.decode(User.self)
try await user.save(on: req.db)
return user
}
// GET /users/:id
func show(req: Request) async throws -> User {
guard let user = try await User.find(req.parameters.get("userID"), on: req.db) else {
throw Abort(.notFound)
}
return user
}
// PUT /users/:id
func update(req: Request) async throws -> User {
guard let user = try await User.find(req.parameters.get("userID"), on: req.db) else {
throw Abort(.notFound)
}
let updated = try req.content.decode(User.self)
user.name = updated.name
user.email = updated.email
try await
```