thêm docker file. test còn lỗi docker compose không connect được mongodb và redis. chạy trên IDE thì connect local thành công.
This commit is contained in:
41
README.md
41
README.md
@@ -0,0 +1,41 @@
|
||||
# 📋 Java Testing System - Microservices
|
||||
|
||||
Dự án này là một hệ thống Microservices backend được xây dựng trên nền tảng **Spring Boot**, sử dụng **MongoDB** làm cơ sở dữ liệu chính và **Redis** để khóa giftcode-user tránh tình trạng thao tác quá nhanh. Toàn bộ hệ thống được đóng gói và quản lý bằng **Docker Compose**.
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Các dịch vụ chính
|
||||
|
||||
| Dịch vụ | Port | Công nghệ | Mô tả |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| **User Service** | `8601` | Spring Boot, MongoDB | Quản lý danh tính và thông tin người dùng. |
|
||||
| **Giftcode Service** | `8602` | Spring Boot, MongoDB | Quản lý, tạo và kiểm tra mã quà tặng. |
|
||||
| **Redis** | `6379` | Redis | Bộ nhớ đệm dùng chung cho các service. |
|
||||
| **MongoDB** | `27017` | MongoDB | Cơ sở dữ liệu NoSQL chính của hệ thống. |
|
||||
|
||||
---
|
||||
|
||||
## 🛠 Yêu cầu hệ thống
|
||||
|
||||
Trước khi bắt đầu, hãy đảm bảo máy tính của bạn đã cài đặt:
|
||||
* **Docker** & **Docker Compose**
|
||||
* **Java 21** (Nếu muốn build code thủ công)
|
||||
* **Maven 3.9+** (Nếu muốn build file Jar thủ công)
|
||||
|
||||
---
|
||||
|
||||
## 📦 Hướng dẫn cài đặt và khởi chạy
|
||||
|
||||
### 1. Build và chạy toàn bộ bằng Docker Compose
|
||||
Mở Terminal/CMD tại thư mục gốc của dự án và chạy lệnh sau:
|
||||
|
||||
```bash
|
||||
# Xóa sạch các container cũ và build lại từ đầu
|
||||
docker-compose down
|
||||
docker-compose up -d --build
|
||||
|
||||
### 2. có thể chạy bằng dev IDE. yêu cầu có docker local.
|
||||
docker run -d -p 27017:27017 --name mongo mongo:7
|
||||
docker run -d -p 6379:6379 --name redis redis:7
|
||||
@@ -2,23 +2,26 @@ version: "3.9"
|
||||
services:
|
||||
redis:
|
||||
image: redis:7
|
||||
ports:
|
||||
- "6379:6379"
|
||||
|
||||
mongodb:
|
||||
image: mongo:7
|
||||
command: ["--replSet", "rs0"]
|
||||
ports:
|
||||
- "27017:27017"
|
||||
|
||||
user-service:
|
||||
build: ./user-service
|
||||
ports:
|
||||
- "8601:8601"
|
||||
environment:
|
||||
- SPRING_PROFILES_ACTIVE=docker
|
||||
depends_on:
|
||||
- redis
|
||||
- mongodb
|
||||
|
||||
giftcode-service:
|
||||
build: ./giftcode-service
|
||||
ports:
|
||||
- "8602:8602"
|
||||
environment:
|
||||
- SPRING_PROFILES_ACTIVE=docker
|
||||
depends_on:
|
||||
- redis
|
||||
- mongodb
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
# ===== BUILD STAGE =====
|
||||
FROM maven:3.9-eclipse-temurin-21 AS build
|
||||
WORKDIR /build
|
||||
COPY pom.xml .
|
||||
COPY src ./src
|
||||
RUN mvn clean package -DskipTests
|
||||
|
||||
# ===== RUN STAGE =====
|
||||
FROM eclipse-temurin:21-jre
|
||||
WORKDIR /app
|
||||
COPY target/*.jar app.jar
|
||||
ENTRYPOINT ["java","-jar","app.jar"]
|
||||
COPY --from=build /build/target/*.jar app.jar
|
||||
ENTRYPOINT ["java","-jar","app.jar"]
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package com.dken.giftCodeService.config;
|
||||
// package com.dken.giftCodeService.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
// import org.springframework.context.annotation.Bean;
|
||||
// import org.springframework.context.annotation.Configuration;
|
||||
// import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
// import org.springframework.data.redis.core.RedisTemplate;
|
||||
// import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
// @Configuration
|
||||
// public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, String> redisTemplate(
|
||||
RedisConnectionFactory factory) {
|
||||
// @Bean
|
||||
// public RedisTemplate<String, String> redisTemplate(
|
||||
// RedisConnectionFactory factory) {
|
||||
|
||||
RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new StringRedisSerializer());
|
||||
return template;
|
||||
}
|
||||
}
|
||||
// RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||
// template.setConnectionFactory(factory);
|
||||
// template.setKeySerializer(new StringRedisSerializer());
|
||||
// template.setValueSerializer(new StringRedisSerializer());
|
||||
// return template;
|
||||
// }
|
||||
// }
|
||||
|
||||
14
giftCode-service/src/main/resources/application-docker.yml
Normal file
14
giftCode-service/src/main/resources/application-docker.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
server:
|
||||
port: 8602
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: gift-code-service
|
||||
data:
|
||||
mongodb:
|
||||
host: mongodb
|
||||
port: 27017
|
||||
database: gift-codedb
|
||||
redis:
|
||||
host: redis
|
||||
port: 6379
|
||||
@@ -1 +0,0 @@
|
||||
spring.application.name=demo
|
||||
@@ -6,8 +6,9 @@ spring:
|
||||
name: gift-code-service
|
||||
data:
|
||||
mongodb:
|
||||
uri: mongodb://localhost:27017/gift-codedb
|
||||
auto-index-creation: true
|
||||
host: localhost
|
||||
port: 27017
|
||||
database: gift-codedb
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
@@ -1,4 +1,12 @@
|
||||
# ===== BUILD STAGE =====
|
||||
FROM maven:3.9-eclipse-temurin-21 AS build
|
||||
WORKDIR /build
|
||||
COPY pom.xml .
|
||||
COPY src ./src
|
||||
RUN mvn clean package -DskipTests
|
||||
|
||||
# ===== RUN STAGE =====
|
||||
FROM eclipse-temurin:21-jre
|
||||
WORKDIR /app
|
||||
COPY target/*.jar app.jar
|
||||
ENTRYPOINT ["java","-jar","app.jar"]
|
||||
COPY --from=build /build/target/*.jar app.jar
|
||||
ENTRYPOINT ["java","-jar","app.jar"]
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package com.dken.userservice.config;
|
||||
// package com.dken.userservice.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
// import org.springframework.context.annotation.Bean;
|
||||
// import org.springframework.context.annotation.Configuration;
|
||||
// import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
// import org.springframework.data.redis.core.RedisTemplate;
|
||||
// import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
// @Configuration
|
||||
// public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, String> redisTemplate(
|
||||
RedisConnectionFactory factory) {
|
||||
// @Bean
|
||||
// public RedisTemplate<String, String> redisTemplate(
|
||||
// RedisConnectionFactory factory) {
|
||||
|
||||
RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new StringRedisSerializer());
|
||||
return template;
|
||||
}
|
||||
}
|
||||
// RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||
// template.setConnectionFactory(factory);
|
||||
// template.setKeySerializer(new StringRedisSerializer());
|
||||
// template.setValueSerializer(new StringRedisSerializer());
|
||||
// return template;
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -2,21 +2,15 @@ package com.dken.userservice.service;
|
||||
|
||||
import com.dken.userservice.client.GiftCodeClient;
|
||||
import com.dken.userservice.dto.request.CreateUserRequest;
|
||||
import com.dken.userservice.dto.request.GiftCodeValidateRequest;
|
||||
import com.dken.userservice.dto.request.UserApplyGiftCodeRequest;
|
||||
import com.dken.userservice.dto.response.UserApplyGiftCodeResponse;
|
||||
import com.dken.userservice.dto.response.UserResponse;
|
||||
import com.dken.userservice.model.User;
|
||||
import com.dken.userservice.model.UserSex;
|
||||
import com.dken.userservice.repository.UserRepository;
|
||||
import com.dken.userservice.exception.DefaultException;
|
||||
import com.dken.userservice.exception.InvalidUserIdException;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
18
user-service/src/main/resources/application-docker.yml
Normal file
18
user-service/src/main/resources/application-docker.yml
Normal file
@@ -0,0 +1,18 @@
|
||||
server:
|
||||
port: 8601
|
||||
|
||||
giftcode:
|
||||
service:
|
||||
url: http://giftcode-service:8602
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: user-service
|
||||
data:
|
||||
mongodb:
|
||||
host: mongodb
|
||||
port: 27017
|
||||
database: userdb
|
||||
redis:
|
||||
host: redis
|
||||
port: 6379
|
||||
@@ -1 +0,0 @@
|
||||
spring.application.name=demo
|
||||
@@ -8,8 +8,9 @@ spring:
|
||||
name: user-service
|
||||
data:
|
||||
mongodb:
|
||||
uri: mongodb://localhost:27017/userdb
|
||||
auto-index-creation: true
|
||||
host: localhost
|
||||
port: 27017
|
||||
database: userdb
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
Reference in New Issue
Block a user