mysql> create database tensquare_user; mysql> use tensquare_user; mysql> source /opt/tensquare_user.sql; mysql> create database tensquare_gathering; mysql> use tensquare_gathering; mysql> source /opt/tensquare_gathering.sql; ------------------------授权远程登录------------------------------------- grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option; mysql> flush privileges; quit
到此,后端连接数据库没问题了
//安装Nginx yum install epel-release yum -y install nginx //修改nginx的端口,默认80,改为9090: vim /etc/nginx/nginx.conf 38 server { 39 listen 9090; 40 listen [::]:9090; 41 server_name _; 42 root /usr/share/nginx/html; //还需要关闭selinux,将SELINUX=disabled setenforce 0 先临时关闭 vim /etc/selinux/config 编辑文件,永久关闭 SELINUX=disabled //启动Nginx systemctl enable nginx 设置开机启动 systemctl start nginx 启动 systemctl stop nginx 停止 systemctl restart nginx 重启
访问:http://192.168.10.70:9090/
Manage Jenkins-->Global Tool Configuration--> 新增NodeJS
新建 item
脚本先粘贴到流水线脚本里
红色部分需要做修改(2处)
//harbor的凭证 def git_auth="015f1ee4-73cf-4d51-95b0-2954fc32aadb" node { stage('pull code') { //切换成变量,字符串符号使用双引号 checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: 'git@192.168.10.10:xxx_group/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)]) } }
安装nodejs等待时间较长
构建成功
网站成功部署!!
上面部署方案存在的问题:
优化方案:
Jenkins+Docker+SpringCloud 集群部署流程说明
服务列表(红色的软件为需要安装的软件,黑色代表已经安装)
服务器名称 | IP地址 | 安装的软件 |
代码托管服务器 | 192.168.10.10 | Gitlab |
持续集成服务器 | 192.168.10.20 | Jenkins,Maven,Docker18.06.1-ce |
Docker仓库服务器 | 192.168.10.60 | Docker18.06.1-ce,docker-compose,Harbor1.9.2 |
生产部署服务器1 | 192.168.10.70 | Docker18.06.1-ce |
生产部署服务器2 | 192.168.10.80 | Docker18.06.1-ce |
//安装必要的软件包 yum install -y yum-utils device-mapper-persistent-data lvm2 //设置下载的镜像仓库 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install -y docker-ce systemctl stop firewalld.service systemctl disable firewalld.service setenforce 0 systemctl start docker docker -v sudo systemctl enable docker //添加阿里云镜像下载地址 镜像加速器: sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://31ycpc34.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker vim /etc/sysctl.conf net.ipv4.ip_forward=1 #添加进去 sysctl -p #立即生效 service network restart systemctl restart docker vim /etc/docker/daemon.json #指向harbor仓库地址 { "registry-mirrors": ["https://31ycpc34.mirror.aliyuncs.com"], "insecure-registries": ["192.168.10.60:85"] } systemctl restart 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.70 client: service-url: # 将自己注册到eureka-server1、eureka-server2这个Eureka上面去 defaultZone: http://192.168.10.70:10086/eureka/,http://192.168.10.80:10086/eureka/ --- server: port: 10086 spring: profiles: eureka-server2 eureka: instance: hostname: 192.168.10.80 client: service-url: defaultZone: http://192.168.10.70:10086/eureka/,http://192.168.10.80:10086/eureka/
修改红色字体部分
server: port: 10020 # 端口 # 基本服务信息 spring: application: name: tensquare-zuul # 服务ID # Eureka配置 eureka: client: service-url: defaultZone: http://192.168.10.70:10086/eureka/,http://192.168.10.80: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
server: port: 9000 spring: application: name: tensquare-admin-service #指定服务名 datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.10.20:3306/tensquare_user?characterEncoding=UTF8 username: root password: abc123 jpa: database: mysql show-sql: true #Eureka配置 eureka: client: service-url: defaultZone: http://192.168.10.70:10086/eureka/,http://192.168.10.80: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.10.20:3306/tensquare_gathering?characterEncoding=UTF8 username: root password: abc123 jpa: database: mysql show-sql: true #Eureka客户端配置 eureka: client: service-url: defaultZone: http://192.168.10.70:10086/eureka/,http://192.168.10.80:10086/eureka/ instance: lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期 prefer-ip-address: true
提交整个项目
新建 item --> tensquare_back_cluster --> Pipeline
把后端 tensquare_back 的 URL(SSH)复制粘贴到下面
字符串参数:分支名称
多选框:项目名称
tensquare_eureka_server@10086,tensquare_zuul@10020,tensquare_admin_service@9000,tensquare_gathering@9002
注册中心,服务网关,权限认证中心,活动微服务
//git的凭证 def git_auth="015f1ee4-73cf-4d51-95b0-2954fc32aadb" //git的URL def git_url="git@192.168.10.10:xxx_group/tensquare_back.git" //定义镜像标签 def tag="latest" //定义harbor的url地址 def harbor_url="192.168.10.60:85" //定义镜像仓库名 def harbor_name="tensquare" //定义harbor的凭证 def harbor_auth="8f55a28e-3be2-463d-b988-34f93063fdf2" 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++){ //项目信息 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 package images') { sh "mvn -f tensquare_common clean install" } //打包微服务项目,制作镜像,上传镜像 stage('make server') { 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)]) } }
最后构建肯定失败的,因为还没有做集群发布。主要是测试能否实现循环代码检查、 循环打包编译制作镜像。
代码检查
循环打包编译制作镜像