一、部署其他微服务
注册中心eureka服务配置指定生产服务器地址eureka_server
服务网关配置指定生产服务器地址zuul
认证中心配置,数据库使用Jenkins中的数据库admin
活动微服务配置gathering
修改完成进行提交
二、数据库脚本导入Jenkins的数据库中
授权远程登陆
grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
更新
mysql> flush privileges;
回到Jenkins,直接编译
成功注册
三、使用postman测试后台数据连接
token:"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMTk0MjgxNTMzMjMwNDE5OTY4Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE2NDY1ODMzNzQsInJvbGVzIjoiYWRtaW4iLCJleHAiOjE2NDY1ODUxNzR9.DwJnrQCdqLvzFjKCQkXBfwh1BRW1s87qIO33INHD5QI"
四、部署静态WEB网站
安装NGINX服务器
yum install epel-release
yum -y install nginx
vi /etc/nginx/nginx.conf
server {
listen 9090 default_server;
listen [::]:9090 default_server;
server_name _;
root /usr/share/nginx/html;
setenforce 0 先临时关闭
vi /etc/selinux/config 编辑文件,永久关闭 SELINUX=disabled
systemctl enable nginx 设置开机启动
systemctl start nginx 启动
systemctl stop nginx 停止
systemctl restart nginx 重启
记得关闭防火墙
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Jenkins----安装nodejs插件
//harbor的凭证
def git_auth="fc695d81-0a6f-4319-96b0-6c4cede451ec"
node {
stage('pull code') {
//切换成变量,字符串符号使用双引号
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: 'git@192.168.10.129:root/tensquare_front.git']]])
}
stage('make package,deploy') {
//使用nodejs的npm打包
nodejs('nodejs12'){
sh '''
npm install
npm run build
'''
}
//远程部署
sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/usr/share/nginx/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
点击推送----点击push
五、集群版
部署第二台生产环境(记得装docker)
部署集群版
# 集群版
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.10.132
client:
service-url:
# 将自己注册到eureka-server1、eureka-server2这个Eureka上面去
defaultZone: http://192.168.10.132:10086/eureka/,http://192.168.10.133:10086/eureka/
---
server:
port: 10086
spring:
profiles: eureka-server2
eureka:
instance:
hostname: 192.168.10.133
client:
service-url:
defaultZone: http://192.168.10.132:10086/eureka/,http://192.168.10.133:10086/eureka/
admin
gathering
zuul
tensquare_eureka_server@10086,tensquare_zuul@10020,tensquare_admin_service@9001,tensquare_gathering@9002
效果
def git_auth="fc695d81-0a6f-4319-96b0-6c4cede451ec"
def git_url="git@192.168.10.129:root/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的URL地址
def harbor_url="192.168.10.131:85"
//镜像仓库名
def harbor_name="tensquare"
def harbor_auth="d20e5cb3-d4df-4720-ab78-88935e7db1ec"
node {
//获取当前选择项目名称
def selectedProjectNames="${project_name}".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++){
//项目信息
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 ') {
sh "mvn -f ${project_name} clean package dockerfile:build"
//镜像名称
def imageName="${project_name}:${tag}"
//对镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}"
//镜像推送到harbor
withCredentials([usernamePassword(credentialsId: 'd20e5cb3-d4df-4720-ab78-88935e7db1ec', 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)])
}
}
def git_auth="fc695d81-0a6f-4319-96b0-6c4cede451ec"
def git_url="git@192.168.10.129:root/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的URL地址
def harbor_url="192.168.10.131:85"
//镜像仓库名
def harbor_name="tensquare"
def harbor_auth="d20e5cb3-d4df-4720-ab78-88935e7db1ec"
node {
//获取当前选择项目名称
def selectedProjectNames="${project_name}".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++){
//项目信息
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 ${project_name}
${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++){
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: 'd20e5cb3-d4df-4720-ab78-88935e7db1ec', 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)])
}
}
六、微服务多服务器远程发布
配置远程部署服务器
从Jenkins服务器拷贝公钥到docker2远程服务器ssh
-copy-id 192.168.10.133
最终效果
idea
def git_auth="fc695d81-0a6f-4319-96b0-6c4cede451ec"
def git_url="git@192.168.10.129:root/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的URL地址
def harbor_url="192.168.10.131:85"
//镜像仓库名
def harbor_name="tensquare"
def harbor_auth="d20e5cb3-d4df-4720-ab78-88935e7db1ec"
node {
//获取当前选择项目名称
def selectedProjectNames="${project_name}".split(",")
def selectedServerNames="${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++){
//项目信息
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++){
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: 'd20e5cb3-d4df-4720-ab78-88935e7db1ec', 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<selectedServerNames.length;j++){
def currentServerName=selectedServerNames[j]
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)])
}
}
}
}
web_server2 记得装好NGINX VIM /etc/nginx/nginx.conf