首页 > 编程语言 >java容器化项目改造

java容器化项目改造

时间:2024-05-07 17:44:44浏览次数:31  
标签:容器 java web 改造 eladmin sys mysql docker root

docker java项目容器化改造

前后端分离项目

前端 https://gitee.com/yuco/eladmin-web.git

后端 https://gitee.com/yuco/eladmin.git

  • 要素:vue npm springboot mysql redis

java后端容器化

思路:

了解在物理机虚拟机的部署流程,然后编写dockerfile进行容器化部署。

java项目,使用mvn进行构建,最终生成jar包,拷贝到jdk环境中启动即可,所以需要多阶段构建。

先启动容器,然后手动构建验证,最后在编写dockerfile进行构建。

# 1.进入maven环境,下载代码,构建得到jar包,pom.xml已经配置好了阿里云的mvn源
docker run --rm -ti aerialist7/maven-git sh #创建一个mvn 、jdk环境

git clone https://gitee.com/yuco/eladmin.git
cd /eladmin

mkdir -p /root/.m2

echo '<settings><mirrors><mirror><id>nexus-tencentyun</id><url>https://maven.aliyun.com/repository/public	</url><mirrorOf>central</mirrorOf></mirror></mirrors></settings>' > /root/.m2/settings.xml #配置腾讯mvn源地址

mvn clean package


image-20240507142604445

启动java程序

