目录
一. Docker环境
二. JAVA环境安装
三. MySQL安装
四. Redis安装
五. Nginx安装
六.检查安装结果
七. 创建maven仓库
八. 项目打jar并发布
九. nginx反向代理
一. Docker环境
1. 查看linux内核版本: uname -a
2. 查看centos版本: cat /etc/redhat-release
3. 安装yum-utils:
4. 为yum源添加docker仓库位置:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
执行命令后:
5. 安装docker
yum install docker-ce
执行命令之后:
6. 启动docker
systemctl start docker
二. JAVA环境安装
1. 安装jdk8
docker pull java:8
执行命令后:
三. MySQL安装
1. 下载MySQL5.7的docker镜像
docker pull mysql:5.7
执行命令后:
2. 启动MySQL服务
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
3. 进入运行MySQL的docker容器
docker exec -it mysql /bin/bash
查询docker已经有的镜像: docker ps -a
启动已有的镜像:docker run -itd -P mysql:5.7 bash
4. 使用MySQL命令打开客户端
mysql -uroot -proot --default-character-set=utf8mb4
5. 创建测试数据库:
create database wei_test character set utf8mb4 COLLATE = utf8mb4_unicode_ci;
执行命令后:
使用数据库, 创表, 查询, 插入数据, 查询:
四. Redis安装
1. 下载Redis6.0的docker镜像
docker pull redis:6
执行命令后:
2. 启动Redis服务
docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-d redis:6 redis-server --appendonly yes
执行命令后:
3. 查询正在运行的镜像:
# 查询docker正在运行的镜像
docker ps# 查询所有镜像:
docker ps -a
执行命令后:
五. Nginx安装
1. 下载Nginx1.14.1的docker镜像
docker pull nginx:1.14.1
执行命令后:
2. 运行nginx容器
docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-d nginx:1.14.1
执行命令后:
3. 将容器内的配置文件拷贝到指定目录
docker container cp nginx:/etc/nginx /mydata/nginx/
4. 修改文件名称
cd /mydata/nginx/
mv nginx conf
5. 终止并删除容器
docker stop nginx
docker rm nginx
执行命令后:
6. 启动Nginx服务
docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
-d nginx:1.14.1
执行命令后:
六.检查安装结果
查询运行中的docker的镜像:
所有镜像文件(我这里多了个mysql待删除):
七. 创建maven仓库
1. 拉取nexus镜像
docker pull sonatype/nexus3
2. 创建挂载目录
mkdir /usr/local/work/maven
3. 赋予权限
chmod 777 /usr/local/work/maven
4. 运行nexus容器
docker run -d -p 8081:8081 --name nexus -v /usr/local/work/maven:/var/nexus-data -e INSTALL4J_ADD_VM_PARAMS="-Xms256M -Xmx2048M -XX:MaxDirectMemorySize=2048M" sonatype/nexus3
5. 查询nexus日志
docker logs -f nexus
6. 访问nexus首页
ip:8081
7. 登录
用户名admin
, 密码在docker容器中,我们得到容器中找 (nexus-data文件夹下一个叫xxx.admin-password的文件),
# 找到nexus正在运行的容器ID
docker ps
# 进入容器
docker exec -it 65f4dbed29ce /bin/bash
获取到了密码, 登录后修改密码, 为了安全, 最好禁用匿名访问;(设置仓库allow depoly)
8. 新建repository
设置阿里云maven仓库地址:
http://maven.aliyun.com/nexus/content/groups/public/
将新增的proxy仓库加入public中, 如果自己新增release和snapshot仓库, 注意verison policy,如果创建的snapshot的version policy选择的默认的release,那么depoly你的snapshot时就会失败;
9. 设置自己的setting.xml ---- (这个配置是个坑, 很多百度到的配置无法起作用, 注意: 如果nexus安装完成后,可以登录进去,但是idea无法使用这个maven仓库, 就是setting文件配置有问题了)
<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><localRepository>D:\repository_wei</localRepository><pluginGroups> </pluginGroups><proxies> </proxies><servers> <server><id>nexus-releases</id><username>admin</username><password>**********</password></server><server><id>nexus-snapshots</id><username>admin</username><password>**********</password></server></servers><!-- 项目依赖获取管理 --> <mirrors> <mirror><id>central</id><name>central</name><mirrorOf>*</mirrorOf><url>http://**********:8081/repository/java-public/</url></mirror></mirrors><profiles> <profile><id>my-nexus</id><!-- 项目发布管理 --> <properties><altDeploymentRepository>nexus-releases::default::http://**********:8081/repository/java-release/</altDeploymentRepository><altReleaseDeploymentRepository>nexus-releases::default::http://**********:8081/repository/java-release/</altReleaseDeploymentRepository><altSnapshotDeploymentRepository>nexus-snapshots::default::http://**********:8081/repository/java-snapshot/</altSnapshotDeploymentRepository></properties><repositories><repository><id>central</id><url>http://central</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository><repository><id>thirdparty</id><url>http://thirdparty</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository><repository><id>thirdparty-snapshots</id><url>http://thirdparty-snapshots</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring</id><url>http://spring</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository><repository><id>jboss</id><url>http://jboss</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>central</id><url>http://central</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>thirdparty</id><url>http://thirdparty</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>thirdparty-snapshots</id><url>http://thirdparty-snapshots</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>spring</id><url>http://spring</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>jboss</id><url>http://jboss</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories></profile></profiles><activeProfiles><activeProfile>my-nexus</activeProfile></activeProfiles>
</settings>
10. 下载测试:
最终本地设置的repository下载了新的jar;
发布测试:
release版本发布::
snapshot版本发布:
11. nexus停止运行了,重新启动
docker start CONTAINER_ID
八. 项目打jar并发布
1. 使用springboot打包插件, 最新pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wei</groupId><artifactId>zpiece</artifactId><version>0.0.1-SNAPSHOT</version><name>zpiece</name><description>zpiece burning</description><properties><java.version>1.8</java.version><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><!-- 文件拷贝时的编码 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- 编译时的编码 --><maven.compiler.encoding>UTF-8</maven.compiler.encoding><!-- <start-class>com.ing.tpa.core.test.integration.apns.mock.MockAPNSServer</start-class>--></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><!-- fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><!-- lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.5</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--@Mapper注解发现--><dependency><groupId>org.xmlunit</groupId><artifactId>xmlunit-core</artifactId></dependency><!-- aop --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build><repositories><repository><id>alimaven</id><url>https://maven.aliyun.com/repository/public</url></repository></repositories><pluginRepositories><pluginRepository><id>alimaven</id><url>https://maven.aliyun.com/repository/public</url></pluginRepository></pluginRepositories></project>
2. 使用idea工具打包完成:
3. 上传到服务器中:
4. 在data下新建Dockerfile文件:
# 设置本镜像需要使用的基础镜像
FROM java:8 # 把jar包复制一份并改名
ADD zpiece-0.0.1-SNAPSHOT.jar /app.jar# 镜像暴露的端口, 要注意映射端口, 最好和外界启动容器端口一致
EXPOSE 6789# 容器启动命令
ENTRYPOINT ["java","-jar","/app.jar"]# 设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
完成后的Dockerfile和jar处于同一个目目录下:
5. 构建docker镜像:
docker build -t zpiece .
执行命令效果:
6. 查看docker镜像:
docker images
效果:
7. 启动
docker run -p 6789:6789 --name zpiece \
-v /mydata/zpiece/html:/usr/share/zpiece/html \
-v /mydata/zpiece/logs:/var/log/zpiece \
-d zpiece
执行命令后:
另外, 也可以设置--restart=always
表示docker容器在停止或服务器开机后会自动重新启动
docker run -d --restart=always --name zpiece -p 8080:8080 zpiece
8. 查看运行中的镜像:
# 查询所有镜像,包括未运行的, docker ps就是仅查询运行中的镜像
docker ps -a
效果:
9. 注意端口配置, docker容器启动的此端口和此服务设置的端口, 否则会连接不上;
注意端口安全组配置, 可用docker curl 343.343.443.343:6789 测试:
10. 测试:
九. nginx反向代理
待完善
十. 删除重新生成jar应用
1. 停用docker应用container
docker stop zpiece
2. 删除container:
docker rm ewrewr3234343
执行命令效果
3. 删除镜像image:
docker rmi sdfds343434
执行代码效果
4. 重新生成image
docker build -t zpiece
注意Dockerfile的内容:
# 设置本镜像需要使用的基础镜像
FROM java:8 # 把jar包复制一份并改名
ADD zpiece-0.0.1-SNAPSHOT.jar /app.jar# 镜像暴露的端口
EXPOSE 6789# 容器启动命令
ENTRYPOINT ["java","-jar","/app.jar"]# 设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone# 设置语言环境
ENV LC_ALL zh_CN.UTF-8
5. 启动目标镜像
docker run -p 6789:6789 --name zpiece \
-v /mydata/zpiece/html:/usr/share/zpiece/html \
-v /mydata/zpiece/logs:/var/log/zpiece \
-d zpiece
执行命令后: