本文转载自微信公众号「JAVA日知录」,作者飘渺Jam。转载本文请联系JAVA日知录公众号。
模块介绍
现有 一个微服务项目,按照业务拆分为如下几个模块
- book-web 前端,采用 Vue MVVM,服务端 Thymeleaf SSR 渲染,友好 SEO MPA。服务端 路由,Spring MVC
- book-gateway 微服务网关,Spring Cloud Gateway
- book-service 接口提供方,Spring Cloud Alibaba Dubbo 服务消费方
- book-server 数据承载方,Spring Cloud Alibaba Dubbo 服务提供方
- book-common 实体,Dubbo 接口 jar
- cloud-common 微服务公共 jar
- common pom
- school-parent 最上级 父工程 pom
common 继承关系
在项目根路径下执行,Maven 命令,截取 2 段输出
- mvn clean install -pl com.lab:book-common -am -Ptest
- [INFO] Reactor Build Order:
- [INFO]
- [INFO] school_parent [pom]
- [INFO] common [pom]
- [INFO] cloud-common [jar]
- [INFO] book-common
- [INFO] Reactor Summary for school_parent 0.0.1-SNAPSHOT:
- [INFO]
- [INFO] school_parent ...................................... SUCCESS [ 0.761 s]
- [INFO] common ............................................. SUCCESS [ 0.083 s]
- [INFO] cloud-common ....................................... SUCCESS [ 6.430 s]
- [INFO] book-common ........................................ SUCCESS [ 3.236 s]
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 11.880 s
- [INFO] Finished at: 2020-05-03T14:13:12+08:00
- [INFO] ------------------------------------------------------------------------
相关中间件
docker 和 docker-compose 适合 测试环境使用,生产环境用 Kubernetes,安装 步骤命令本站基本都有:http://javadaily.cn/
- root@jazz-pc:/opt# docker ps --format "table {{.Names}} ------------- {{.Image}}"
- NAMES ------------- IMAGE
- rmqbroker ------------- apacherocketmq/rocketmq:4.5.2-alpine
- rmqnamesrv ------------- apacherocketmq/rocketmq:4.5.2-alpine
- seata-server ------------- seataio/seata-server:1.2.0
- nacos ------------- nacos/nacos-server:1.2.1
- minio ------------- minio/minio
- es ------------- elasticsearch:7.6.2
- zookeeper ------------- zookeeper
- mysql ------------- mysql:5.7
- mongo ------------- mongo
- redis ------------- redis
Alibaba Sentinel
- nohup java -Dserver.port=8858 -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=Aa123456 -jar sentinel-dashboard-1.7.2.jar >sentinel.log 2>&1 &
Maven 环境
- root@jazz-pc:/opt# mvn -v
- Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
- Maven home: /usr/local/apache-maven-3.6.3
- Java version: 11.0.7, vendor: Oracle Corporation, runtime: /usr/local/java/jdk-11.0.7
- Default locale: zh_CN, platform encoding: UTF-8
- OS name: "linux", version: "5.3.0-51-generic", arch: "amd64", family: "unix"
settings.xml 文件,加个国内镜像
- <mirror>
- <id>aliyunmaven</id>
- <mirrorOf>*</mirrorOf>
- <name>阿里云公共仓库</name>
- <url>https://maven.aliyun.com/repository/public</url>
- </mirror>
顶级父工程 pom.xml 添加,跳过 单元测试 编译和执行
- <maven.test.skip>true</maven.test.skip>
- <skipTests>true</skipTests>
如果有 Maven Nexus,则另外添加设置,install 替换为 deploy
Jenkins
- nohup java -jar /usr/local/jenkins/jenkins.war --ajp13Port=-1 --httpPort=8086 >/usr/local/jenkins/jenkins.out 2>&1 &
Jenkins JDK 环境 >=1.8 and <=11 目前最新版本不支持 Java 14
Jenkins 安装一般会卡住 2 次,则是去国外下载插件数据缓慢的原因,kill -9 进程 ID,ps -ef | grep jenkins 找到进程 ID。
第一次卡住修改 :/root/.jenkins/hudson.model.UpdateCenter.xml 文件里面链接内容为 国内镜像地址 https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
第二次卡住执行替换命令:
- cd /root/.jenkins/updates
- sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
Git or SVN
- root@jazz-pc:/opt# which git
- /usr/bin/git
- root@jazz-pc:/opt# which svn
- /usr/bin/svn
Jenkins CI/CD
- Global Tool Configuration 设置好,JDK,Maven,Git
- 新建 Item,Freestyle project,如下
Item book-common
- 源码管理,选择 Git,填写 Repository URL 和 Credentials 设置 Jenkins 凭据
- 构建 Execute shell
- #!/bin/bash
- cd ./school
- mvn clean install -pl com.lab:book-common -am -Ptest
- echo 'install ok!'
Item book-web
- 源码管理 略
- Execute shell
- #!/bin/bash
- cd ./school/book-web
- mvn clean package -Ptest
- echo 'package ok!'
- echo 'build start!'
- service_name="book-web"
- service_prot=80
- IID=$(docker images | grep "$service_name" | awk '{print $3}')
- echo "IID $IID"
- if [ -n "$IID" ]
- then
- echo "exist $service_name image,IID=$IID"
- docker rmi -f $service_name
- echo "delete $service_name image"
- docker build -t $service_name .
- echo "build $service_name image"
- else
- echo "no exist $service_name image,build docker"
- docker build -t $service_name .
- echo "build $service_name image"
- fi
- CID=$(docker ps -a | grep "$service_name" | awk '{print $1}')
- echo "CID $CID"
- if [ -n "$CID" ]
- then
- echo "exist $service_name container,CID=$CID"
- docker stop $service_name
- docker rm $service_name
- else
- echo "no exist $service_name container"
- fi
- docker run -d --name $service_name \
- -v /etc/localtime:/etc/localtime:ro \
- -v /etc/timezone:/etc/timezone:ro \
- -v /data/logs:/data/logs:rw \
- --net=host -p $service_prot:$service_prot $service_name
Item book-gateway
- 源码管理和 Execute shell 略
Item book-service
- Execute shell
- #!/bin/bash
- cd ./school/book-server
- mvn clean package -Ptest
- echo 'package ok!'
- echo 'build start!'
- service_name="book-server"
- service_prot=20880
- IID=$(docker images | grep "$service_name" | awk '{print $3}')
- echo "IID $IID"
- if [ -n "$IID" ]
- then
- echo "exist $service_name image,IID=$IID"
- docker rmi -f $service_name
- echo "delete $service_name image"
- docker build --no-cache -t $service_name .
- echo "build $service_name image"
- else
- echo "no exist $service_name image,build docker"
- docker build -t $service_name .
- echo "build $service_name image"
- fi
- CID=$(docker ps -a | grep "$service_name" | awk '{print $1}')
- echo "CID $CID"
- if [ -n "$CID" ]
- then
- echo "exist $service_name container,CID=$CID"
- docker stop $service_name
- docker rm $service_name
- else
- echo "no exist $service_name container"
- fi
- docker run -d --name $service_name \
- -e DUBBO_IP_TO_REGISTRY=192.168.1.6 \
- -e DUBBO_PORT_TO_REGISTRY=$service_prot \
- -e DUBBO_IP_TO_BIND=192.168.1.6 \
- -p $service_prot:$service_prot \
- -v /etc/localtime:/etc/localtime:ro \
- -v /etc/timezone:/etc/timezone:ro \
- -v /data/logs:/data/logs:rw \
- --net=host \
- $service_name
Dockerfile
- 位置和 pom.xml 路径平级,book-server 内容如下,其他略
- FROM adoptopenjdk/openjdk11:jdk-11.0.7_10-alpine
- VOLUME ["/tmp","/data/logs"]
- COPY ./target/book-server-0.0.1-SNAPSHOT.jar book-server.jar
- ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/book-server.jar","&"]
book-web 执行构建 Build Now
book-web 前端 浏览器访问
login.html 登录模板示例页面,调用 /api/login 接口,内容如下
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8"/>
- <title>理想生活上天猫,登录页面</title>
- <r th:insert="common/header::#headerApp"/>
- </head>
- <body>
- <div id="app">
- <el-row :gutter="20">
- <el-col :span="24"> </el-col>
- </el-row>
- <el-row :gutter="20">
- <el-col :span="1"> </el-col>
- <el-col :span="4">
- <img alt="我是一只天猫" src="//img.alicdn.com/tfs/TB11ojWRXXXXXafaFXXXXXXXXXX-190-27.png">
- </el-col>
- <el-col :span="19"> </el-col>
- </el-row>
- <el-row :gutter="20">
- <el-col :span="15">
- <img alt="九寨沟" src="/images/JiuZhaiGou.jpg" width="800" height="490">
- </el-col>
- <el-col :span="7">
- <template>
- <el-tabs v-model="activeName" @tab-click="handleClick">
- <el-tab-pane label="密码登录" name="first">
- <el-form ref="form" :model="userForm" label-width="80px">
- <el-row>
- <el-input placeholder="会员名/邮箱/手机号" prefix-icon="el-icon-user" v-model="userForm.name"></el-input>
- </el-row>
- <el-row>
- <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="userForm.password" show-password></el-input>
- </el-row>
- <el-row>
- <el-button @click="login" type="danger">登录</el-button>
- </el-row>
- </el-form>
- </el-tab-pane>
- <el-tab-pane label="短信登录" name="second">短信登录</el-tab-pane>
- </el-tabs>
- </template>
- </el-col>
- <el-col :span="2"> </el-col>
- </el-row>
- </div>
- <r th:replace="common/footer::.footerApp"/>
- <script src="/js/request.js"></script>
- <script>
- new Vue({
- el: '#app',
- data: {
- userForm:{}, activeName: 'first'
- },
- methods: {
- login() {
- request({
- url: '/api/login',method: 'post',data: this.userForm,
- headers: {
- 'content-type': 'application/json;charset=UTF-8',"token": ''
- }
- }).then(response=>{
- var res = response.data;
- this.$message(res.message);
- })
- },
- handleClick(tab, event) {
- //console.log(tab, event);
- }
- }
- });
- </script>
- </body>
- </html>