by wangpwm

背景信息

SkyWalking是一款针对分布式系统的国产APM(Application Performance Monitoring,应用性能监控)产品,主要针对微服务、Cloud Native和容器化(Docker、Kubernetes、Mesos)架构的应用。SkyWalking的核心是一个分布式追踪系统,目前已进入Apache 。

要通过SkyWalking将Java应用数据上报至链路追踪控制台,首先需要完成埋点工作。SkyWalking既支持自动探针(Dubbo、gRPC、JDBC、OkHttp、Spring、Tomcat、Struts、Jedis等),也支持手动埋点(OpenTracing)。本文介绍安装和自动动埋点方法。

安装 elasticsearch 7

下载

  1. wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.6.2/elasticsearch-7.6.2-x86_64.rpm

安装

  1. yum install -y elasticsearch-7.6.2-x86_64.rpm

修改配置中目录的用户与用户组,不然无法启动

  1. groupadd elasticsearch
  2. useradd elasticsearch -g elasticsearch -p peter@2020
  3. chown -R elasticsearch:elasticsearch /var/lib/elasticsearch
  4. chown -R elasticsearch:elasticsearch /var/log/elasticsearch
  1. chmod g+w /etc/elasticsearch

修改 elasticsearch 的配置

  1. # ---------------------------------- Cluster -----------------------------------
  2. #
  3. # Use a descriptive name for your cluster:
  4. #
  5. cluster.name: skywalking-application
  6. #
  7. # ------------------------------------ Node ------------------------------------
  8. #
  9. # Use a descriptive name for the node:
  10. #
  11. node.name: skywalking-node-1
  12. #
  13. # Add custom attributes to the node:
  14. #
  15. #node.attr.rack: r1
  16. #
  17. # ----------------------------------- Paths ------------------------------------
  18. #
  19. # Path to directory where to store the data (separate multiple locations by comma):
  20. #
  21. path.data: /var/lib/elasticsearch
  22. #
  23. # Path to log files:
  24. #
  25. path.logs: /var/log/elasticsearch
  26. #
  27. # ----------------------------------- Memory -----------------------------------
  28. #
  29. # Lock the memory on startup:
  30. #
  31. #bootstrap.memory_lock: true
  32. #
  33. # Make sure that the heap size is set to about half the memory available
  34. # on the system and that the owner of the process is allowed to use this
  35. # limit.
  36. #
  37. # Elasticsearch performs poorly when the system is swapping the memory.
  38. #
  39. # ---------------------------------- Network -----------------------------------
  40. #
  41. # Set the bind address to a specific IP (IPv4 or IPv6):
  42. #
  43. network.host: 0.0.0.0
  44. #
  45. # Set a custom port for HTTP:
  46. #
  47. http.port: 9200
  48. #
  49. # For more information, consult the network module documentation.
  50. #
  51. # --------------------------------- Discovery ----------------------------------
  52. #
  53. # Pass an initial list of hosts to perform discovery when this node is started:
  54. # The default list of hosts is ["127.0.0.1", "[::1]"]
  55. #
  56. discovery.seed_hosts: ["127.0.0.1"]
  57. #
  58. # Bootstrap the cluster using an initial set of master-eligible nodes:
  59. #
  60. cluster.initial_master_nodes: ["skywalking-node-1"]
  61. #
  62. # For more information, consult the discovery and cluster formation module documentation.
  63. #
  64. # ---------------------------------- Gateway -----------------------------------
  65. #
  66. # Block initial recovery after a full cluster restart until N nodes are started:
  67. #
  68. #gateway.recover_after_nodes: 3
  69. #
  70. # For more information, consult the gateway module documentation.
  71. #
  72. # ---------------------------------- Various -----------------------------------
  73. #
  74. # Require explicit names when deleting indices:
  75. #
  76. #action.destructive_requires_name: true

启动 elasticsearch

  1. sudo systemctl daemon-reload
  2. sudo systemctl enable elasticsearch.service
  3. sudo systemctl start elasticsearch.service

查看是否启动成功

  • 验证是否启动成功
  1. curl http://localhost:9200/

成功会看到如下信息

  1. {
  2. "name" : "skywalking-node-1",
  3. "cluster_name" : "skywalking-application",
  4. "cluster_uuid" : "6TSWaZJ-SaKhpQbUh4TMQw",
  5. "version" : {
  6. "number" : "7.0.0",
  7. "build_flavor" : "default",
  8. "build_type" : "rpm",
  9. "build_hash" : "b7e28a7",
  10. "build_date" : "2019-04-05T22:55:32.697037Z",
  11. "build_snapshot" : false,
  12. "lucene_version" : "8.0.0",
  13. "minimum_wire_compatibility_version" : "6.7.0",
  14. "minimum_index_compatibility_version" : "6.0.0-beta1"
  15. },
  16. "tagline" : "You Know, for Search"
  17. }
  • 查看 启动失败的信息
  1. systemctl status elasticsearch.service
  2. journalctl -xe
  • 安装失败后,可以用下面命令进行卸载
  1. yum remove elasticsearch-7.6.2-x86_64.rpm

skyWalking

下载

  1. wget https://mirrors.tuna.tsinghua.edu.cn/apache/skywalking/7.0.0/apache-skywalking-apm-es7-7.0.0.tar.gz

解压

  1. mkdir -p /opt/apache/skywalking
  2. mv apache-skywalking-apm-es7-7.0.0.tar.gz /opt/apache/skywalking
  3. cd /opt/apache/skywalking
  4. tar -zxvf apache-skywalking-apm-es7-7.0.0.tar.gz

修改配置文件

  1. vim /opt/apache/skywalking/apache-skywalking-apm-bin-es7/config/application.yml

只需关注如下内容,原配置文件的其他内容无需更改

  1. ... ##
  2. storage:
  3. selector: elasticsearch7
  4. elasticsearch:
  5. nameSpace: ${SW_NAMESPACE:"skywalking-application"}
  6. clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:127.0.0.1:9200}
  7. protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
  8. trustStorePath: ${SW_SW_STORAGE_ES_SSL_JKS_PATH:"../es_keystore.jks"}
  9. trustStorePass: ${SW_SW_STORAGE_ES_SSL_JKS_PASS:""}
  10. user: ${SW_ES_USER:""}
  11. password: ${SW_ES_PASSWORD:""}
  12. secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool.
  13. enablePackedDownsampling: ${SW_STORAGE_ENABLE_PACKED_DOWNSAMPLING:true} # Hour and Day metrics will be merged into minute index.
  14. dayStep: ${SW_STORAGE_DAY_STEP:1} # Represent the number of days in the one minute/hour/day index.
  15. indexShardsNumber: ${SW_STORAGE_ES_INDEX_SHARDS_NUMBER:2}
  16. indexReplicasNumber: ${SW_STORAGE_ES_INDEX_REPLICAS_NUMBER:0}
  17. # Those data TTL settings will override the same settings in core module.
  18. recordDataTTL: ${SW_STORAGE_ES_RECORD_DATA_TTL:7} # Unit is day
  19. otherMetricsDataTTL: ${SW_STORAGE_ES_OTHER_METRIC_DATA_TTL:45} # Unit is day
  20. monthMetricsDataTTL: ${SW_STORAGE_ES_MONTH_METRIC_DATA_TTL:18} # Unit is month
  21. # Batch process setting, refer to https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-bulk-processor.html
  22. bulkActions: ${SW_STORAGE_ES_BULK_ACTIONS:1000} # Execute the bulk every 1000 requests
  23. flushInterval: ${SW_STORAGE_ES_FLUSH_INTERVAL:10} # flush the bulk every 10 seconds whatever the number of requests
  24. concurrentRequests: ${SW_STORAGE_ES_CONCURRENT_REQUESTS:2} # the number of concurrent requests
  25. resultWindowMaxSize: ${SW_STORAGE_ES_QUERY_MAX_WINDOW_SIZE:10000}
  26. metadataQueryMaxSize: ${SW_STORAGE_ES_QUERY_MAX_SIZE:5000}
  27. segmentQueryMaxSize: ${SW_STORAGE_ES_QUERY_SEGMENT_SIZE:200}
  28. profileTaskQueryMaxSize: ${SW_STORAGE_ES_QUERY_PROFILE_TASK_SIZE:200}
  29. advanced: ${SW_STORAGE_ES_ADVANCED:""}
  30. elasticsearch7:
  31. nameSpace: ${SW_NAMESPACE:"skywalking-application"}
  32. clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:127.0.0.1:9200}
  33. protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
  34. trustStorePath: ${SW_SW_STORAGE_ES_SSL_JKS_PATH:"../es_keystore.jks"}
  35. trustStorePass: ${SW_SW_STORAGE_ES_SSL_JKS_PASS:""}
  36. enablePackedDownsampling: ${SW_STORAGE_ENABLE_PACKED_DOWNSAMPLING:true} # Hour and Day metrics will be merged into minute index.
  37. dayStep: ${SW_STORAGE_DAY_STEP:1} # Represent the number of days in the one minute/hour/day index.
  38. user: ${SW_ES_USER:""}
  39. password: ${SW_ES_PASSWORD:""}
  40. secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool.
  41. indexShardsNumber: ${SW_STORAGE_ES_INDEX_SHARDS_NUMBER:2}
  42. indexReplicasNumber: ${SW_STORAGE_ES_INDEX_REPLICAS_NUMBER:0}
  43. # Those data TTL settings will override the same settings in core module.
  44. recordDataTTL: ${SW_STORAGE_ES_RECORD_DATA_TTL:7} # Unit is day
  45. otherMetricsDataTTL: ${SW_STORAGE_ES_OTHER_METRIC_DATA_TTL:45} # Unit is day
  46. monthMetricsDataTTL: ${SW_STORAGE_ES_MONTH_METRIC_DATA_TTL:18} # Unit is month
  47. # Batch process setting, refer to https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-bulk-processor.html
  48. bulkActions: ${SW_STORAGE_ES_BULK_ACTIONS:1000} # Execute the bulk every 1000 requests
  49. flushInterval: ${SW_STORAGE_ES_FLUSH_INTERVAL:10} # flush the bulk every 10 seconds whatever the number of requests
  50. concurrentRequests: ${SW_STORAGE_ES_CONCURRENT_REQUESTS:2} # the number of concurrent requests
  51. resultWindowMaxSize: ${SW_STORAGE_ES_QUERY_MAX_WINDOW_SIZE:10000}
  52. metadataQueryMaxSize: ${SW_STORAGE_ES_QUERY_MAX_SIZE:5000}
  53. segmentQueryMaxSize: ${SW_STORAGE_ES_QUERY_SEGMENT_SIZE:200}
  54. profileTaskQueryMaxSize: ${SW_STORAGE_ES_QUERY_PROFILE_TASK_SIZE:200}
  55. advanced: ${SW_STORAGE_ES_ADVANCED:""}
  56. mysql:
  57. properties:
  58. jdbcUrl: ${SW_JDBC_URL:"jdbc:mysql://localhost:3306/swtest"}
  59. dataSource.user: ${SW_DATA_SOURCE_USER:root}
  60. dataSource.password: ${SW_DATA_SOURCE_PASSWORD:root@1234}
  61. dataSource.cachePrepStmts: ${SW_DATA_SOURCE_CACHE_PREP_STMTS:true}
  62. dataSource.prepStmtCacheSize: ${SW_DATA_SOURCE_PREP_STMT_CACHE_SQL_SIZE:250}
  63. dataSource.prepStmtCacheSqlLimit: ${SW_DATA_SOURCE_PREP_STMT_CACHE_SQL_LIMIT:2048}
  64. dataSource.useServerPrepStmts: ${SW_DATA_SOURCE_USE_SERVER_PREP_STMTS:true}
  65. metadataQueryMaxSize: ${SW_STORAGE_MYSQL_QUERY_MAX_SIZE:5000}
  66. influxdb:
  67. # Metadata storage provider configuration
  68. metabaseType: ${SW_STORAGE_METABASE_TYPE:H2} # There are 2 options as Metabase provider, H2 or MySQL.
  69. h2Props:
  70. dataSourceClassName: ${SW_STORAGE_METABASE_DRIVER:org.h2.jdbcx.JdbcDataSource}
  71. dataSource.url: ${SW_STORAGE_METABASE_URL:jdbc:h2:mem:skywalking-oap-db}
  72. dataSource.user: ${SW_STORAGE_METABASE_USER:sa}
  73. dataSource.password: ${SW_STORAGE_METABASE_PASSWORD:}
  74. mysqlProps:
  75. jdbcUrl: ${SW_STORAGE_METABASE_URL:"jdbc:mysql://localhost:3306/swtest"}
  76. dataSource.user: ${SW_STORAGE_METABASE_USER:root}
  77. dataSource.password: ${SW_STORAGE_METABASE_PASSWORD:root@1234}
  78. dataSource.cachePrepStmts: ${SW_STORAGE_METABASE_CACHE_PREP_STMTS:true}
  79. dataSource.prepStmtCacheSize: ${SW_STORAGE_METABASE_PREP_STMT_CACHE_SQL_SIZE:250}
  80. dataSource.prepStmtCacheSqlLimit: ${SW_STORAGE_METABASE_PREP_STMT_CACHE_SQL_LIMIT:2048}
  81. dataSource.useServerPrepStmts: ${SW_STORAGE_METABASE_USE_SERVER_PREP_STMTS:true}
  82. metadataQueryMaxSize: ${SW_STORAGE_METABASE_QUERY_MAX_SIZE:5000}
  83. # InfluxDB configuration
  84. url: ${SW_STORAGE_INFLUXDB_URL:http://localhost:8086}
  85. user: ${SW_STORAGE_INFLUXDB_USER:root}
  86. password: ${SW_STORAGE_INFLUXDB_PASSWORD:}
  87. database: ${SW_STORAGE_INFLUXDB_DATABASE:skywalking}
  88. actions: ${SW_STORAGE_INFLUXDB_ACTIONS:1000} # the number of actions to collect
  89. duration: ${SW_STORAGE_INFLUXDB_DURATION:1000} # the time to wait at most (milliseconds)
  90. fetchTaskLogMaxSize: ${SW_STORAGE_INFLUXDB_FETCH_TASK_LOG_MAX_SIZE:5000} # the max number of fetch task log in a request
  91. ...

修改的内容为

  1. storage:
  2. selector: elasticsearch7
  3. elasticsearch7:
  4. nameSpace: ${SW_NAMESPACE:"skywalking-application"}
  5. clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:127.0.0.1:9200}

