vlambda博客
学习文章列表

纯干货|微服务架构 CI/CD 实战

概述

现有 Demo,一项目某块业务拆分为如下几个模块

  • 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

本篇内容使用Jenkis实现自动构建。

common 继承关系

在项目根路径下执行,Maven 命令 mvn clean install -pl com.lab:book-common -am -Ptest,截取输出如下

[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] ------------------------------------------------------------------------

相关中间件

root@jazz-pc:/opt# docker ps --format "table {{.Names}} ------------- {{.Image}}"NAMES ------------- IMAGErmqbroker ------------- apacherocketmq/rocketmq:4.5.2-alpinermqnamesrv ------------- apacherocketmq/rocketmq:4.5.2-alpineseata-server ------------- seataio/seata-server:1.2.0nacos ------------- nacos/nacos-server:1.2.1minio ------------- minio/minioes ------------- elasticsearch:7.6.2zookeeper ------------- zookeepermysql ------------- mysql:5.7mongo ------------- mongoredis ------------- 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 -vApache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)Maven home: /usr/local/apache-maven-3.6.3Java version: 11.0.7, vendor: Oracle Corporation, runtime: /usr/local/java/jdk-11.0.7Default locale: zh_CN, platform encoding: UTF-8OS 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单元测试 编译和执行

<maven.test.skip>true</maven.test.skip><skipTests>true</skipTests>

如果有 Maven Nexus,则另外添加设置,install 替换为 deploy

Jenkins相关配置

启动Jenkis

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

cd /root/.jenkins/updatessed -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/gitroot@jazz-pc:/opt# which svn/usr/bin/svn

Jenkins CI/CD

  • Global Tool Configuration 设置好,JDK,Maven,Git

  • 新建 Item,Freestyle project,如下

image.png

Item book-common

  • 源码管理,选择 Git,填写 Repository URL 和 Credentials 设置 Jenkins 凭据

  • 构建 Execute shell

#!/bin/bashcd ./schoolmvn clean install -pl com.lab:book-common -am -Ptestecho 'install ok!'

Item book-web

  • 源码管理 略

  • Execute shell

#!/bin/bashcd ./school/book-webmvn clean package -Ptestecho 'package ok!'echo 'build start!'service_name="book-web"service_prot=80IID=$(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"fiCID=$(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_nameelse    echo "no exist $service_name container"fidocker 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/bashcd ./school/book-servicemvn clean package -Ptestecho 'package ok!'echo 'build start!'service_name="book-service"service_prot=30880IID=$(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"fiCID=$(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_nameelse    echo "no exist $service_name container"fidocker 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

Item book-server

  • Execute shell

#!/bin/bashcd ./school/book-servermvn clean package -Ptestecho 'package ok!'echo 'build start!'service_name="book-server"service_prot=20880IID=$(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"fiCID=$(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_nameelse    echo "no exist $service_name container"fidocker 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-alpineVOLUME ["/tmp","/data/logs"]COPY ./target/book-server-0.0.1-SNAPSHOT.jar book-server.jarENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/book-server.jar","&"]

book-web 执行构建 Build Now

纯干货|微服务架构 CI/CD 实战


book-web 前端 浏览器访问

纯干货|微服务架构 CI/CD 实战

image.png

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>

好了,各位朋友们,本期的内容到此就全部结束啦,能看到这里的同学都是优秀的同学,下一个升职加薪的就是你了!


纯干货|微服务架构 CI/CD 实战




觉得不错,请点个在看