一、初识sonarqube
- sonarqube是一个代码质量管理平台,可通过安装不同的插件sonar可以集成测试工具、代码质量分析工具、持续集成等多种功能
- sonarqube架构主要由:sonarqube服务器、sonar数据库、插件、sonarqube scanners四部分组成
- sonarqube支持语言多达十几种具体参考官方文档:https://www.sonarqube.org/
二、sonarqube简单搭建(Linux环境搭建教程)
目前网上看到很多环境搭建资料,对于小白而言会存在一定的误导,是无法搭建完成的,下面的搭建步骤将会引导如何完成正确的搭建
1.下载jdk安装包,因为sonarqube环境是依赖JVM运行的,所以需要安装jdk
2.下载sonarqube安装包:https://www.sonarqube.org/
3.在服务器下执行vi /etc/profile,配置jdk环境变量
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
4.配置sonarqube环境变量
export SONAR_HOME=/workspace/sonar/sonarqube-7.7
export PATH=$PATH:$SNOAR_HOME/bin/linux-x86-64
5.修改${安装包路径}/bin/linux-x86-64路径下sonar.sh、wrapper文件的脚步权限(初始没有执行权限)
chmod a+x sonar.sh wrapper
6.执行sonar.sh start启动sonarqube服务,会发现浏览器无法访问http://ip:9000
7.执行netstat -ntlp |grep 9000发现没有此服务端口,ps -ef|grep sonarqube无此服务进程
8.查看${安装包路径}/logs/sonar.log文件,发现报错如下
2019.05.14 13:54:48 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /workspace/sonar/sonarqube-7.7/temp
2019.05.14 13:54:48 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
2019.05.14 13:54:48 ERROR app[][o.s.a.p.SQProcess] Fail to launch process [es]
java.lang.IllegalStateException: Fail to launch process [es]
at org.sonar.application.process.ProcessLauncherImpl.launchExternal(ProcessLauncherImpl.java:119)
at org.sonar.application.process.ProcessLauncherImpl.launch(ProcessLauncherImpl.java:87)
at org.sonar.application.SchedulerImpl.lambda$tryToStartProcess$2(SchedulerImpl.java:154)
at org.sonar.application.process.SQProcess.start(SQProcess.java:68)
at org.sonar.application.SchedulerImpl.tryToStart(SchedulerImpl.java:160)
at org.sonar.application.SchedulerImpl.tryToStartProcess(SchedulerImpl.java:152)
at org.sonar.application.SchedulerImpl.tryToStartEs(SchedulerImpl.java:110)
at org.sonar.application.SchedulerImpl.tryToStartAll(SchedulerImpl.java:102)
at org.sonar.application.SchedulerImpl.schedule(SchedulerImpl.java:98)
at org.sonar.application.App.start(App.java:64)
at org.sonar.application.App.main(App.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:240)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Cannot run program "/workspace/sonar/sonarqube-7.7/elasticsearch/bin/elasticsearch" (in directory "/workspace/sonar/sonarqube-7.7/elasticsearch"): error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at org.sonar.application.process.ProcessLauncherImpl$JavaLangProcessBuilder.start(ProcessLauncherImpl.java:321)
at org.sonar.application.process.ProcessLauncherImpl.launchExternal(ProcessLauncherImpl.java:117)
... 16 common frames omitted
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 18 common frames omitted
2019.05.14 13:54:48 INFO app[][o.s.a.SchedulerImpl] Process [es] is stopped
2019.05.14 13:54:48 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped
9.发现在${安装包路径}/elasticsearch/bin/elasticsearch启动elasticsearch服务的的时候被拒绝了
10.而后去该路径下看到elasticsearch elasticsearch-env这两个文件是没有执行权限的,遂赋执行权限
chmod a+x elasticsearch elasticsearch-env
11.而后在执行sonar.sh restart,浏览器还是无法访问http://ip:9000,查看日志
2019.05.14 14:19:52 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /workspace/sonar/sonarqube-7.7/temp
2019.05.14 14:19:52 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
2019.05.14 14:19:52 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/workspace/sonar/sonarqube-7.7/elasticsearch]: /workspace/sonar/sonarqube-7.7/elasticsearch/bin/elasticsearch
2019.05.14 14:19:52 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
2019.05.14 14:19:53 INFO app[][o.e.p.PluginsService] no modules loaded
2019.05.14 14:19:53 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019.05.14 14:20:04 INFO app[][o.s.a.SchedulerImpl] Process[es] is up
2019.05.14 14:20:04 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/workspace/sonar/sonarqube-7.7]: /usr/local/jdk/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/workspace/sonar/sonarqube-7.7/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/workspace/sonar/sonarqube-7.7/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.server.app.WebServer /workspace/sonar/sonarqube-7.7/temp/sq-process8648001503132587757properties
2019.05.14 14:20:08 INFO app[][o.s.a.SchedulerImpl] Process [web] is stopped
2019.05.14 14:20:08 WARN app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 143
2019.05.14 14:20:08 INFO app[][o.s.a.SchedulerImpl] Process [es] is stopped
2019.05.14 14:20:08 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped
<-- Wrapper Stopped
12.日志发现sonarqube服务停止,原因是root用户不能启动es,这个切记
13.创建普通用户权限
adduser sonaruser
passwd sonaruser(赋予普通用户密码)
14.给普通用户赋sonar执行权限,以及Java执行权限
chown -R sonarUser:sonaruser sonarqube-7.7
chown -R sonarUser:sonaruser jdk
15.切换到sonaruser普通用户身份,执行./sonar.sh console,输出日志发现以及正常
Running SonarQube...
wrapper | --> Wrapper Started as Console
wrapper | Launching a JVM...
jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
jvm 1 |
jvm 1 | 2018.06.23 23:55:31 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/snoar/sonarqube-7.2/temp
jvm 1 | 2018.06.23 23:55:31 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
jvm 1 | 2018.06.23 23:55:31 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/snoar/sonarqube-7.2/elasticsearch]: /opt/snoar/sonarqube-7.2/elasticsearch/bin/elasticsearch -Epath.conf=/opt/snoar/sonarqube-7.2/temp/conf/es
jvm 1 | 2018.06.23 23:55:31 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
jvm 1 | 2018.06.23 23:55:31 INFO app[][o.e.p.PluginsService] no modules loaded
jvm 1 | 2018.06.23 23:55:31 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
jvm 1 | 2018.06.23 23:55:39 INFO app[][o.s.a.SchedulerImpl] Process[es] is upprocess2095846763000389244properties
jvm 1 | 2018.06.23 23:55:39 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/opt/snoar/sonarqube-7.2]: /opt/java/jdk1.8.0_144/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/snoar/sonarqube-7.2/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/opt/snoar/sonarqube-7.2/lib/jdbc/h2/h2-1.3.176.jar org.sonar.server.app.WebServer /opt/snoar/sonarqube-7.2/temp/sq-
16 ./sonar.sh start启动sonar服务,浏览器访问http://ip:9000成功
登录web初始用户名/密码:admin/admin
17.sonar服务虽然启动成功,但仅仅是简单的启动并没有加上sonar数据库这一块,这也是我刚开始讲的sonar架构由四部分组成,所以我们还有剩下三个组成部分要完成
18.创建sonar数据库,以MySQL为例,Linux服务器下没有安装MySQL服务的参考文章
19.安装成功后进入MySQL,创建sonar数据库
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonarpassword';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonarpassword';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonarpassword';
mysql> FLUSH PRIVILEGES;
20.创建成功后,将MySQL的配置文件写入sonar.properties
#sonar JDBC连接池配置
sonar.jdbc.username=sonar
sonar.jdbc.password=sonarpassword
sonar.jdbc.url=jdbc:mysql://ip:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.driverClassName=org.gjt.mm.mysql.Driver
sonar.sorceEncoding=UTF-8
#sonarWEB登录用户密码
sonar.login=admin
sonar.password=admin
#端口不写也可以默认9000
sonar.web.port=9000
执行chmod 777 -R ${sonar路径}/sonarqube-7.7
将sonar目录全部赋予执行权限,因为初始化DB的时候还会依赖其它文件的运行,所以事先先避免这个问题
21.进入到普通用户模式,启动sonar服务即可
22.启动sonar服务之后访问web端下载checkstyle、findbugs、ChinesePack这个三个是sonar服务支持的组件
23.将这三个jar包下载完放至sonar目录下extensions/plugins,完成后重启sonar服务,访问web已经变成中文了,这是下载Chinesepack jar包效果导致
24.checkstyle、findbugs组件的作用,我就不详细诉说了网上资料一大堆,在如图地方可以设置使用findbugs组件来检测代码质量
25.最后就只有sonarqube scanners这个包需要下载安装了,我这边是参考这篇问题做了个CI的效果起到方便的效果,具体可以参考这篇文章
三、sonarqube运行原理
1.sonar服务启动会启动sonar服务,elasticsearch服务、MySQL服务
ps -ef | grep elasticsearch
ps -ef | grep soanr
ps -ef | grep mysqld
可通过以上命令查看服务进程
2.启动服务后会初始化MySQL数据库
3.使用sonar-scanners组件会执行GET请求往数据库插入数据(可通过-X来打印scanner日志),web访问代码质量也是一系列GET请求获取数据库中的返回值