skywalking服务应用
案例准备
规划节点
IP | 主机名 | 节点 |
---|---|---|
192.168.80.101 | node-1 | Skywalking实验节点 |
192.168.80.102 | mall | 商城搭建节 |
下载软件包
curl -O http://172.30.26.138/23guosai/01-%cb%bd%d3%d0%d4%c6/skywalking.tar.gz
tar zxf skywalking.tar.gz -C /opt
案例实施
1. 部署Elasticsearch服务
(1)修改主机名
[root@localhost ~]# hostnamectl set-hostname node-1
[root@localhost ~]# bash
(2)修改Elasticsearch配置,在文件最后添加如下几行:
[root@node-1 ~]# cd /opt/skywalking
[root@node-1 skywalking]# tar zxf elasticsearch-7.17.0-linux-x86_64.tar.gz -C /opt
[root@node-1 skywalking]# cd /opt/elasticsearch-7.17.0/
[root@node-1 elasticsearch-7.17.0]# mkdir data
[root@node-1 elasticsearch-7.17.0]# vi config/elasticsearch.yml
…
cluster.name: my-application
node.name: node-1
path.data: /opt/elasticsearch-7.17.0/data
path.logs: /opt/elasticsearch-7.17.0/logs
network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"]
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
正常修改此处还要修改以下三个配置,实际测试 无需修改 使用简化修改即可
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
简化修改(竞赛方法):
…
cluster.name: my-application
node.name: node-1
path.data: /opt/elasticsearch-7.17.0/data
path.logs: /opt/elasticsearch-7.17.0/logs
network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"]
(3)创建Elasticsearch启动用户,并设置属组及权限:
[root@node-1 elasticsearch-7.17.0]# groupadd elsearch
[root@node-1 elasticsearch-7.17.0]# useradd elsearch -g elsearch -p elasticsearch
[root@node-1 elasticsearch-7.17.0]# chown -R elsearch:elsearch /opt/elasticsearch-7.17.0
(4)修改资源限制及内核配置,添加如下内容:
[root@node-1 elasticsearch-7.17.0]# vi /etc/security/limits.conf
…
* hard nofile 65536
* soft nofile 65536
[root@node-1 elasticsearch-7.17.0]# vi /etc/sysctl.conf
vm.max_map_count=262144
[root@node-1 elasticsearch-7.17.0]# sysctl -p
[root@node-1 elasticsearch-7.17.0]# reboot
重启后等待一段时间后刷新页面点击重新连接。
(5)安装java环境:
[root@node-1 skywalking]# tar -zxvf jdk-8u144-linux-x64.tar.gz -C /usr/local/
[root@node-1 ~]# vi /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_144
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
[root@node-1 ~]# source /etc/profile
[root@node-1 ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
竞赛方法装Java:
配置好yum源 使用gpmall-repo或者使用centos源安装 只要有1.8版本的java 直接安装
[root@node-1 skywalking]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
(6)启动Elasticsearch服务:
[root@node-1 ~]# cd /opt/elasticsearch-7.17.0/
[root@node-1 elasticsearch-7.17.0]# su elsearch
[elsearch@node-1 elasticsearch-7.17.0]# ./bin/elasticsearch -d
(7)查询端口,存在9200则成功启动:
[root@node-1 elasticsearch-7.17.0]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1336/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1048/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1336/master
tcp6 0 0 :::9200 :::* LISTEN 9307/java
tcp6 0 0 :::9300 :::* LISTEN 9307/java
tcp6 0 0 :::22 :::* LISTEN 1048/sshd
(8)查询端口,存在9200则成功启动:
2. 部署SkyWalking OAP服务
(1)解压文件:
[root@node-1 skywalking]# cd /opt/skywalking
[root@node-1 skywalking]# tar zxf apache-skywalking-apm-es7-8.0.0.tar.gz -C /opt
[root@node-1 skywalking]# cd /opt/apache-skywalking-apm-bin-es7/
(2)进入解压后目录,修改OAP配置文件:
[root@node-1 ~]# cd /opt/apache-skywalking-apm-bin-es7/
[root@node-1 apache-skywalking-apm-bin-es7]# vi config/application.yml
…
#集群配置使用单机版
cluster:
selector: ${SW_CLUSTER:standalone}
standalone:
…
#数据库使用elasticsearch7
storage:
selector: ${SW_STORAGE:elasticsearch7}
…
elasticsearch7:
nameSpace: ${SW_NAMESPACE:""}
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:192.168.80.101:9200}
…
(3)启动OAP服务,查询端口,存在11800与12800则成功启动:
[root@node-1 apache-skywalking-apm-bin-es7]# ./bin/oapService.sh
SkyWalking OAP started successfully!
[root@node-1 apache-skywalking-apm-bin-es7]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1336/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1048/sshd
tcp6 0 0 :::11800 :::* LISTEN 9608/java
tcp6 0 0 ::1:25 :::* LISTEN 1336/master
tcp6 0 0 :::12800 :::* LISTEN 9608/java
tcp6 0 0 :::9200 :::* LISTEN 9307/java
tcp6 0 0 :::9300 :::* LISTEN 9307/java
tcp6 0 0 :::22 :::* LISTEN 1048/sshd
3. 部署SkyWalking UI服务
(1)由于SkyWalking UI的默认地址是8080,与很多中间件可能存在冲突,修改一下:
[root@node-1 apache-skywalking-apm-bin-es7]# vi webapp/webapp.yml
…
server:
port: 8888
…
(2)启动SkyWalking UI服务:
[root@node-1 apache-skywalking-apm-bin-es7]# ./bin/webappService.sh
SkyWalking Web Application started successfully!
(3)查看端口,存在8888则成功启动:
[root@node-1 apache-skywalking-apm-bin-es7]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1081/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1041/master
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 610/rpcbind
tcp6 0 0 :::9300 :::* LISTEN 2261/java
tcp6 0 0 :::22 :::* LISTEN 1081/sshd
tcp6 0 0 :::8888 :::* LISTEN 3133/java
tcp6 0 0 :::11800 :::* LISTEN 2416/java
tcp6 0 0 ::1:25 :::* LISTEN 1041/master
tcp6 0 0 :::12800 :::* LISTEN 2416/java
tcp6 0 0 :::111 :::* LISTEN 610/rpcbind
tcp6 0 0 :::9200 :::* LISTEN 2261/java
进入浏览器查看:
4. 搭建并启动应用商城服务
切换至提供mall节点,搭建并启动应用商城服务,并配置SkyWalking Agent
(1)修改mall节点主机名,修改完成后,单击实验环境左侧工具栏最后一个重连按钮以生效新主机名。
[root@localhost ~]# hostnamectl set-hostname mall
修改/etc/hosts配置文件如下:
[root@mall ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.102 mall
(2)配置本地Yum源
将提供的gpmall包从node节点传输到mall节点的/root目录下并解压gpmall.tar.gz,配置本地local.repo文件,具体代码如下所示:
[root@node-1 skywalking]# scp -r gpmall 192.168.80.102:/opt
[root@mall ~]# tar -zxvf gpmall.tar.gz
[root@mall ~]# mv /etc/yum.repos.d/* /media/
[root@mall ~]# cd /opt/gpmall/
[root@mall gpmall]# tar zxf gpmall-repo.tar.gz -C /opt
[root@mall ~]# vi /etc/yum.repos.d/local.repo
[mall]
name=mall
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
(3)安装基础服务
安装基础服务,包括Java JDK环境、数据库、Redis、Nginx等,安装基础服务的命令具体如下。
① 安装Java环境
[root@mall ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
...
[root@mall ~]# java -version
openjdk version "1.8.0_322"
OpenJDK Runtime Environment (build 1.8.0_322-b06)
OpenJDK 64-Bit Server VM (build 25.322-b06, mixed mode)
② 安装Redis缓存服务
[root@mall ~]# yum install redis -y
③ 安装Nginx服务
[root@mall ~]# yum install nginx -y
④ 安装MariaDB数据库
[root@mall ~]# yum install mariadb mariadb-server -y
安装ZooKeeper服务,将提供的zookeeper-3.4.14.tar.gz解压,命令如下:
[root@mall gpmall]# tar -zxvf zookeeper-3.4.14.tar.gz -C /opt
进入到zookeeper-3.4.14/conf目录下,将zoo_sample.cfg文件重命名为zoo.cfg,命令如下:
[root@mall gpmall]# cd /opt/zookeeper-3.4.14/conf/
[root@mall conf]# mv zoo_sample.cfg zoo.cfg
进入到zookeeper-3.4.14/bin目录下,启动ZooKeeper服务,命令如下:
[root@mall conf]# cd ../bin
[root@mall bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看ZooKeeper状态,命令如下:
[root@mall bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone
安装Kafka服务,将提供的kafka_2.11-1.1.1.tgz解压至/root目录下,命令如下:
[root@mall bin]# tar -zxvf /opt/gpmall/kafka_2.11-1.1.1.tgz -C /opt
进入到kafka_2.11-1.1.1/bin目录下,启动Kafka服务,命令如下:
[root@mall bin]# cd /opt/kafka_2.11-1.1.1/bin/
[root@mall bin]# ./kafka-server-start.sh -daemon ../config/server.properties
使用jps或者netstat -ntpl命令查看Kafka是否成功启动,命令如下:
[root@mall bin]# jps
6039 Kafka
1722 QuorumPeerMain
6126 Jps
[root@mall bin]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1400/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1082/sshd
tcp6 0 0 :::43993 :::* LISTEN 8895/java
tcp6 0 0 ::1:25 :::* LISTEN 1400/master
tcp6 0 0 :::9092 :::* LISTEN 8895/java
tcp6 0 0 :::37956 :::* LISTEN 8582/java
tcp6 0 0 :::2181 :::* LISTEN 8582/java
tcp6 0 0 :::22 :::* LISTEN 1082/sshd
运行结果查看到Kafka服务和9092端口,说明Kafka服务已启动。
(4)启动服务
① 启动数据库并配置
修改数据库配置文件并启动MariaDB数据库,设置root用户密码为123456,并创建gpmall数据库,将提供的gpmall.sql导入。
修改/etc/my.cnf文件,添加字段如下所示:
#实际测试:可不做此步骤,竞赛方法跳过
[root@mall bin]# cd
[root@mall ~]# vi /etc/my.cnf
[mysqld]
…
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
启动数据库命令如下。
[root@mall ~]# systemctl start mariadb && systemctl enable mariadb
设置root用户的密码为123456并登录。
[root@mall ~]# mysqladmin -uroot password 123456
[root@mall ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
设置root用户的权限,命令如下:
MariaDB [(none)]> grant all privileges on *.* to root@localhost identified by '123456' with grant option;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant all privileges on *.* to root@"%" identified by '123456' with grant option;
Query OK, 0 rows affected (0.001 sec)
快速给权限方法(竞赛用):
MariaDB [(none)]> grant all privileges on *.* to '*'@"%" identified by '123456' with grant option;
Query OK, 0 rows affected (0.001 sec)
创建数据库gpmall并导入/root/gpmall/目录中的gpmall.sql文件。
MariaDB [(none)]> create database gpmall;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use gpmall;
MariaDB [gpmall]> source /opt/gpmall/gpmall.sql
退出数据库并设置开机自启。
MariaDB [gpmall]> exit
Bye
[root@mall ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/mysqld.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
② 启动Redis服务
修改Redis配置文件,编辑/etc/redis.conf文件。
将bind 127.0.0.1这一行注释掉;将protected-mode yes 改为 protected-mode no。
启动Redis服务命令如下。
[root@mall ~]# vi /etc/redis.conf
…
#bind 127.0.0.1
protected-mode no
…
[root@mall ~]# systemctl start redis
[root@mall ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
③ 启动Nginx服务
启动Nginx服务命令如下。
[root@mall ~]# systemctl start nginx
[root@mall ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
(5)应用系统部署
使用提供gpmall-shopping-0.0.1-SNAPSHOT.jar、gpmall-user-0.0.1-SNAPSHOT.jar、shopping-provider-0.0.1-SNAPSHOT.jar、user-provider-0.0.1-SNAPSHOT.jar 、dist这5个包部署应用系统,其中4个jar包为后端服务包,dist为前端包。(包在gpmall目录下)
① 全局变量配置
修改/etc/hosts文件,修改项目全局配置文件如下(原有的172.128.11.42 mall映射删除):
[root@mall ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.102 mall
192.168.80.102 kafka.mall
192.168.80.102 mysql.mall #竞赛时使用127.0.0.1 mysql.mall 以免出问题
192.168.80.102 redis.mall
192.168.80.102 zookeeper.mall
② 部署前端
清空默认项目路径下的文件,将dist目录下的文件,复制到Nginx默认项目路径(文件在gpmall目录下)。
[root@mall ~]# rm -rf /usr/share/nginx/html/*
[root@mall ~]# cp -rvf gpmall/dist/* /usr/share/nginx/html/
修改Nginx配置文件/etc/nginx/nginx.conf,添加映射如下所示:
[root@mall ~]# vi /etc/nginx/nginx.conf
…
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /user {
proxy_pass http://127.0.0.1:8082;
}
location /shopping {
proxy_pass http://127.0.0.1:8081;
}
location /cashier {
proxy_pass http://127.0.0.1:8083;
}
error_page 404 /404.html;
…
重启Nginx服务,命令如下:
[root@mall ~]# systemctl restart nginx
到此,前端部署完毕。
③ 部署后端
将node-1节点的/opt/apache-skywalking-apm-bin-es7目录下的agent目录复制到mall节点下:
[root@mall ~]# scp -r 192.168.80.101:/opt/apache-skywalking-apm-bin-es7/agent /opt
修改SkyWalking agent配置文件:
[root@mall ~]# vi agent/config/agent.config
…
agent.service_name=${SW_AGENT_NAME:my-application}
agent.sample_n_per_3_secs=${SW_AGENT_SAMPLE:1}
…
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.80.101:11800}
…
采样率修改:agent.sample_n_per_3_secs配置说明:
在访问量较少时,链路全量收集不会对系统带来太大负担,能够完整的观测到系统的运行状况。但是在访问量较大时,全量的链路收集,对链路收集的客户端(agent探针)、服务端(SkyWalking OAP)、存储器(例如说Elastcsearch)都会带来较大的性能开销,甚至会影响应用的正常运行。在访问量级较大的情况下,往往会选择抽样采样,只收集部分链路信息。SkyWalking Agent在agent/config/agent.config 配置文件中,定义了agent.sample_n_per_3_secs配置项,设置每3秒可收集的链路数据的数量。
将gpmall软件包中提供的4个jar包,放置探针并启动,通过设置启动参数的方式检测系统,启动命令如下:
[root@mall ~]# nohup java -javaagent:/opt/agent/skywalking-agent.jar -jar /opt/gpmall/shopping-provider-0.0.1-SNAPSHOT.jar &
[1] 20086
[root@mall ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@mall ~]# nohup java -javaagent:/opt/agent/skywalking-agent.jar -jar /opt/gpmall/user-provider-0.0.1-SNAPSHOT.jar &
[2] 20132
[root@mall ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@mall ~]# nohup java -javaagent:/opt/agent/skywalking-agent.jar -jar /opt/gpmall/gpmall-shopping-0.0.1-SNAPSHOT.jar &
[3] 20177
[root@mall ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@mall ~]# nohup java -javaagent:/opt/agent/skywalking-agent.jar -jar /opt/gpmall/gpmall-user-0.0.1-SNAPSHOT.jar &
[4] 20281
[root@mall ~]# nohup: ignoring input and appending output to ‘nohup.out’
# 让网络畅通
[root@mall ~]# setenforce 0
[root@mall ~]# systemctl stop firewalld
按照顺序运行4个jar包后,至此后端服务部署完毕。
④ 网站访问
打开浏览器,在地址栏中输入http://192.168.80.102,访问界面,如图所示。
查看skywalking识别状态 出现拓补图 代表成功
GPMall 商城系统 遇到的问题及解决方案
暂时关闭selinux 关闭防火墙
setenforce 0
getenforce
systemctl stop firewalld
关闭防火墙服务
systemctl stop firewalld
systemctl disable firewalld
问题1
启动失败 异常退出 原因: java运行jar包同时运行或者没有按照顺序运行
问题二
关于kafka服务(启动kafka后,自动关闭的问题),在 /etc/hosts 文件中添加mall主机名和ip映射,启动kafka服务,再查看就不会自动关闭kafka服务了。
vi /etc/hosts
主机IP mall
主机IP kafka.mall
主机IP mysql.mall
主机IP redis.mall
主机IP zookeeper.mall
问题三
当用命令 jobs 查看jar包运行状态或访问网站只显示个标题(1+x 示例项目)的时候,那就是4个jar包没运行成功。
查看之前的服务都启动了没有,特别是kafka服务;再重新运行4个jar包(需按照顺序运行),然后再jobs查看验证启动运行了没有,jobs查看后是运行状态,然后去刷新一下网页。
nohup java -jar /root/shopping-provider-0.0.1-SNAPSHOT.jar &
nohup java -jar /root/user-provider-0.0.1-SNAPSHOT.jar &
nohup java -jar /root/gpmall-shopping-0.0.1-SNAPSHOT.jar &
nohup java -jar /root/gpmall-user-0.0.1-SNAPSHOT.jar &
jobs -l
问题四
关于数据库问题(访问网站能显示界面但是页面内容加载不出来),当用主机IP mysql.mall主机名和ip映射的时候,访问网站一直显示“加载中”;当把主机名ip映射改为127.0.0.1 mysql.mall,再去刷新网页,就能成功访问页面。(个人认为:页面一直显示加载中,说明数据库没连上,连上了的话就会显示页面的内容)
vi /etc/hosts
主机IP mall
主机IP kafka.mall
127.0.0.1 mysql.mall
主机IP redis.mall
主机IP zookeeper.mall
问题五
有些问题可能是数据库和nginx服务没重启导致的,需重启数据库和nginx服务。
systemctl restart mariadb
systemctl restart nginx
jps
查看kafka服务
jobs
查看Jar包运行状态
ps aux |grep java
查看java进程
jobs -l
查看jar包进程PID号
kill -9 PID号
杀死指定PID号进程
tail -f /var/log/nginx/error.log
查看nginx的error(错误)日志
nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
重新启动gpmall-user的jar包(例子)
tailf -f nohup.out