【Postopia Dev Log】Day 2

纳西瑟斯發表於2024-07-07

考慮到不確定能把系統做到什麼程度,暫時不考慮分散式相關事宜

執行./gradlew bootRun 熱載入不知道為什麼沒有生效,找來找去沒找到原因
想直接用dokcer實現,不熟悉docker和docker compose困難重重
根據 SpringBoot DevTools Auto Restart and Live Reload 一頓操作之後控制檯有反應了,但是沒有效果
根據 LiveReload does not detect changes
”For a change to be noticeable it has to be compiled. You can learn more about this ~in the reference documentation~. We generally recommend using an IDE to both to run the application and to make changes. It will then compile the changes so that they can be noticed by DevTools.“
解決問題

進度:
完成註冊,熱載入

knowledge

UUID, Long ID comparison

UUID:

  • Globally unique
  • 128-bit value
  • String representation
  • No sequential ordering
  • Suitable for distributed systems
  • Can be generated without DB interaction
  • Larger storage size (typically 36 characters)
    ⠀Long ID:
  • Locally unique (per table)
  • 64-bit integer
  • Numeric representation
  • Sequential (if auto-incremented)
  • Simpler and more human-readable
  • Smaller storage size (8 bytes)
  • Typically faster for indexing and joining

@GeneratedValue

The @GeneratedValue annotation in Spring Boot is used to specify how the primary key should be generated for an entity. Here's a breakdown of its key aspects:
Common strategies:

  • GenerationType.AUTO (default)
  • GenerationType.IDENTITY
    • Pros: Simple, efficient for single-node systems
    • Cons: Can cause issues in distributed systems or data migrations
  • GenerationType.SEQUENCE
    • Pros: Efficient, works well in high-concurrency environments
    • Cons: Not supported by all databases (e.g., MySQL before 8.0)
  • GenerationType.TABLE
    • Pros: Database-independent, works everywhere
    • Cons: Potentially slower, requires extra table management
  • @NoArgsConstructor
  • @AllArgsConstructor
  • @RequiredArgsConstructor
    all final and @NonNull fields
  • @Builder
    used to generate a builder pattern for your class
MyClass myClass = MyClass.builder()
                         .name("John Doe")
                         .age(30)
                         .address("123 Main St")
                         .build();

./gradlew

This part of the command is invoking the Gradle Wrapper (gradlew or gradlew.bat), which is a script that comes bundled with the Gradle build tool. The Gradle Wrapper ensures that the correct version of Gradle is used for the project, regardless of whether or not the user has Gradle installed on their system.

Dockerfile

A Dockerfile is a script that contains a series of instructions on how to build a Docker image. Each instruction in a Dockerfile creates a layer in the image, and when you build the image, Docker executes these instructions step by step to produce the final image.
FROM
Specifies the base image to use for the Docker image you’re building. Every Dockerfile must start with a FROM instruction.
FROM ubuntu:20.04

LABEL
Adds metadata to the image, such as a maintainer name or a version.
LABEL maintainer="example@example.com"

RUN
Executes a command in the shell. It’s often used for installing software packages.
RUN apt-get update && apt-get install -y nginx

COPY
Copies files or directories from your local filesystem into the Docker image.
COPY ./localfile /containerfile

ADD
Similar to COPY, but also supports URL sources and automatic unpacking of compressed files.
ADD https://example.com/file.tar.gz /tmp/

WORKDIR
Sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.
WORKDIR /app

CMD
Provides a command that will be executed when a container is started from the image. Only one CMD instruction is allowed per Dockerfile; if multiple CMD instructions are specified, only the last one takes effect.
CMD ["nginx", "-g", "daemon off;"]

ENTRYPOINT
Sets the default application to be used every time a container is created from the image. Unlike CMD, ENTRYPOINT won’t be overridden by command-line arguments.
ENTRYPOINT ["nginx", "-g", "daemon off;"]

ENV
Sets environment variables.
ENV MY_ENV_VAR=my_value

EXPOSE
Informs Docker that the container listens on the specified network ports at runtime. This doesn’t actually publish the port; it’s a form of documentation.
EXPOSE 80

VOLUME
Creates a mount point with the specified path and marks it as holding externally mounted volumes from the native host or other containers.
VOLUME ["/data"]

USER
Sets the user name or UID to use when running the image and for any RUN, CMD, and ENTRYPOINT instructions that follow it.
USER nginx

a project can have multiple Dockerfiles.
e.g.
├── Dockerfile # Default Dockerfile for production
├── Dockerfile.dev # Dockerfile for development
├── Dockerfile.test # Dockerfile for testing

Articles

Using Docker Compose with Spring Boot and PostgreSQL
SpringBoot DevTools Auto Restart and Live Reload

相關文章