一、集群部署流程说明
![](https://img.haomeiwen.com/i13587608/ba699e1e4b5e1a66.png)
环境配置
#环境配置
hostnamectl set-hostname web2-server && su
systemctl stop firewalld
systemctl disable firewalld
vim /etc/selinux/config
SELINUX=disabled
vim /etc/resolv.conf
nameserver 114.114.114.114
#安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
#设置阿里云镜像源
cd /etc/yum.repos.d/
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装 docker-ce 社区版
yum install -y docker-ce
systemctl start docker
systemctl enable docker
#配置镜像加速,官方网址可参考:https://help.aliyun.com/document_detail/60750.html
mkdir -p /etc/docker
#直接命令行输入以下内容:
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://t466r8qg.mirror.aliyuncs.com"]
}
EOF
#把Harbor地址加入到Docker信任列表(harbor仓库的docker中不需要配)
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://t466r8qg.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.8.20:85"]
}
systemctl daemon-reload
systemctl restart docker
#网络优化
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
systemctl restart network
systemctl restart docker
docker version
二、修改idea中微服务配置
2.1 注册中心配置
idea中修改注册中心eureka的配置,单节点换成集群
![](https://img.haomeiwen.com/i13587608/e0846e6badea8235.png)
# 集群版
spring:
application:
name: EUREKA-HA
---
server:
port: 10086
spring:
# 指定profile=eureka-server1
profiles: eureka-server1
eureka:
instance:
# 指定当profile=eureka-server1时,主机名是eureka-server1
hostname: 192.168.8.17
client:
service-url:
# 将自己注册到eureka-server1、eureka-server2这个Eureka上面去
defaultZone: http://192.168.8.17:10086/eureka/,http://192.168.8.16:10086/eureka/
---
server:
port: 10086
spring:
profiles: eureka-server2
eureka:
instance:
hostname: 192.168.8.16
client:
service-url:
defaultZone: http://192.168.8.17:10086/eureka/,http://192.168.8.16:10086/eureka/
在启动微服务的时候,加入参数: spring.profiles.active 来读取对应的配置
2.2 其他微服务配置
- 网关服务配置
defaultZone: http://192.168.8.17:10086/eureka/,http://192.168.8.16:10086/eureka/ # Eureka访问地址
![](https://img.haomeiwen.com/i13587608/5adb0b7aea783909.png)
- 权限管理配置
defaultZone: http://192.168.8.17:10086/eureka,http://192.168.8.16:10086/eureka
![](https://img.haomeiwen.com/i13587608/505875dd3e3777c2.png)
- 业务模块配置
defaultZone: http://192.168.8.17:10086/eureka,http://192.168.8.16:10086/eureka
![](https://img.haomeiwen.com/i13587608/ee8121345a399afb.png)
2.3 提交已修改的配置
![](https://img.haomeiwen.com/i13587608/cf753ef31a875c90.png)
![](https://img.haomeiwen.com/i13587608/9462da6f3a476598.png)
![](https://img.haomeiwen.com/i13587608/595911a550fcf7ff.png)
三、设计Jenkins集群项目的构建参数
3.1 安装Extended Choice Parameter插件
该插件可以支持多选框
![](https://img.haomeiwen.com/i13587608/4ecb732952becb5a.png)
3.2 创建流水线项目
![](https://img.haomeiwen.com/i13587608/89ee6b0b023191ac.png)
![](https://img.haomeiwen.com/i13587608/9efd121d2d81589b.png)
3.3 配置项目参数
![](https://img.haomeiwen.com/i13587608/53d715954b680086.png)
![](https://img.haomeiwen.com/i13587608/6b1dd330297a7b2d.png)
![](https://img.haomeiwen.com/i13587608/0851dec7fe82952a.png)
逗号必须得是英文,否则出错
tensquare_eureka_server@10086,tensquare_zuul@10020,tensquare_admin_service@9001,tensquare_gathering@9002
![](https://img.haomeiwen.com/i13587608/41269883a7c194d5.png)
![](https://img.haomeiwen.com/i13587608/42a970cea2925aaf.png)
3.4 测试代码循环构建
-
循环测试代码检查
![](https://img.haomeiwen.com/i13587608/90de4d5a2d33cb75.png)
![](https://img.haomeiwen.com/i13587608/5c02ee145b302d2a.png)
![](https://img.haomeiwen.com/i13587608/72a90fa992550d56.png)
![](https://img.haomeiwen.com/i13587608/2d762da9648b45a7.png)
![](https://img.haomeiwen.com/i13587608/15b8d32a185dc6d3.png)
-
循环打包编译
//添加公共子工程
stage('make install public sub project') {
sh "mvn -f tensquare_common clean install"
}
//编译打包微服务,制作镜像
stage('make package') {
for (int i=0;i<selectedProjectNames.length;i++){
//项目名称tensquare_eureka_server@10086
def projectInfo=selectedProjectNames[i]
//当前项目名称
def currentProjectName=projectInfo.split("@")[0]
//当前端口
def currentProjectPort=projectInfo.split("@")[1]
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
//定义镜像名称
def imageName="${currentProjectName}:${tag}"
//对镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}"
//镜像推送到harbor
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
//登录harbor
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//镜像上传
sh "docker push ${harbor_url}/${harbor_name}/${imageName}"
sh "echo 镜像上传成功"
}
}
//部署应用
sshPublisher(publishers: [sshPublisherDesc(configName: "master_server", transfers: [sshTransfer(cleanRemote: false, excludes: "", execCommand: "/opt/jenkins_shell/deploy.sh ${harbor_url} ${harbor_name} ${project_name} ${tag} ${port}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: "[, ]+", remoteDirectory: "", remoteDirectorySDF: false, removePrefix: "", sourceFiles: "")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
![](https://img.haomeiwen.com/i13587608/57c7d79fbb85c2df.png)
![](https://img.haomeiwen.com/i13587608/d29dad6dc2565113.png)
![](https://img.haomeiwen.com/i13587608/923016c549984bee.png)
![](https://img.haomeiwen.com/i13587608/dbc09a69343120bc.png)
![](https://img.haomeiwen.com/i13587608/370a4af43b1acce1.png)
四、完成微服务多服务器远程发布
4.1 配置远程部署服务器
从Jenkins服务器拷贝公钥到docker2远程服务器
ssh-copy-id 192.168.8.16
![](https://img.haomeiwen.com/i13587608/fc9f4f52db710e39.png)
![](https://img.haomeiwen.com/i13587608/79a22b27fbd41636.png)
4.2 Jenkins中添加远程服务器
系统配置中添加远程服务器
![](https://img.haomeiwen.com/i13587608/4e21568e12f67621.png)
![](https://img.haomeiwen.com/i13587608/c81822547c80049d.png)
4.3 修改Docker配置信任Harbor私服地址
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://t466r8qg.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.8.20:85"]
}
systemctl restart docker
4.4 项目配置参数
添加构建参数——》多选框:部署服务器
![](https://img.haomeiwen.com/i13587608/28eb08b5f3cc8ceb.png)
![](https://img.haomeiwen.com/i13587608/c29dbed229958210.png)
![](https://img.haomeiwen.com/i13587608/5dc7b8272da3c870.png)
![](https://img.haomeiwen.com/i13587608/9033b0916deb32ca.png)
4.5 修改Jenkinsfile构建脚本
//定义gitlab的凭证
def git_auth="03757112-b3bd-4955-93ef-ad84869f39a9"
//定义gitlab的URL路径
def git_url="git@192.168.8.18:gl/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的url地址
def harbor_url="192.168.8.20:85"
//镜像仓库名
def harbor_name="tensquare"
//harbor凭证
def harbor_auth="778a9460-c68d-4ab6-ab7f-f5113ec3a900"
node {
//选择的微服务项目名称
def selectedProjectNames="${project_name}".split(",")
//获取当前选择服务器
def selectedServers="${publish_server}".split(",")
stage('pull code') {
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
stage('check code') {
//循环检查
for (int i=0;i<selectedProjectNames.length;i++){
//项目名称tensquare_eureka_server@10086
def projectInfo=selectedProjectNames[i]
//当前项目名称
def currentProjectName=projectInfo.split("@")[0]
//当前端口
def currentProjectPort=projectInfo.split("@")[1]
//定义SonarQubeScanner工具
def scannerHome = tool 'sonar-scanner'
//引用SonarQube系统环境
withSonarQubeEnv('sonarqube') {
sh """
cd ${currentProjectName}
${scannerHome}/bin/sonar-scanner
"""
}
}
}
//添加公共子工程
stage('make install public sub project') {
sh "mvn -f tensquare_common clean install"
}
//编译打包微服务,制作镜像
stage('make package') {
for (int i=0;i<selectedProjectNames.length;i++){
//项目名称tensquare_eureka_server@10086
def projectInfo=selectedProjectNames[i]
//当前项目名称
def currentProjectName=projectInfo.split("@")[0]
//当前端口
def currentProjectPort=projectInfo.split("@")[1]
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
//定义镜像名称
def imageName="${currentProjectName}:${tag}"
//对镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}"
//镜像推送到harbor
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
//登录harbor
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//镜像上传
sh "docker push ${harbor_url}/${harbor_name}/${imageName}"
sh "echo 镜像上传成功"
}
//遍历所有服务器,分别部署
for (int j=0;j<selectedServers.length;j++){
//获取当前服务器名称
def currentServerName=selectedServers[j]
//调用不同服务器模块内容--spring.profiles.active=eureka-server1/eureka-server2
def activeProfile="--spring.profiles.active="
//根据不同的服务器名称调用不同的服务器配置信息
if (currentServerName=="master_server"){
activeProfile=activeProfile+"eureka-server1"
}else if (currentServerName=="slave_server"){
activeProfile=activeProfile+"eureka-server2"
}
//部署应用
sshPublisher(publishers: [sshPublisherDesc(configName: "${currentServerName}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deployCluster.sh ${harbor_url} ${harbor_name} ${currentProjectName} ${tag} ${currentProjectPort} ${activeProfile}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
推送上传gitlab
![](https://img.haomeiwen.com/i13587608/82a085ba6fb23a8f.png)
![](https://img.haomeiwen.com/i13587608/0facca4642bf602d.png)
4.6 编写部署脚本
编写deployCluster.sh部署脚本,放到两台生产服务器中路径:
/opt/jenkins_shell/deployCluster.sh
![](https://img.haomeiwen.com/i13587608/0527509a03ef173c.png)
![](https://img.haomeiwen.com/i13587608/88d08c37b4f18e5d.png)
4.7 进行构建测试
![](https://img.haomeiwen.com/i13587608/37d9b2128f28dc6e.png)
可以看到构建成功
![](https://img.haomeiwen.com/i13587608/dbc65e66b9e2b926.png)
查看容器发布结果
![](https://img.haomeiwen.com/i13587608/77acc5e936b51b6d.png)
![](https://img.haomeiwen.com/i13587608/08704bf899910ebd.png)
查看eureka注册结果
![](https://img.haomeiwen.com/i13587608/fb24ad73a4777fc3.png)
![](https://img.haomeiwen.com/i13587608/cf19c639b5e6c670.png)
4.8 构建剩下的集群微服务
![](https://img.haomeiwen.com/i13587608/7e9d9cc2f74949cd.png)
![](https://img.haomeiwen.com/i13587608/ddc9f2c19f78358c.png)
![](https://img.haomeiwen.com/i13587608/2d539fdbf45623bd.png)
![](https://img.haomeiwen.com/i13587608/4ca2d36699f341b4.png)
![](https://img.haomeiwen.com/i13587608/35e6eeff4466b0ed.png)
![](https://img.haomeiwen.com/i13587608/5b5eedac124357f8.png)
五、Nginx+Zuul 集群实现高可用网关
![](https://img.haomeiwen.com/i13587608/e442f26b737b57ad.png)
5.1 web2-server服务器上安装nginx
yum install epel-release.noarch -y
yum install -y nginx
5.2 修改nginx配置文件
vim /etc/nginx/nginx.conf
#37行出添加反向代理服务器池,42行出修改端口号
36 include /etc/nginx/conf.d/*.conf;
37 upstream zuulServer{
38 server 192.168.8.16:10020 weight=1;
39 server 192.168.8.17:10020 weight=1;
40 }
41 server {
42 listen 85;
43 listen [::]:85;
44 server_name _;
45 root /usr/share/nginx/html;
#49行出添加反向代理指向服务器池
48 include /etc/nginx/default.d/*.conf;
49 location / {
50 proxy_pass http://zuulServer/;
51 }
nginx -t
systemctl start nginx.service
![](https://img.haomeiwen.com/i13587608/15e234456c3fa8a7.png)
![](https://img.haomeiwen.com/i13587608/ac9c5224c3ecfe22.png)
5.3 修改前端Nginx的访问地址
![](https://img.haomeiwen.com/i13587608/d744967f154e687d.png)
![](https://img.haomeiwen.com/i13587608/822a45188370d7be.png)
![](https://img.haomeiwen.com/i13587608/f55cd1680189ac41.png)
![](https://img.haomeiwen.com/i13587608/4b7ba2b07b717d2d.png)
![](https://img.haomeiwen.com/i13587608/85923f3a918accca.png)
![](https://img.haomeiwen.com/i13587608/c7ce862975e7cab4.png)
5.4 Jenkins中进行前端项目构建
![](https://img.haomeiwen.com/i13587608/fb69dc7c2eea9e7a.png)
![](https://img.haomeiwen.com/i13587608/f0a0bbf0952c4f72.png)
成功访问前端数据库中的数据
![](https://img.haomeiwen.com/i13587608/083e214479f8af60.png)
参考:
https://blog.csdn.net/m0_56444183/article/details/123262744
网友评论