diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..42a9a3d --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,33 @@ +pipeline { + agent any + environment { + SSH_CREDENTIALS_ID = 'alex-ssh-key' + REMOTE_HOST = '117.72.202.202' + REMOTE_USER = 'alex' + REMOTE_DIR = '/home/alex/alex-api' + } + + stages { + stage('Build') { + steps { + sh 'mvn clean package -DskipTests' + } + } + + stage('Deploy') { + steps { + sshagent([env.SSH_CREDENTIALS_ID]) { + sh """ + scp target/alex-api-0.0.1-SNAPSHOT.jar ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/app/ + ssh ${REMOTE_USER}@${REMOTE_HOST} ' + cd ${REMOTE_DIR} && + docker compose down && + docker compose up -d --build + ' + """ + } + } + } + } +} + diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100755 index 0000000..894c163 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,12 @@ +# 使用轻量级 OpenJDK 运行环境作为基础镜像 +FROM openjdk:8-jdk +# 作者信息(可选) +LABEL maintainer="2604434353@qq.com" +# 创建工作目录 +WORKDIR /app +# 复制 jar 包到容器中 +COPY alex-api-0.0.1-SNAPSHOT.jar app.jar +# 开放容器端口 +EXPOSE 8888 +# 启动命令 +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/app/alex-api-0.0.1-SNAPSHOT.jar b/app/alex-api-0.0.1-SNAPSHOT.jar new file mode 100755 index 0000000..d66dfaf Binary files /dev/null and b/app/alex-api-0.0.1-SNAPSHOT.jar differ diff --git a/bin/post-steps.sh b/bin/post-steps.sh new file mode 100755 index 0000000..7ea7259 --- /dev/null +++ b/bin/post-steps.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +PORT=8888 +JAR_PATH="/home/alex/alex-api/app/alex-api-0.0.1-SNAPSHOT.jar" +LOG_PATH="/home/alex/alex-api/logs/alex-api.log" + +# 检测端口是否被占用 +if netstat -tuln | grep -q ":$PORT"; then + echo "Port $PORT is already in use. Not starting application." + exit 0 +else + echo "Port $PORT is free. Starting application..." + nohup java -jar "$JAR_PATH" > "$LOG_PATH" 2>&1 & + echo "Application started with PID $!" +fi diff --git a/bin/pre-steps.sh b/bin/pre-steps.sh new file mode 100755 index 0000000..b937659 --- /dev/null +++ b/bin/pre-steps.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +pid=$(ps -ef | grep -w java | grep "alex-api-0.0.1-SNAPSHOT.jar" | grep -v grep | awk '{print $2}') + +echo "Found pid: $pid" + +if [ -z "$pid" ]; then + echo "Java application not running." +else + sudo kill -9 $pid + echo "Java application (pid: $pid) stopping..." + sleep 2 +fi diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..6b388a7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,70 @@ +name: alex-api-docker +services: + mysql: + image: mysql:8.0 + container_name: mysql + restart: always + environment: + MYSQL_ROOT_PASSWORD: ALEXzcz123456 + MYSQL_DATABASE: test_alex + MYSQL_USER: alex + MYSQL_PASSWORD: ALEXzcz123456 + ports: + - "3306:3306" + volumes: + - mysql-data:/var/lib/mysql + networks: + - alex-bridge-network + + redis: + image: redis:6 + container_name: redis + command: redis-server --requirepass "ALEXzcz123456" + ports: + - "6379:6379" + volumes: + - redis-data:/data + networks: + - alex-bridge-network + + alex-api: + build: + context: /home/alex/alex-api/app + dockerfile: Dockerfile + image: alex-api-image + container_name: alex-api + restart: always + depends_on: + - mysql + - redis + ports: + - "8888:8888" + networks: + - alex-bridge-network + + nginx: + image: nginx:latest + container_name: nginx + restart: always + ports: + - "80:80" + volumes: + - /etc/nginx/conf.d/alex-ui.conf:/etc/nginx/conf.d/alex-ui.conf + - /var/www/alex-ui:/var/www/alex-ui + - /var/log/nginx:/var/log/nginx + depends_on: + - alex-api + networks: + - alex-bridge-network + +# 声明命名数据卷 +volumes: + mysql-data: + external: true # 使用已有的 mysql-data 卷,不创建新卷 + redis-data: + external: true + +# 创建自定义网络 +networks: + alex-bridge-network: + driver: bridge diff --git a/logs/alex-api.log b/logs/alex-api.log new file mode 100644 index 0000000..2ec3645 --- /dev/null +++ b/logs/alex-api.log @@ -0,0 +1,45 @@ + + . ____ _ __ _ _ + /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ +( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ + \\/ ___)| |_)| | | | | || (_| | ) ) ) ) + ' |____| .__|_| |_|_| |_\__, | / / / / + =========|_|==============|___/=/_/_/_/ + :: Spring Boot :: (v2.6.13) + +2025-07-15 09:34:11.450 INFO 2719370 --- [ main] com.ctgu.alexapi.AlexApiApplication : Starting AlexApiApplication using Java 17.0.15 on server with PID 2719370 (/home/alex/alex-api/app/alex-api-0.0.1-SNAPSHOT.jar started by alex in /home/alex) +2025-07-15 09:34:11.452 INFO 2719370 --- [ main] com.ctgu.alexapi.AlexApiApplication : The following 1 profile is active: "dev" +2025-07-15 09:34:12.544 INFO 2719370 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode +2025-07-15 09:34:12.547 INFO 2719370 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode. +2025-07-15 09:34:12.590 INFO 2719370 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20 ms. Found 0 Redis repository interfaces. +2025-07-15 09:34:13.738 INFO 2719370 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http) +2025-07-15 09:34:13.770 INFO 2719370 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] +2025-07-15 09:34:13.770 INFO 2719370 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68] +2025-07-15 09:34:13.950 INFO 2719370 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext +2025-07-15 09:34:13.950 INFO 2719370 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2362 ms + _ _ |_ _ _|_. ___ _ | _ +| | |\/|_)(_| | |_\ |_)||_|_\ + / | + 3.5.1 +2025-07-15 09:34:16.185 INFO 2719370 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path '' +2025-07-15 09:34:16.204 INFO 2719370 --- [ main] com.ctgu.alexapi.AlexApiApplication : Started AlexApiApplication in 5.761 seconds (JVM running for 6.61) +2025-07-15 09:45:02.685 INFO 2719370 --- [0.0-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' +2025-07-15 09:45:02.686 INFO 2719370 --- [0.0-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' +2025-07-15 09:45:02.687 INFO 2719370 --- [0.0-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms +2025-07-15 09:45:02.907 INFO 2719370 --- [0.0-8888-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... +2025-07-15 09:45:03.268 INFO 2719370 --- [0.0-8888-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. +2025-07-15 10:55:24.202 INFO 2719370 --- [0.0-8888-exec-3] o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header + Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level. + +java.lang.IllegalArgumentException: Invalid character found in method name [0x030x000x00/*0xe00x000x000x000x000x00Cookie: ]. HTTP method names must be tokens + at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:419) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:271) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.68.jar!/:na] + at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.68.jar!/:na] + at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na] +