Similar presentations:
CarrotFarmer_Presentation
1. Carrot Farmer
MICROSERVICES ARCHITECTURE WITH UNITY INTEGRATIONEDUCATIONAL PROJECT - ASP.NET CORE
2. Project Overview
3 microservices on ASP.NET Core (.NET 8/9)API Gateway based on YARP
Web application (ASP.NET MVC + Bootstrap 5)
Unity game with backend integration
Async messaging via Kafka
gRPC for inter-service communication
JWT + Refresh Tokens authentication
3. Onion Architecture
Each microservice is divided into 4 layers:API Layer (Controllers, Middleware)
|
Infrastructure Layer (Repositories, DbContext, gRPC, Kafka)
|
Application Layer (Services, DTOs, Validators)
|
Domain Layer (Entities, Interfaces)
4. System Architecture
CLIENTSUnity Game
Web MVC (7222)
\
/
API Gateway (YARP:7055)
/
|
\
Auth:7273
Blog:7011
Statistics:7083
|
|
|
SQL:1433
SQL:1434
SQL:1435
Auth <--gRPC-- Blog
Auth <--gRPC-- Statistics
Auth <--Kafka-- Blog (notifications)
5. System Scheme
6. Microservices
Auth Service (port 7273):Blog Service (port 7011):
- CRUD articles and comments
- gRPC Client -> Auth
- Kafka Producer (notifications)
- JWT + Refresh Tokens
- Users, Roles, Notifications
- gRPC Server, Kafka Consumer
- S3/MinIO for avatars
Statistics Service (port 7083):
- Carrot statistics
- gRPC Client -> Auth
- Game API for Unity
7. HealthCheck
8. Docker Compose
services:auth-db:
image: mcr.microsoft.com/mssql/server:2022-latest
ports: ['1433:1433']
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
blog-db:
image: mcr.microsoft.com/mssql/server:2022-latest
ports: ['1434:1433']
kafka:
image: confluentinc/cp-kafka:7.5.0
ports: ['9092:9092']
statistics-db:
image: mcr.microsoft.com/mssql/server:2022-latest
ports: ['1435:1433']
9. Docker Compose
10. JWT Authentication (code)
// AuthService.csvar tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Name, user.Username),
new Claim(ClaimTypes.Role, 'User')
}),
Expires = DateTime.UtcNow.AddMinutes(60),
Issuer = 'AuthService',
Audience = 'AuthClient',
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(key),
SecurityAlgorithms.HmacSha256Signature)
};
11. gRPC - Inter-service Communication
// user.protoservice UserService {
rpc CheckUserExists (UserExistsRequest)
returns (UserExistsResponse);
rpc GetUser (GetUserRequest)
returns (GetUserResponse);
rpc GetUsers (GetUsersRequest)
returns (GetUsersResponse);
// Batch
}
// Usage (Statistics -> Auth)
var response = await _client.CheckUserExistsAsync(
new UserExistsRequest { UserId = id.ToString() }
);
return response.Exists;
12. Kafka - Async Notifications
Blog Service (Producer):var message = new Message<string, string> {
Key = notification.PostAuthorId.ToString(),
Value = JsonSerializer.Serialize(notification)
};
await _producer.ProduceAsync('notifications', msg);
Auth Service (Consumer - BackgroundService):
consumer.Subscribe('notifications');
while (!stoppingToken.IsCancellationRequested) {
var result = consumer.Consume();
var data = JsonSerializer.Deserialize<...>(result);
await _notificationRepo.AddAsync(data);
}
13. Unity Integration
// FormLogin.cs - AuthenticationIEnumerator SendLoginRequest(string user, string pass) {
string url = GameConfig.AuthEndpoint +
'?login=' + user + '&password=' + pass;
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
var json = JSON.Parse(www.downloadHandler.text);
if (json['success'].AsBool) {
UserController.Instance.EnterOnlineMode(
json['token'], json['userId'], 0);
}
}
14. Summary
Patterns Used:* Microservices Architecture
* Onion Architecture
* API Gateway Pattern (YARP)
* Database per Service
* Event-Driven (Kafka)
* Circuit Breaker + Retry (Polly)
* Offline-First (Unity)
* JWT + Refresh Tokens