HelloKoding Practical Coding Guides

repository·master·Indexed 20 days ago

https://github.com/hellokoding/hellokoding-courses

A collection of practical coding guides and educational resources for software engineering. The repository includes Java and Spring Boot examples, data structure and algorithm implementations (such as linked list operations with ListNode), and various Docker Compose configurations for deploying stacks featuring Nginx, MySQL, MongoDB, Jetty, Prometheus, and Grafana.

Tokens
11.8K
Snippets
49
Records
49
Agent score
67%

What's inside HelloKoding Courses

  1. Access HelloKoding Practical Coding Guides

    master

    HelloKoding offers a series of practical coding guides covering topics such as Spring Boot, Java, Algorithms, and other software engineering disciplines. You can access the full collection of guides and resources at the official website.

    https://hellokoding.com
  2. Implement Spring Data Projections for Quote and Author

    master

    Projections allow you to retrieve a subset of data from the database, which is more efficient than loading entire entities. This project uses interface-based projections:

    • AuthorProjection: Defines getId() and getName().
    • QuoteProjection: Defines getId(), getName(), and getAuthor() (which returns an AuthorProjection).

    When using findFirst10ByOrderByNameDesc(), Spring Data JPA will populate these interfaces with the requested data, minimizing the SQL query footprint.

    interface AuthorProjection {
        Integer getId();
        String getName();
    }
    
    interface QuoteProjection {
        Integer getId();
        String getName();
        AuthorProjection getAuthor();
    }
  3. Deploy Spring Boot, MongoDB, and Nginx using Docker Compose

    master

    This Docker Compose configuration sets up a full-stack environment consisting of an Nginx reverse proxy, a Spring Boot application, and a MongoDB database.

    Service Architecture

    • hk-nginx: Acts as the entry point, mapping ports 80 and 443 to the host. It depends on the app service and uses a local volume ./nginx/conf.d to provide Nginx configuration files.
    • hk-mongodb: A MongoDB instance (version 3.6) running on port 27017. It uses environment variables for data and log directories and is configured to run with the --smallfiles flag.
    • app: The Spring Boot application built from the ./app directory. It runs using Maven (mvn clean spring-boot:run), exposes port 8080 internally, and depends on hk-mongodb being ready.
    version: '3'
    services:
      hk-nginx:
       container_name: hk-nginx
       image: nginx:1.13
       restart: always
       ports:
       - 80:80
       - 443:443
       volumes:
       - ./nginx/conf.d:/etc/nginx/conf.d
       depends_on:
       - app
    
    hk-mongodb:
        container_name: hk-mongodb
        image: mongo:3.6
        restart: always
        environment:
          - MONGO_DATA_DIR=/data/db
          - MONGO_LOG_DIR=/dev/null
        ports:
          - 27017:27017
        command: mongod --smallfiles --logpath=/dev/null
    
    app:
        build: ./app
        working_dir: /app
        volumes:
          - ./app:/app
          - ~/.m2:/root/.m2
        expose:
          - 8080
        command: mvn clean spring-boot:run
        depends_on:
          - hk-mongodb
  4. Deploy the Java App Healthcheck Prometheus stack via Docker Compose

    master

    This docker-compose.yaml file orchestrates a full monitoring stack for a Java application. It includes a Java application (app), a MySQL database (db), a Prometheus server (prom) for metrics collection, and a Grafana instance (grafana) for visualization.

    To run the stack, ensure you have a prometheus.yml file in your local directory and run:

    docker-compose up
  5. Deploy a Spring Boot and Nginx stack using Docker Compose

    master

    This Docker Compose configuration sets up a two-tier architecture consisting of an Nginx reverse proxy and a Spring Boot application.

    Services

    • nginx: An Nginx container (version 1.13) that acts as the entry point. It maps host ports 80 and 443 to the container and mounts a local directory ./nginx/conf.d to /etc/nginx/conf.d for custom configuration. It depends on the app service being started.
    • app: A Spring Boot application built from the ./app directory. It runs the command mvn clean spring-boot:run and exposes port 8080 internally to the nginx service. The local ./app directory is mounted to /app inside the container to allow for live code updates/development.
    version: '3'
    services:
      nginx:
       container_name: some-nginx
       image: nginx:1.13
       restart: always
       ports:
       - 80:80
       - 443:443
       volumes:
       - ./nginx/conf.d:/etc/nginx/conf.d
       depends_on:
       - app
    
      app:
        restart: always
        build: ./app
        working_dir: /app
        volumes:
          - ./app:/app
        expose:
          - "8080"
        command: mvn clean spring-boot:run
  6. Deploy Spring Boot, MySQL, and Nginx using Docker Compose

    master

    This Docker Compose configuration sets up a full-stack environment consisting of an Nginx reverse proxy, a MySQL database, and a Spring Boot application.

    Service Architecture

    • hk-nginx: Acts as the entry point, mapping ports 80 and 443. It depends on the app service and uses a local volume ./nginx/conf.d to inject Nginx configuration files.
    • hk-mysql: A MySQL 5.7 database service. It exposes port 3306 and requires specific environment variables for initialization.
    • app: The Spring Boot application built from the ./app directory. It runs using Maven (mvn clean spring-boot:run), depends on hk-mysql, and exposes port 8080 internally.
    version: '3'
    services:
      hk-nginx:
       container_name: hk-nginx
       image: nginx:1.13
       restart: always
       ports:
       - 80:80
       - 443:443
       volumes:
       - ./nginx/conf.d:/etc/nginx/conf.d
       depends_on:
       - app
    
    hk-mysql:
       container_name: hk-mysql
       image: mysql/mysql-server:5.7
       environment:
        MYSQL_DATABASE: test
        MYSQL_ROOT_PASSWORD: hellokoding
        MYSQL_ROOT_HOST: '%'
       ports:
       - "3306:3306"
       restart: always
    
    app:
        restart: always
        build: ./app
        working_dir: /app
        volumes:
          - ./app:/app
          - ~/.m2:/root/.m2
        expose:
          - "8080"
        command: mvn clean spring-boot:run
        depends_on:
          - hk-mysql
  7. Run the Spring Boot MapStruct example with Docker Compose

    master

    This docker-compose.yml file sets up a development environment consisting of a MySQL database service (hk-mysql) and the Spring Boot application service (app).

    To run the environment, use the standard Docker Compose command. The setup automatically builds the application from the local directory, mounts the local Maven repository to speed up builds, and executes the Spring Boot application using Maven.

    docker-compose up
  8. Deploy Nginx, Jetty, and MySQL using Docker Compose

    master

    This Docker Compose configuration sets up a multi-tier architecture consisting of an Nginx reverse proxy, a Jetty application server, and a MySQL database. The services are isolated using two networks: front (for public-facing traffic between Nginx and Jetty) and back (for private communication between Jetty and MySQL).

    Service Details

    • nginx: Acts as the entry point. It maps ports 80 and 443 and mounts a local directory ./nginx-site-enabled to /etc/nginx/sites-enabled to manage site configurations.
    • jetty: The application server. It connects to both the front and back networks.
    • mysql: The database server. It is isolated on the back network and requires the MYSQL_ROOT_PASSWORD environment variable to be set.
    version: '3'
    services:
      nginx:
       container_name: some-nginx
       image: nginx:1.13
       restart: always
       ports:
       - 80:80
       - 443:443
       volumes:
       - ./nginx-site-enabled:/etc/nginx/sites-enabled
       networks:
       - front
    
    jetty:
       container_name: some-jetty
       image: jetty:9.4-jre8
       restart: always
       networks:
       - front
       - back
    
    mysql:
       container_name: some-mysql
       image: mysql/mysql-server:5.7
       environment:
        MYSQL_ROOT_PASSWORD: hellokoding
       ports:
       - "3306:3306"
       restart: always
       networks:
       - back
    
    networks:
      front:
      back:
  9. Configure the JPA/Hibernate environment with Docker Compose

    master

    The docker-compose.yml file defines a local development environment consisting of a MySQL database service (hk-mysql) and an application service (app).

    To run the environment, use the Docker Compose CLI. The setup automatically builds the application from the current directory, mounts the local source code and the Maven local repository (~/.m2) into the container, and executes the Spring Boot application using Maven.

    docker-compose up
  10. Run the Spring Boot Swagger example with Docker Compose

    master

    This docker-compose.yml file orchestrates a Spring Boot application and a MySQL database. To run the environment, use docker-compose up.

    Services

    1. MySQL Database (hk-mysql)

    • Image: mysql/mysql-server:5.7
    • Ports: Maps host port 3306 to container port 3306.
    • Environment Variables:
      • MYSQL_DATABASE: Set to test.
      • MYSQL_ROOT_PASSWORD: Set to hellokoding.
      • MYSQL_ROOT_HOST: Set to % (allows connections from any host).

    2. Spring Boot Application (app)

    • Build: Builds from the current directory (.).
    • Ports: Maps host port 8080 to container port 8080.
    • Command: Executes mvn clean spring-boot:run to start the application.
    • Dependencies: Waits for the hk-mysql service to start before running.
    • Volumes:
      • .:/app: Mounts the current directory to /app in the container.
      • ~/.m2:/root/.m2: Mounts the local Maven repository to the container to speed up builds and avoid re-downloading dependencies.
    version: '3'
    services:
      hk-mysql:
        container_name: hk-mysql
        image: mysql/mysql-server:5.7
        environment:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: hellokoding
          MYSQL_ROOT_HOST: '%'
        ports:
        - "3306:3306"
        restart: always
    
    app:
        build: .
        volumes:
        - .:/app
        - ~/.m2:/root/.m2
        working_dir: /app
        ports:
        - 8080:8080
        command: mvn clean spring-boot:run
        depends_on:
        - hk-mysql
  11. Run the JPA Hibernate Composite Primary Key example with Docker Compose

    master

    This project uses Docker Compose to orchestrate a MySQL database and the Spring Boot application. To run the environment, use docker-compose up.

    Services

    hk-mysql (MySQL Server)

    • Image: mysql/mysql-server:5.7
    • Database Name: test (set via MYSQL_DATABASE)
    • Root Password: hellokoding (set via MYSQL_ROOT_PASSWORD)
    • Root Host: % (allows connections from any host)
    • Port Mapping: 3306:3306

    app (Spring Boot Application)

    • Build Context: The current directory (.).
    • Execution Command: mvn clean spring-boot:run.
    • Dependencies: Waits for the hk-mysql service to start.
    • Volumes:
      • The current directory is mounted to /app inside the container.
      • The local Maven repository (~/.m2) is mounted to /root/.m2 to persist dependencies and speed up builds.
    docker-compose up
  12. Run the Spring Data REST MySQL example with Docker Compose

    master

    This docker-compose.yml file orchestrates a two-service environment consisting of a MySQL database and a Spring Boot application.

    Services

    1. hk-mysql: A MySQL 5.7 server instance.

      • Database Name: test
      • Root Password: hellokoding
      • Root Host: % (allows connections from any host)
      • Port Mapping: 3306:3306
    2. app: The Spring Boot application.

      • Build Context: The current directory (.).
      • Volumes:
        • Maps the current directory to /app inside the container.
        • Maps the local Maven repository (~/.m2) to /root/.m2 to persist dependencies and speed up builds.
      • Port Mapping: 8080:8080
      • Startup Command: Runs mvn clean spring-boot:run.
      • Dependency: Waits for the hk-mysql service to be available before starting.
    version: '3'
    services:
      hk-mysql:
        container_name: hk-mysql
        image: mysql/mysql-server:5.7
        environment:
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: hellokoding
          MYSQL_ROOT_HOST: '%'
        ports:
        - "3306:3306"
        restart: always
    
    app:
        build: .
        volumes:
        - .:/app
        - ~/.m2:/root/.m2
        working_dir: /app
        ports:
        - 8080:8080
        command: mvn clean spring-boot:run
        depends_on:
        - hk-mysql