上述部署方案存在部分问题:
优化方案:
安装 Docker:
#环境配置
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
#直接命令行输入以下内容:
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://wrdun890.mirror.aliyuncs.com"]
}
EOF
#把Harbor地址加入到Docker信任列表(harbor仓库的docker中不需要配)
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://wrdun890.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.74.7: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
# 集群版
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.74.12
client:
service-url:
# 将自己注册到eureka-server1、eureka-server2这个Eureka上面去
defaultZone: http://192.168.74.12:10086/eureka/,http://192.168.74.6:10086/eureka/
---
server:
port: 10086
spring:
profiles: eureka-server2
eureka:
instance:
hostname: 192.168.74.6
client:
service-url:
defaultZone: http://192.168.74.12:10086/eureka/,http://192.168.74.6:10086/eureka/
server:
port: 9001
spring:
application:
name: tensquare-admin-service #指定服务名
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.74.8:3306/tensquare_user?characterEncoding=UTF8
username: root
password: abc123
jpa:
database: mysql
show-sql: true
#Eureka配置
eureka:
client:
service-url:
defaultZone: http://192.168.74.12:10086/eureka/,http://192.168.74.6:10086/eureka/
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
prefer-ip-address: true
# jwt参数
jwt:
config:
key: itcast
ttl: 1800000
server:
port: 9002
spring:
application:
name: tensquare-gathering #指定服务名
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.74.8:3306/tensquare_gathering?characterEncoding=UTF8
username: root
password: abc123
jpa:
database: mysql
show-sql: true
#Eureka客户端配置
eureka:
client:
service-url:
defaultZone: http://192.168.74.12:10086/eureka/,http://192.168.74.6:10086/eureka/
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
prefer-ip-address: true
server:
port: 10020 # 端口
# 基本服务信息
spring:
application:
name: tensquare-zuul # 服务ID
# Eureka配置
eureka:
client:
service-url:
defaultZone: http://192.168.74.12:10086/eureka/,http://192.168.74.6:10086/eureka/ # Eureka访问地址
instance:
prefer-ip-address: true
# 修改ribbon的超时时间
ribbon:
ConnectTimeout: 1500 # 连接超时时间,默认500ms
ReadTimeout: 3000 # 请求超时时间,默认1000ms
# 修改hystrix的熔断超时时间
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMillisecond: 2000 # 熔断超时时长,默认1000ms
# 网关路由配置
zuul:
routes:
admin:
path: /admin/**
serviceId: tensquare-admin-service
gathering:
path: /gathering/**
serviceId: tensquare-gathering
# jwt参数
jwt:
config:
key: itcast
ttl: 1800000
循环测试代码检查
修改Jenkinsfile文件
//git的凭证
def git_auth="0aa6d8b5-b2c8-4a66-8b76-a9d7d16c5bd5"
//git的URL
def git_url="[email protected]:kgc/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的url地址
def harbor_url="192.168.74.7:85"
//镜像仓库名
def harbor_name="tensquare"
//定义harbor的凭证
def harbor_auth="49a8e73a-5c5f-483f-b6f1-78c15a1adf7f"
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)])
}
}
}
}
从Jenkins服务器拷贝公钥到docker2远程服务器
ssh-copy-id 192.168.74.6
系统配置中添加远程服务器
添加构建参数——》多选框:部署服务器
//git的凭证
def git_auth="0aa6d8b5-b2c8-4a66-8b76-a9d7d16c5bd5"
//git的URL
def git_url="[email protected]:kgc/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的url地址
def harbor_url="192.168.74.7:85"
//镜像仓库名
def harbor_name="tensquare"
//定义harbor的凭证
def harbor_auth="49a8e73a-5c5f-483f-b6f1-78c15a1adf7f"
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
编写deployCluster.sh部署脚本,放到两台生产服务器中路径:
/opt/jenkins_shell/deployCluster.sh
cd /opt/jenkins_shell/
#把脚本拉进去
chmod +x deployCluster.sh #给权限
yum install epel-release.noarch -y
yum install -y nginx
vim /etc/nginx/nginx.conf
#37行出添加反向代理服务器池,42行出修改端口号
include /etc/nginx/conf.d/*.conf;
upstream zuulServer{
server 192.168.74.11:10020 weight=1;
server 192.168.74.6:10020 weight=1;
}
server {
listen 85;
listen [::]:85;
server_name _;
root /usr/share/nginx/html;
#49行出添加反向代理指向服务器池
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://zuulServer/;
}
nginx -t
systemctl start nginx.service