How haomiao000/dy works
Project Type and Purpose
• The project is clearly a backend microservices system built using Go, with a focus on different business capabilities such as user management, video handling, interactions (comments, favorites), and social relations.
• The presence of directories like “server/base_serv” (with submodules like interact, relation, user, video), “server/gateway_serv”, and “server/redis_svr” indicates a microservices setup where a gateway routes API requests to dedicated service modules.
• The README’s architecture design image (although external) and the mention of multiple “接口文档” (API documentation), “工具类文档” (tool documentation), etc., further confirm that this is a distributed system built with modularity and clear component separation in mind.
Analyzing the File Structure
• Key directories and components:
• “comm” directory: Contains shared modules for discovery (likely service discovery), Redis interactions, and distributed tracing.
• “internal” directory: Houses gRPC-generated code and interceptor logic which are vital for implementing RPC communication and middleware functions.
• “server” directory: Divided into different service modules:
▪ “base_serv”: Contains multiple subfolders for core business logic like interact, relation, user, and video. Each has its own API server, configuration, DAO (data access), initialization scripts, and gRPC definition files.
▪ “gateway_serv”: Likely acts as the API gateway, handling the routing, authentication (using middleware like JWT), and aggregating responses from underlying services.
▪ “redis_svr”: A dedicated Redis service providing caching or other Redis-based operations.
• “scripts” & “build”: Contain Dockerfiles and utility scripts (like proto generation, db operations, etc.) for building and deploying the system.
• “assets”: May hold static content used (like videos), possibly supporting a minor frontend or media service.
• Configuration files, build scripts, and deployment files (e.g., “dockerfile” under build/deploy, “grpc.yaml” files) indicate that the system is containerized and uses protocol definitions for interservice communication.
a. Main Components:
• API Gateway: Represented by “server/gateway_serv”. This acts as the single entry point for client requests.
• Service Modules (“base_serv”): Break these into four main business services:
• Interact Service (handles comments, favorites, etc.)
• Relation Service (manages following/follower relationships)
• User Service (user registration, management, login, etc.)
• Video Service (video upload, retrieval, and management)
• Redis Service: Represented by “server/redis_svr”, used either for caching or as a fast in-memory data store.
• Common/Shared Libraries: “comm” and “server/common” which provide cross-cutting concerns like service discovery (possibly backed by etcd as seen in configuration files), distributed tracing (with Jaeger integration), and authentication middleware (JWT).
• gRPC & Protocol Modules: “internal/grpc_gen” and “scripts/proto” illustrate how RPC communications are structured between services.
• Infrastructure & Operations: Deployment scripts, Docker configurations, and build scripts show the DevOps aspect of the system.
b. Relationships and Interactions:
• The API Gateway receives external API requests. It then routes these request using business logic either directly or via RPC calls to the respective base_serv modules.
• The base_serv modules (user, video, interact, relation) can interact with one another via gRPC, as indicated by the generated gRPC code and the “grpc.yaml” files.
• Common middleware (from “server/common” and “internal/interceptor”) is applied for logging, authentication, and tracing (Jaeger and JWT).
• Shared components (like Redis in “server/redis_svr”) may be called by multiple services to cache data or manage sessions.
• The “comm” modules provide auxiliary functionalities such as service discovery (ensuring dynamic routing and addressing), trace logging, and Redis connectivity across services.
c. Architectural Patterns and Technologies:
• Microservices Architecture: The separation of services (user, video, interact, relation, gateway) indicates independent deployment, scalability, and intercommunication.
• RPC Communication: Extensive use of gRPC for service-to-service calls.
• Middleware Pattern: Use of interceptors for cross-cutting concerns.
• Containerization: Docker-related files indicate that the system is containerized for deployment.
• Use of configuration management (etcd configurations) for dynamic service discovery or configuration updates.
• Technologies: Go for implementation, MySQL (as seen in “dao/mysql.go”) for data persistence, Redis for caching, Jaeger for distributed tracing, and etcd for service discovery/configuration.
• For a microservices-based backend, focus on clearly delineating service boundaries between the API Gateway, business modules, and supporting services like Redis.
▪ Another for business service modules (Interact, Relation, User, Video).
▪ A third for supporting/common utilities (discovery, tracing, middleware).
Elements
• Clear Labels: Each component should have a name (e.g., “API Gateway”, “User Service”, “Relational Service”, “Redis Service”, “Discovery Module”, “Tracing Module”).
• Entry point components (API Gateway)
• Business logic modules (microservices under base_serv)
• Infrastructure services (Redis, common libraries, service discovery, tracing)
• Utility or support sections (build, scripts, test)
• A bidirectional flow between the API Gateway and the servers using gRPC.
• Outside these, include a “Common Services” block representing “comm” and “server/common” that interact with almost every service (for tasks such as service discovery and distributed tracing).
• A “Deployment & DevOps” section to denote Docker build/execution pipelines and overall container orchestration.
• Finally, overlay middleware functions as a layer that spans API Gateway and inter-service communication, annotating that they incorporate JWT, Jaeger, etc.