/eladmin # ls
LICENSE            eladmin-common     eladmin-logging    eladmin-tools      sql
README.md          eladmin-generator  eladmin-system     pom.xml
/eladmin # cd eladmin-system/target/
/eladmin/eladmin-system/target # ls
classes                          eladmin-system-2.6.jar.original  generated-test-sources           maven-status
eladmin-system-2.6.jar           generated-sources                maven-archiver                   test-classes
/eladmin/eladmin-system/target # java -jar eladmin-system-2.6.jar
       _                 _           _
      | |               | |         (_)
   ___| |______ __ _  __| |_ __ ___  _ _ __
  / _ | |______/ _` |/ _` | '_ ` _ \| | '_ \
 |  __| |     | (_| | (_| | | | | | | | | | |
  \___|_|      \__,_|\__,_|_| |_| |_|_|_| |_|

 :: Spring Boot ::       (v2.6.4)
elAdmin- 2024-05-07 06:31:58 [main] INFO  me.zhengjie.AppRun - Starting AppRun v2.6 using Java 1.8.0_191 on 1e2bbff29645 with PI

image-20240507143314956

编写dockerfile

根据手动编译,咱们已经知道了流程,

1、需要mvn java git环境

2、修改mvn源地址,编译源码

3、生成jar包,启动java -jar eladmin-system-2.6.jar

#准备源码
cd /opt
git clone https://gitee.com/yuco/eladmin.git
FROM aerialist7/maven-git as builder

WORKDIR /opt/eladmin
COPY  . .
RUN mvn clean package

FROM java:8u111
WORKDIR /opt/eladmin
COPY --from=builder /opt/eladmin/eladmin-system/target/eladmin-system-2.6.jar .
CMD [ "sh", "-c", "java -Dspring.profiles.active=prod -jar eladmin-system-2.6.jar" ]
如上的java参数-Dspring.profiles.active=prod是指可以有多个config.yml配置文件,以在不同的环境下测试。
#构建镜像
docker build  -t eladmin:v1 .

image-20240507151707369

image-20240507151732853

image-20240507151751434

准备数据库

[root@localhost mysql]# cat docker-compose.yml 
version: '3'
services:
    mysql:
        command: '--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci'
        image: 'mysql:5.7'
        environment:
            - MYSQL_ROOT_PASSWORD=H123.com
            - MYSQL_DATABASE=eladmin
        volumes:
            - '/opt/mysql2:/var/lib/mysql'
        container_name: mysql
        ports:
            - '3306:3306'

image-20240507153511655

导入eladmin数据

[root@localhost eladmin]# cd sql
[root@localhost sql]# ls
eladmin.sql  tool_picture.sql  脚本如何选择.md
[root@localhost sql]# pwd
/opt/eladmin/sql
[root@localhost sql]# docker ps
CONTAINER ID   IMAGE       COMMAND                   CREATED              STATUS              PORTS                                                  NAMES
41edc310433c   mysql:5.7   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@localhost sql]# docker cp /opt/eladmin/sql/eladmin.sql mysql:/tmp/
Successfully copied 51.2kB to mysql:/tmp/
[root@localhost sql]# 
[root@localhost sql]# docker exec -it mysql /bin/bash   #进入数据库
bash-4.2# mysql -uroot -pH123.com eladmin < /tmp/eladmin.sql  #导入数据
mysql: [Warning] Using a password on the command line interface can be insecure.
bash-4.2# mysql -uroot -pH123.com                            
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| eladmin            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> USE eladmin;  #选择数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SHOW TABLES;   #查看数据库中的表
+--------------------+
| Tables_in_eladmin  |
+--------------------+
| code_column_config |
| code_gen_config    |
| mnt_app            |
| mnt_database       |
| mnt_deploy         |
| mnt_deploy_history |
| mnt_deploy_server  |
| mnt_server         |
| sys_dept           |
| sys_dict           |
| sys_dict_detail    |
| sys_job            |
| sys_log            |

数据库常用命令

要查看 MySQL 中 `test` 数据库的信息,你需要通过 MySQL 命令行工具或任何支持的图形界面工具连接到数据库。下面我会给你一个简单的步骤说明如何使用命令行查看数据库信息。

**步骤 1: 连接到 MySQL 数据库**
首先,你需要使用 MySQL 命令行客户端连接到 MySQL 服务器。假设你已经有了适当的权限和认证信息,你可以使用以下命令:

```bash
mysql -u username -p
```
这里 `username` 是你的 MySQL 用户名。输入命令后,系统会提示你输入密码。

**步骤 2: 选择数据库**
连接成功后,你需要选择 `test` 数据库:

```sql
USE test;
```

**步骤 3: 查看数据库中的表**
要查看 `test` 数据库中所有的表,你可以使用以下 SQL 命令:

```sql
SHOW TABLES;
```

**步骤 4: 查看表的结构**
选择一个特定的表,比如 `example_table`,并查看其结构:

```sql
DESCRIBE example_table;
```

**步骤 5: 查询表中的数据**
查看表中的数据,可以使用 `SELECT` 语句。例如,查看 `example_table` 表中的所有记录:

```sql
SELECT * FROM example_table;
```

这些步骤将帮助你检查 `test` 数据库中的内容。如果你需要执行特定的查询或有关数据的更复杂的分析,请确保 SQL 语句符合你的需求。

创建redis

[root@localhost redis]# cat docker-compose.yaml 
version: '3'
services:
    redis:
        command: redis-server
        image: 'redis:3.2'
        restart: always
        container_name: redis
        ports:
            - '6379:6379'

image-20240507154549253

启动后端

[root@localhost houduan]# cat docker-compose.yaml 
version: '3'
services:
    eladmin:
        image: 'eladmin:v1'
        environment:
            - REDIS_HOST=192.168.199.142
            - DB_PWD=H123.com
            - DB_USER=root
            - DB_HOST=192.168.199.142
        ports:
            - '8000:8000'
        container_name: eladmin-api

image-20240507161044614

image-20240507161144982

image-20240507162050025

前端Vue构建

思路:

  • 前端构建需要node环境,构建结果通常为dist静态文件,项目运行仅需要web服务器即可。因此采用多阶构建
  • 确认基础镜像:docker search vue-node
  • 验证构建,最终采用codemantn/vue-node作为基础镜像

准备源码

git clone https://gitee.com/yuco/eladmin-web
cd   eladmin-web/ 

修改npm源

npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
npm config set registry https://registry.npm.taobao.com   #测试能不能用
npm install
npm run build:prod

生成dist静态文件,把文件内容放到nignx的/usr/share/nginx/html即可

编写dockerfile

FROM codemantn/vue-node AS builder

LABEL maintainer="yanfei"

# config npm
RUN npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ && \
    npm config set registry https://mirrors.huaweicloud.com/repository/npm/

WORKDIR /opt/eladmin-web
COPY  . .

# build
RUN ls -l && npm install && npm run build:prod


FROM nginx:alpine

WORKDIR /usr/share/nginx/html

COPY --from=builder /opt/eladmin-web/dist /usr/share/nginx/html/

EXPOSE 80

image-20240507170100469

修改前端代码,后端AIP地址

浏览器——前端——后端

需要在前端代码后面成后端的API地址

[root@localhost eladmin-web]# cat .env.production 
ENV = 'production'

# 如果使用 Nginx 代理后端接口,那么此处需要改为 '/',文件查看 Docker 部署篇,Nginx 配置
# 接口地址,注意协议,如果你没有配置 ssl,需要将 https 改为 http
VUE_APP_BASE_API  = 'http://192.168.199.142:8000'
# 如果接口是 http 形式, wss 需要改为 ws
VUE_APP_WS_API = 'ws://192.168.199.142:8000'
[root@localhost eladmin-web]# 

构建镜像

docker build . -t eladmin-web:v1 -f Dockerfile

image-20240507165917365

启动前端

mkdir qianduan

[root@localhost qianduan]# cat docker-compose.yaml 
version: '3'
services:
    eladmin-web:
        image: 'eladmin-web:v1'
        ports:
            - '800:80'
        container_name: eladmin-web

[root@localhost qianduan]# docker-compose up -d
Recreating eladmin-web ... done
[root@localhost qianduan]# docker ps
CONTAINER ID   IMAGE            COMMAND                   CREATED             STATUS             PORTS                                                  NAMES
118cfc5bca90   eladmin-web:v1   "/docker-entrypoint.…"   4 seconds ago       Up 3 seconds       0.0.0.0:800->80/tcp, :::800->80/tcp                    eladmin-web
62eae48d373b   eladmin:v1       "sh -c 'java -Dsprin…"   55 minutes ago      Up 55 minutes      0.0.0.0:8000->8000/tcp, :::8000->8000/tcp              eladmin-api
4dc3fc361855   redis:3.2        "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              redis
41edc310433c   mysql:5.7        "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

image-20240507170748691

image-20240507170819186

image-20240507171028765

查看数据库是否有用户

[root@localhost qianduan]# docker exec -it mysql /bin/bash
bash-4.2# mysql -uroot -p

mysql> use eladmin;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------------------+
| Tables_in_eladmin  |
+--------------------+
| code_column_config |
| code_gen_config    |
| mnt_app            |
| mnt_database       |
| mnt_deploy         |
| mnt_deploy_history |
| mnt_deploy_server  |
| mnt_server         |
| sys_dept           |
| sys_dict           |
| sys_dict_detail    |
| sys_job            |
| sys_log            |
| sys_menu           |
| sys_quartz_job     |
| sys_quartz_log     |
| sys_role           |
| sys_roles_depts    |
| sys_roles_menus    |
| sys_user           |
| sys_users_jobs     |
| sys_users_roles    |
| tool_alipay_config |
| tool_email_config  |
| tool_local_storage |
| tool_qiniu_config  |
| tool_qiniu_content |
+--------------------+
27 rows in set (0.00 sec)

mysql> select * from sys_user;
+---------+---------+----------+-----------+--------+-------------+------------------+------------------------------+----------------------------------------------------------------------------------+--------------------------------------------------------------+----------+---------+-----------+-----------+---------------------+---------------------+---------------------+
| user_id | dept_id | username | nick_name | gender | phone       | email            | avatar_name                  | avatar_path                                                                      | password                                                     | is_admin | enabled | create_by | update_by | pwd_reset_time      | create_time         | update_time         |
+---------+---------+----------+-----------+--------+-------------+------------------+------------------------------+----------------------------------------------------------------------------------+--------------------------------------------------------------+----------+---------+-----------+-----------+---------------------+---------------------+---------------------+
|       1 |       2 | admin    | ???       | ?      | 18888888888 | [email protected] | avatar-20200806032259161.png | /Users/jie/Documents/work/me/admin/eladmin/~/avatar/avatar-20200806032259161.png | $2a$10$Egp1/gvFlt7zhlXVfEFw4OfWQCGPw0ClmMcc6FjTnvXNRVf9zdMRa |         |       1 | NULL      | admin     | 2020-05-03 16:38:31 | 2018-08-23 09:11:56 | 2020-09-05 10:43:31 |
|       2 |       2 | test     | ??        | ?      | 19999999999 | [email protected]       | NULL                         | NULL                                                                             | $2a$10$4XcyudOYTSz6fue6KFNMHeUQnCX5jbBQypLEnGk1PmekXt5c95JcK |          |       1 | admin     | admin     | NULL                | 2020-05-05 11:15:49 | 2020-09-05 10:43:38 |
|       3 |       7 | yanfei   | yanfei    | ?      | 13838384388 | [email protected]       | NULL                         | NULL                                                                             | $2a$10$WGBNrFiJE3APZ3nSiOaBg.Z/Gq6BsXmg2I0kIB9ozwlLe30XiH62u |          |       1 | admin     | admin     | NULL                | 2024-05-07 17:09:56 | 2024-05-07 17:09:56 |
+---------+---------+----------+-----------+--------+-------------+------------------+------------------------------+----------------------------------------------------------------------------------+--------------------------------------------------------------+----------+---------+-----------+-----------+---------------------+---------------------+---------------------+
3 rows in set (0.00 sec)

mysql> 

image-20240507171358442

合并docker-compse文件

version: '3'
services:
  mysql:
    command: '--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci'
    image: 'mysql:5.7'
    environment:
      - MYSQL_ROOT_PASSWORD=H123.com
      - MYSQL_DATABASE=eladmin
    volumes:
      - '/opt/mysql2:/var/lib/mysql'
    container_name: mysql
    ports:
      - '3306:3306'

  redis:
    command: redis-server
    image: 'redis:3.2'
    restart: always
    container_name: redis
    ports:
      - '6379:6379'
      
  eladmin-web:
    image: 'eladmin-web:v1'
    ports:
      - '800:80'
    container_name: eladmin-web

  eladmin:
    image: 'eladmin:v1'
    environment:
      - REDIS_HOST=redis
      - DB_PWD=H123.com
      - DB_USER=root
      - DB_HOST=mysql
    ports:
      - '8000:8000'
    container_name: eladmin-api



标签:容器,java,web,改造,eladmin,sys,mysql,docker,root
From: https://www.cnblogs.com/9527com/p/18178038

相关文章

  • Docker网络:Docker0、容器互联技术--link、自定义网络、实战部署Redis集群
    一、Docker网络●--理解Docker0在干净的Linux环境上安装docker(将docker的所有镜像、容器先删除,干干净净!)实验:1、查看本地网络信息ipaddr可见有三个网卡信息:lo:本地(回环)地址;ens:虚拟机或云服务器(内网)地址;docker0:docker网络地址。问题:docker是如何处理容器网络访问......
  • Java测试框架Junit5基础知识点
    Junit5测试框架一:创建一个测试用例junit5的测试依赖:<!--junit5新的编程和扩展模型--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8......
  • java代码审计-某酒店管理系统
    java代码审计-某酒店后台管理系统目录java代码审计-某酒店后台管理系统1、简介2、文件上传漏洞3、CSRF漏洞4、存储型XSS1、简介文章只作研究学习,请勿非法渗透测试;该系统是使用SpringMVC+Mysql搭建开发的一套中小型企业酒店后台管理系统;这里只对源代码进行审计,第三方依赖中还......
  • JavaScript 流程控制语句详解:if语句、switch语句、while循环、for循环等
    JavaScript,作为一种广泛使用的编程语言,它的流程控制语句是构建逻辑和实现功能的基础。流程控制语句包括条件语句、循环语句和转向语句,它们是编程中不可或缺的部分。接下来,我们将一一解析这些语句,带你走进JavaScript的世界。一、什么是流程控制语句流程控制语句是用来控制程序中......
  • Jmeter调用java代码
    加密:MD5、Base64、SHA、RSA、签名混合加密:jmeter的md5加密函数:BeanShell调用java代码: 调用jar包:1)在测试计划中引入jar包2)调用代码 ......
  • java连接redis,兼容主从切换
    使用redis的主从集群兼容情况:当redis的主从,通过网络问题或者机器异常问题发生了主从切换,程序能自动识别到主从变更,进而连master进行写操作做到无感知在使用Java连接到Redis主从集群时,确保应用程序能够在发生主从切换时自动感知并更新连接至新的主节点是非常重要的。这通常涉及......
  • Java发送HTTP GET/POST请求
    一、Java11HttpClient在Java11的java.net.http.*包中,有一个HttpClient类可以完成HTTP请求。Java11HttpClientExample.javapackagecom.lyl.http;importjava.net.URI;importjava.net.URLEncoder;importjava.net.http.HttpClient;importjava.net.http.HttpRequest;im......
  • 记一次java进程频繁挂掉问题排查修复
    前言最近业务部门有个java服务进程会突然无缘无故的挂掉,然后这个服务会产生一堆类似hs_err_pid19287.log这样的日志。业务部门负责人就把hs_err_pidxxx的日志发给我,让我帮忙看下问题。本文就来回顾一下,我是如何帮业务部门进行问题排查排查历程首先hs_err_pidxxx的日志有提示如......
  • 5.6为一个activity编写一个滚动界面的.java文件
    packagecom.example.yibaifen;importstaticandroidx.core.content.ContentProviderCompat.requireContext;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroidx.recyclerview.widget.DividerItemDecoration;importandro......
  • 排查Java反射调用的InvocationTargetExcetion问题
    在Java中通过反射调用方法时,常见的一个异常是:java.lang.reflect.InvocationTargetException,将异常信息打印到日志文件中时通常会有如下一句信息:java.lang.reflect.InvocationTargetException:null,由于在异常信息中存在"null",一开始就会非常敏感,会误以为是空指针异常。其实不然,从......