启动 skywalking

  • 启动 skywalking 后台服务
  1. /opt/apache/skywalking/apache-skywalking-apm-bin-es7$ sh bin/oapService.sh
  • 启动 UI
  1. /opt/apache/skywalking/apache-skywalking-apm-bin-es7$ sh bin/webappService.sh

注意 虽然启动后,会返回SkyWalking OAP started successfully!但是并不表示真的启动成功了。 看到 SkyWalking Web Application started successfully! 也不表示正在的成功;

在日志中

  1. /opt/apache/skywalking/apache-skywalking-apm-bin-es7$ tail -f logs/skywalking-oap-server.log

看到Started @66759ms表示启动成功

浏览器访问

  1. http://127.0.0.1:8080/

SkyWalking上报Java应用数据

请将以下示例代码中的替换为Agent文件夹中的skywalking-agent.jar的绝对路径。

JAR File或Spring Boot

在应用程序的启动命令行中添加-Dskywalking.agent.service_name参数。

  1. java -javaagent:<skywalking-agent-path> -Dskywalking.agent.service_name=<ServiceName> -Dskywalking.collector.backend_service=127.0.0.1:11800 -jar yourApp.jar

Jetty

在{JETTY_HOME}/start.ini配置文件中添加以下内容:

  1. --exec # 去掉前面的井号取消注释。
  2. -javaagent:<skywalking-agent-path>

Tomcat

linux下 在 tomcat/bin/catalina.sh 第一行添加以下内容:

  1. CATALINA_OPTS="$CATALINA_OPTS -javaagent:<skywalking-agent-path>"; export CATALINA_OPTS

Windows下在 tomcat/bin/catalina.bat 第一行添加以下内容:

  1. set "CATALINA_OPTS=-javaagent:<skywalking-agent-path>"

idea 下

  1. -Dskywalking.agent.service_name=skywalking-fiarap -Dskywalking.collector.backend_service=127.0.0.1:11800 -javaagent://opt/apache/skywalking/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar

Screen Shot 2020-06-12 at 9.59.08 AM.png

Screen Shot 2020-06-12 at 9.59.29 AM.png

skywalking 支持的中间件

Screen Shot 2020-06-12 at 10.08.26 AM.png