Light Reading Cloud Documentation

repository·master·Indexed 23 days ago

https://github.com/zealon159/light-reading-cloud

A microservices-based book reading application implementing the Spring Cloud ecosystem. The project demonstrates service discovery and configuration via Nacos, gateway routing with Spring Cloud Gateway, and inter-service communication using OpenFeign. Key technical implementations include JWT-based authentication, ElasticSearch search synchronization via RabbitMQ, and Redis-backed chapter navigation using a doubly linked list structure.

Tokens
2.6K
Snippets
1
Records
14
Agent score
81%

What's inside Light Reading Cloud

  1. Overview of Light Reading Cloud

    master

    Light Reading Cloud (轻松阅读) is a microservices-based book reading application built using the Spring Cloud ecosystem. It serves as a practical implementation of microservices architecture, utilizing technologies such as Spring Cloud Gateway, Nacos, Hystrix, OpenFeign, JWT, and ElasticSearch.

    The project is designed to demonstrate how to implement microservices in real-world business scenarios. The client-side is developed using Vue.js and Vuetify.

  2. Use the feign-client module for inter-service communication

    master
    The reading-cloud-feign-client project contains Feign client definitions designed to be imported as dependencies by other microservices within the light-reading-cloud ecosystem. Instead of manually defining HTTP clients for every service call, you can include this module to use pre-defined Feign interfaces for seamless inter-service communication.
  3. Understand the project module structure

    master

    The project is divided into two main types of modules:

    1. Base Services (基础服务): Infrastructure components including the Configuration Center, Service Registry, and Service Gateway.
    2. Business Services (业务服务): Core application logic including the Book Center (reading-cloud-book), Account Center (reading-cloud-account), and Homepage Center (reading-cloud-homepage).

    Additionally, reading-cloud-common is a shared module containing POJOs, constants, and utility classes used as a standalone JAR dependency across other services to prevent code redundancy.

  4. Use Alibaba-Nacos for Configuration and Service Discovery

    master

    The project uses Alibaba-Nacos to handle two critical infrastructure roles:

    • Configuration Center: Provides centralized management of configuration files. Instead of maintaining multiple .yml or .properties files across different nodes, all services fetch their configurations from Nacos. This simplifies deployment and reduces errors when scaling service nodes.
    • Service Registry: Acts as a central repository for service information. When a service provider starts, it registers its address and port with Nacos. When a consumer needs to call a service, it retrieves the provider's address from Nacos rather than using a hardcoded URL. Nacos also handles health checks and cache updates.
  5. Understand the Light Reading Cloud Architecture

    master

    The system follows a microservices architecture where:

    • Traffic Entry: All client requests are received and responded to by a unified entry point, SpringCloud-Gateway.
    • Communication: The gateway and microservices communicate via asynchronous IO using Netty.
    • Service Discovery & Configuration: Microservices register themselves and discover other services via Nacos after retrieving configurations.
    • Inter-service Communication: Microservices call each other using HTTP-based FeignClient clients.
  6. Understand the role of the reading-cloud-common module

    master

    The reading-cloud-common module is a shared JAR package used across the system. It is designed to centralize reusable components to ensure consistency and reduce duplication. It primarily contains:

    • Entity Classes (POJOs): Shared data models used by multiple services.
    • Utility Classes: Common helper methods and tools.
    • RedisService: A centralized service for Redis operations, defined in common to provide a unified interface for all microservices in the ecosystem.
  7. Implement Search using ElasticSearch and RabbitMQ

    master

    Search functionality is implemented using ElasticSearch (v6.3.1) with the Jest client. While logically a separate service, it is currently hosted within the reading-cloud-homepage project.

    Data Synchronization Strategies

    To keep the ElasticSearch index in sync with the primary database, two methods are discussed:

    1. Scheduled Tasks (Cron): A traditional approach using incremental sync scripts. It is simpler but can be resource-intensive and results in delayed data updates.
    2. MQ-based Synchronization (Recommended): Uses RabbitMQ for near real-time updates.
      • When a write operation occurs on a book (e.g., via an admin tool), a message is sent to a RabbitMQ exchange.
      • A consumer service listens to the queue and immediately updates the ElasticSearch index.
      • This approach provides better decoupling and lower latency.
  8. Implement Security and Authentication with JWT

    master

    The reading-cloud-account service handles user registration, login, and bookshelves. Security is implemented using JWT (JSON Web Token).

    Authentication Workflow

    1. Login: The user provides credentials to the Account Center. Upon successful verification, the server returns a signed JWT.
    2. Client Storage: The client stores the token (e.g., in local storage).
    3. Request: The client includes the token in the HTTP Authorization header for subsequent requests.
    4. Gateway Validation: The Spring Cloud Gateway intercepts the request and validates the token using AuthFilter.
    5. Context Propagation: If valid, the Gateway parses the user information from the token and passes it to the downstream microservices. This ensures that internal services do not need to re-parse or re-validate the token.

    Best Practices

    • Internal Trust: Since the Gateway is the single entry point and performs authentication, internal microservice-to-microservice calls (via Feign) do not require repeated authentication.
    • Performance: To reduce CPU load caused by cryptographic verification, consider caching validated tokens in a cache (like Redis) to avoid repeated decryption/verification for every request.
  9. Implement chapter reading in the Book Center

    master

    The reading-cloud-book service provides book details, chapters, and reading interfaces.

    Chapter Reading Logic

    To optimize performance, chapter navigation (previous/next chapter) is implemented using a doubly linked list structure stored in Redis (Hash type). This avoids expensive database calculations for every page turn.

    Data Structure Model: Each entry in the Redis Hash uses the chapter ID as the key. The value contains the chapter details and pointers to the previous (pre) and next (next) chapters.

    [
        {
            "key":"519",
            "value":{
                "id":529,
                "name":"第一章 装B的乞丐",
                "pre":null,
                "next":[
                    {
                        "id":530,
                        "name":"第二章 资格"
                    }
                ]
            }
        },
        {
            "key":"530",
            "value":{
                "id":530,
                "name":"第二章 资格",
                "pre":[
                    {
                        "id":529,
                        "name":"第一章 装B的乞丐"
                    }
                ],
                "next":[
                    {
                        "id":531,
                        "name":"第三章 开始修炼清心诀"
                    }
                ]
            }
        }
    ]

    Workflow:

    1. Request chapter via book/chapter/readChapter.
    2. Check Redis cache for the chapter node data.
    3. If cache miss: Query the database, calculate the full linked list for the book, and store the result in the Redis Hash.
    4. If cache hit: Return the data directly from Redis (O(1) complexity).
    [
        {
            "key":"519",
            "value":{
                "id":529,
                "name":"第一章 装B的乞丐",
                "pre":null,
                "next":[
                    {
                        "id":530,
                        "name":"第二章 资格"
                    }
                ]
            }
        }
    ]
  10. Locate bootstrap.yml configuration files for microservices

    master

    The project uses bootstrap.yml files for service configuration. Due to potential Nacos instability, these configuration files are stored in the bootstrap-config directory. Each microservice has a corresponding YAML file that should be used for its specific configuration.

    ProjectConfiguration File
    reading-cloud-accountreading-cloud-account.yml
    reading-cloud-bookreading-cloud-book.yml
    reading-cloud-gatewayreading-cloud-gateway.yml
    reading-cloud-homepagereading-cloud-homepage.yml
  11. Quick Start: Setup and Installation

    master

    To run the Light Reading Cloud project locally, follow these steps:

    1. Create Databases

    Import the provided database scripts to create the following databases and run the table creation scripts:

    • reading_cloud_resource
    • reading_cloud_account

    2. Configure the Project

    Because the hosted version uses a configuration center, you may need to manually update the bootstrap.yml file in each project module to match your local environment. Update settings for:

    • Database connection information
    • Redis configuration

    3. Start the Services

    Start the services in the following order to ensure proper registration and discovery:

    1. Nacos (Registration/Configuration Center)
    2. reading-cloud-book (Book Center)
    3. reading-cloud-account (Account Center)
    4. reading-cloud-homepage (Homepage Center)
    5. reading-cloud-gateway (Service Gateway)