Spring Cloud Demo

repository·master·Indexed 43 days ago

https://github.com/chxfantasy/spring-cloud-demo

A demonstration project for Spring Cloud microservice architecture using Spring Boot. It showcases service discovery with Eureka, API gateway routing, and dynamic database selection via AOP. Key technologies include Spring Cloud Config, Ribbon, Feign, Hystrix, Turbine, MyBatis, and Alibaba Druid, featuring a modular structure with dedicated services for accounts, business logic, and gateway routing.

Tokens
1.7K
Snippets
3
Records
5
Agent score
5%

What's inside Spring Cloud Demo

  1. Overview of the Spring Cloud Demo Architecture

    master

    This project is a demonstration of a microservice architecture using Spring Cloud and Spring Boot. It implements several key cloud patterns including service discovery, configuration management, and API gateway routing.

    Key technologies included:

    • Service Discovery & Config: Spring Cloud Eureka and Spring Cloud Config.
    • Resilience & Load Balancing: Ribbon, Feign, Hystrix, and Turbine.
    • Data Layer: Multi-database support with dynamic selection via AOP, MyBatis, PageHelper, and Alibaba Druid connection pooling.
    • Infrastructure: Redis (JDK serialization), SLF4J/Logback, and a Task Executor thread pool.
    • Features: Global traceId, Internationalization, Global Exception handling, and HealthChecks.
  2. Understand the Module Responsibilities

    master

    The project is divided into several specialized modules:

    • spring-cloud-parent: An empty Maven project containing common POM configurations inherited by other modules.
    • spring-cloud-starter: A demo starter containing common beans and configurations used by spring-cloud-biz, spring-cloud-account, and spring-cloud-gateway.
    • spring-cloud-client: A shared dependency containing utility classes and Java Models that map to database tables.
    • spring-cloud-eureka: The server for Service Registration and Service Discovery. It integrates Spring Cloud Config for dynamic configurations. Note: The Eureka heartbeat is configured to 5s (default is 15s).
    • spring-cloud-account: Handles account-related logic.
    • spring-cloud-biz: The core business module containing the moment and comment modules. It utilizes dynamic database selection across two databases.
    • spring-cloud-gateway: The entry point for all external requests. It handles authentication (login status verification), file uploading, and request filtering.
  3. Run the Spring Cloud Demo locally

    master

    Prerequisites

    • Java 8
    • Maven 3

    Installation and Execution Steps

    Run the following commands in order to build the modules and start the services:

    1. Build Modules:

      • Build spring-cloud-parent (common POMs).
      • Build spring-cloud-client (utilities and models).
      • Build spring-cloud-starter (common beans and configurations).
    2. Start Services:

      • Start spring-cloud-eureka (Service Discovery).
      • Start spring-cloud-account (Account module).
      • Start spring-cloud-biz (Business module: moments and comments).
      • Start spring-cloud-gateway (API Gateway).
    3. Access the App: Open http://127.0.0.1:7001/index in your browser.

    # Build phase
    cd spring-cloud-parent
    mvn clean install -DskipTests
    cd ../spring-cloud-client
    mvn clean install -DskipTests
    cd ../spring-cloud-starter
    mvn clean install -DskipTests
    
    # Run phase
    cd spring-cloud-eureka
    mvn clean spring-boot:run
    
    cd spring-cloud-account
    mvn clean spring-boot:run
    
    cd spring-cloud-biz
    mvn clean spring-boot:run
    
    cd spring-cloud-gateway
    mvn clean spring-boot:run
  4. Set up MySQL and Redis for the Demo

    master

    Before running the application, you must have a running instance of Redis and MySQL. Use the following SQL commands to initialize the required databases and tables:

    1. Create two databases: test and test2.
    2. Create the account table in test.
    3. Create the moment table in test.
    4. Create the comment table in test2.
    create database test;
    
    CREATE TABLE `account` (
      `user_id` varchar(127) NOT NULL DEFAULT '',
      `user_name` varchar(127) NOT NULL DEFAULT '',
      `password` varchar(127) NOT NULL DEFAULT '',
      `gmt_created` datetime NOT NULL,
      `gmt_modified` datetime DEFAULT NULL,
      `is_deleted` tinyint(1) NOT NULL DEFAULT '0',
      PRIMARY KEY (`user_id`),
      KEY `index_user_id` (`user_id`) KEY_BLOCK_SIZE=10
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    CREATE TABLE `moment` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `user_id` varchar(127) COLLATE utf8mb4_unicode_ci NOT NULL,
      `content` text COLLATE utf8mb4_unicode_ci,
      `gmt_created` datetime NOT NULL,
      `gmt_modified` datetime DEFAULT NULL,
      `is_deleted` tinyint(1) DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `index_user_id` (`user_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    create database test2;
    
    CREATE TABLE `comment` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `moment_id` bigint(20) unsigned NOT NULL,
      `content` text COLLATE utf8mb4_unicode_ci,
      `gmt_created` datetime NOT NULL,
      `gmt_modified` datetime DEFAULT NULL,
      `is_deleted` tinyint(1) DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `index_moment_id` (`moment_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  5. Shut down a microservice gracefully

    master

    To stop a specific service (spring-cloud-account, spring-cloud-biz, or spring-cloud-gateway) and ensure it stops sending heartbeats to the Eureka cluster, use the following shutdown command.

    Note: Replace localhost:7004 with the actual port of the service you wish to stop.

    curl -H 'Accept:application/json' -X POST localhost:7004/shutdown