大型分布式项目自动化部署管理方案 weir 2016-10-22 21:19:22.0 分布式 4889 今天要引入的话题在大型公司或者在大项目上来说也是非常重要的一部分。大家可以设想一下,如果一个项目有几十个上百个模块,有可能还是分布式的,实际物理机器可能会成倍的增加,这个时候怎么去部署这个项目以及日常维护工作。那我们自然就会想,如果有什么东西可以自动的去部署到每个台机器上那该多好,如果我更新了也可以实现自动更新和部署那就完美了。不错你能想到的都已经成为现实,这就是jenkins 和 Hudson 这两个东西本质上没有区别,甚至连界面都是大同小异,你百度谷歌一下就知道了。 我们回头在思考,我们的项目一般都会放在cvs,svn,git这些版本控制里面,存放在这些服务器里面,部署项目一般都是war包,然后放在tomcat,jetty上面才能运行。这都是比较经典的项目部署方式,我们暂且不说现在流行的spring boot。这里的处理逻辑就是假定svn上面的代码都是可运行的,首先要做的就是将svn上面的项目打包成war,这个步骤谁能做到呢?我们很容易想到maven,在国内如果直接用官网的数据源有可能黄花菜都凉了,所以我们可以搭建自己的maven私有仓库。按道理说提交的代码都是没问题但是谁又能保障呢?所以我们还需要对代码进行质量管理,这么下来我们用到的东西就不是一两个能完成的,各种不同的管理工具汇集到一起有hudson统一管理,hudson就像是指挥家或者将军一样指挥布阵的。 总结一下我们都需要什么东西: Jdk肯定跑不了,svn服务器(包括svn的管理jsvnadmin),maven及其私有仓库,代码质量管理sonar,最后加上hudson。Svn服务器可以有apache来接管,svn服务器会以模块的方式由apache来管理使用。 我们首先来安装apache,我用的是centos7.2 [root@h1 home]# yum install httpd httpd-devel 安装过程如果有什么依赖没有安装的直接yum 就可以了,另外说一下yum源最好换成国内的否则你想死的心都有,我直接就用阿里的(CentOS-Base.repo): # CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-$releasever - Base - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #released updates [updates] name=CentOS-$releasever - Updates - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #contrib - packages by Centos Users [contrib] name=CentOS-$releasever - Contrib - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 完了以后找到/etc/httpd/conf/httpd.conf 我觉得最少可以换个端口80就不要用了,具体你自己定夺。如果你还开了防火墙 那你懂的。 接下来就是安装svn模块: [root@h1 home]# yum install mod_dav_svn subversion 完了以后重启apache。运气好svn也会跟着启动,但是我没有那好运气,别急我们想看一下有没有svn了,进:[root@h1 home]# cd /etc/httpd/modules/ 看看有没有mod_dav_svn.so mod_authz_svn.so 还不放心可以[root@h1 modules]# svn –version 验证一下 接下来我们创建一个svn的主目录,用来管理所有的svn库,比如就在根目录上创建svn文件夹 下一步配置svn服务器,运气好会在/etc/httpd/conf.d 这里看到subversion.conf文件夹 LoadModule dav_svn_module modules/mod_dav_svn.so LoadModule authz_svn_module modules/mod_authz_svn.so #Include /svn/httpd.confDAV svn SVNListParentPath on SVNParentPath /svn AuthType Basic AuthName "Subversion repositories" AuthUserFile /svn/passwd.http AuthzSVNAccessFile /svn/authz Require valid-userRedirectMatch ^(/svn)$ $1/ 里面内容就这些OK。 大家会发现有这两个文件:passwd.http httpd.conf 你可以创建一个空的先记着是在你上面创建的svn目录下的。 完了以后重启apache,蛋疼我的svn还是没有跟着apache启动,我只好手动启动了 首先svn在那里启动呢,如果不出意外你可以在[root@h1 ~]# /usr/bin/svnserve 看到svnserver 这个东西 ,怎么启动在他后面加 -d -r /svn/ svn知道是怎么回事吧,就是我们上面自己创建的文件夹。 /usr/bin/svnserve -d -r /svn/ 完整命令 svn没有自启动的原因找到了: 这时候apache svn 先告一段落,接下来我们去部署svn的web管理工具svnadmin,我相信你能在网上找到这东西(http://download.csdn.net/detail/weir2008/9660343),这玩意儿需要数据库支持的mysql就够用了。创建一个数据库名然后导入下载解压文件里面的mysql5.sql和lang里面的en.Sql 即可。 然后就是把svnadmin.war 放在tomcat里面运行,第一次需要设置管理员用户和密码。 这时你会看到一个非常简单的界面: 此时要恭喜你了。 这是你再回去看我们创建的svn目录下面的几个文件有什么变化。 我们还是先创建一个svn库玩玩 按照这样来做 weir01就是 库名 提交之后: 当你点击URL的连接进入下一个页面时会看到: 认证失败,这是怎么回事,跑到文件夹里一看,可能是权限问题: 授权完一看: 再刷新页面看看 还是一样,认证失败,怎么办,回到主页面有个 设置用户组的一栏,点进去进入: 这里有三个我们首先要设置的是manager 设置用户: 这里定会有你开始时候设置的管理员,然后添加一下就行了。再回去看认证失败就没有,就说明你的这个svn库服务器就可以用了。 怎么往svn服务器端添加项目你知道么,这就很简单了 ,你安装TortoiseSVN 这个客户端了么, 这个页面怎么出来的知道么? 然后看到: 这回知道了吧。 怎么把项目放到svn呢? 右键: 加入文件夹,去选择你要上传的项目 很快就有了,这取决于你的项目文件有多少。 这是你就可以用:http://192.168.38.11:88/svn/weir01/spring-boot-redis 通过svn方式导入项目了,以后你的项目就成了有svn版本控制的了。 到这里至少你的svn服务器弄好了。 接下来我们把maven和maven私有库搞定了。Maven的安装我就不说,解压配置到系统变量中,主要是配置到系统变量因为后面可以使用maven命令实现自动化部署项目。 那么maven私有库怎么建立呢?https://www.sonatype.com/download-oss-sonatype 我用的是2.X nexus-2.14.0-01-bundle.tar.gz 当前最新的 当然最新的是3.x,你如果想挑战一下也可以,不说了 说多了都是泪。(英语差资料少结构目录变化大啊啊啊) 解压进入 主要是配置nexus.properties: 很简单 什么都不要动 把端口自己定一个就行 然后修改: 把里面的:RUN_AS_USER=root 这样就行了 启动: [root@h1 bin]# ./nexus start **************************************** WARNING - NOT RECOMMENDED TO RUN AS ROOT **************************************** Starting Nexus OSS... Started Nexus OSS. [root@h1 bin]# http://192.168.38.11:8082/nexus 默认用户名密码 admin admin123 看到这个页面我们先配置一个 3 ,其他的以后再说。 3 是proxy 所谓的代理,代理什么?代理maven官网中心 设为true,另外一个一样。 我们就设置这么多,此时我们的私有仓库就可用了。 那我们怎么使用私有仓库呢?那你还知道K:\apache-maven-3.3.9\conf\settings.xml 你可以复制一份自己取个名字,内容: K:/apache-maven-3.3.9/.m2/repositorytruefalseorg.mortbay.jettyorg.jenkins-ci.toolsnexus-releasesdeploymentdeployment123nexus-snapshotsdeploymentdeployment123weirfalse1.8nexushttp://192.168.38.11:8082/nexus/content/groups/public/truetruenexushttp://192.168.38.11:8082/nexus/content/groups/public/truetrueweir 知道了吧。然后你再eclipse配置一下上面的配置 记者一定要点击 update settings。 那接下来我们的项目怎么部署到我们的私有仓库呢?那就更简单了,只需要在pom.xml文件中添加: nexus-releasesNexus Release Repositoryhttp://192.168.38.11:8082/nexus/content/repositories/releases/nexus-snapshotsNexus Snapshot Repositoryhttp://192.168.38.11:8082/nexus/content/repositories/snapshots/ 重点是 clean install deploy 不出意外你就会在私有仓库里面看到 到这里maven和maven私有仓库都已经ok了。以后我们就可以用自己的maven仓库来管理我们的仓库,对于 nexus的其他用法大家自行百度谷歌。 接下来我们安装代码质量工具sonarqube-LTS,我安装的是5.6.2 现在应该是5.6.3。 这东西也需要数据库支持,需要在配置文件里面指定数据库 用户 密码等等。 sonar.properties [root@h1 home]# cd sonarqube-5.6.2/conf/ [root@h1 conf]# ll 总用量 16 -rw-r--r-- 1 root root 12258 9月 25 15:24 sonar.properties -rw-r--r-- 1 root root 3213 9月 19 16:34 wrapper.conf [root@h1 conf]# [root@h1 conf]# cd /home/sonarqube-5.6.2/bin/linux-x86-64/ [root@h1 linux-x86-64]# ll 总用量 128 drwxr-xr-x 2 root root 26 12月 9 2010 lib -rwxr-xr-x 1 root root 15521 9月 19 16:34 sonar.sh -rwxr-xr-x 1 root root 111027 12月 9 2010 wrapper [root@h1 linux-x86-64]# [root@h1 linux-x86-64]# ./sonar.sh start Starting SonarQube... Started SonarQube. [root@h1 linux-x86-64]# ^C [root@h1 linux-x86-64]# 启动之后就会自动初始化数据库。 打开网页 管理员密码:admin admin 这玩意儿可以汉化真是好呀:https://github.com/SonarQubeCommunity/sonar-l10n-zh 下载1.11 就够用了。 然后放在: [root@h1 sonarqube-5.6.2]# cd extensions/plugins/ [root@h1 plugins]# ll 总用量 29932 -rw-r--r-- 1 root root 128 9月 19 16:21 README.txt -rw-r--r-- 1 root root 5459683 9月 25 23:24 sonar-checkstyle-plugin-2.4.jar -rw-r--r-- 1 root root 7797781 4月 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 5866104 9月 25 23:40 sonar-java-plugin-4.2.jar -rw-r--r-- 1 root root 1678073 4月 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 36538 9月 23 13:16 sonar-l10n-zh-plugin-1.11.jar -rw-r--r-- 1 root root 3233128 4月 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 4月 7 2016 sonar-scm-svn-plugin-1.3.jar [root@h1 plugins]# 其他的配置实用 我就不啰嗦了,这里说一下maven的分析插件和怎么用eclipse来分析代码。 maven的分析插件在settings.xml中配置 也就是之前配置私有仓库的那个,在里面的profiles 里面添加: sonartrue jdbc:mysql://192.168.38.11:3306/sonar?useUnicode=true&characterEncoding=utf8 root 336393 http://192.168.38.11:9090/sonarqube 在eclipse里面分析的命令是: 重点是clean install sonar:sonar 这个里面内容很多,自己慢慢研究,这里不多说了,下面我们做最后也是最关键的安装配置hudson 前面安装maven时说过最好使用3.0.5版本,高版本会有问题。 下面安装hudson http://hudson-ci.org/ 下载war包 弄一下tomcat 在server.xml中: <="" p=""> connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> 第二./home/HudsonHome 这个要配置在系统环境在里面 HudsonHome 文件夹自己起名字 设置到环境变量中 还好hudson 是不需要数据库的,启动tomcat就可以用 刚登陆进去先注册一个管理员账户 添加插件: /home/HudsonHome 这个要配置在系统环境在里面 最好再启动hudson之前就设置好了 构建任务: 到此我们可以看到,只要是上传到svn上面的工程,我们都可以自动的去构建打包,这还不算什么最重要的是自动部署到指定的机器上面,那才是我们想要的结果,这个以后再说。