Skywalking
(https://github.com/apache/skywalking) 是一个开源的服务跟踪插件。
服务端目前支持http和grpc两种协议,在apisix中目前只支持http协议
endpoint
: Skywalking 的 http 节点,例如http://127.0.0.1:12800
。sample_ratio
: 监听的比例,最小为0.00001,最大为1。service_name
: 可选参数,标记当前服务的名称,默认值是APISIX
。下面是一个示例,在指定的 route 上开启了 skywalking 插件:
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "methods": ["GET"], "uris": [ "/uid/*" ], "plugins": { "skywalking": { "endpoint": "http://10.110.149.175:12800", "sample_ratio": 1, "service_name": "APISIX_SERVER" } }, "upstream": { "type": "roundrobin", "nodes": { "10.110.149.175:8089": 1 } } }'
你可以使用浏览器打开 dashboard:http://127.0.0.1:9080/apisix/dashboard/
,通过 web 界面来完成上面的操作,先增加一个 route:
然后在 route 页面中添加 skywalking 插件:
启动Skywalking Server:
默认使用H2存储,直接启动skywalking即可
sudo docker run --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 --restart always apache/skywalking-oap-server
如果使用elasticsearch存储
sudo docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 --restart always -e "discovery.type=single-node" elasticsearch:6.7.2
sudo docker run -d --name elastic-hq -p 5000:5000 --restart always elastichq/elasticsearch-hq
sudo docker run --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 --restart always --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server
Skywalking管理系统:
sudo docker run --name skywalking-ui -d -p 8080:8080 --link skywalking:skywalking -e SW_OAP_ADDRESS=skywalking:12800 --restart always apache/skywalking-ui
测试示例:
通过访问apisix,访问上游服务
$ curl -v http://10.110.149.192:9080/uid/12 HTTP/1.1 200 OK OK ...
打开浏览器,访问 Skywalking 的 web 页面:
http://10.110.149.175:8080/
可以看到访问拓扑图
可以看到服务追踪图
当你想去掉插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
$ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d value=' { "methods": ["GET"], "uris": [ "/uid/*" ], "plugins": { }, "upstream": { "type": "roundrobin", "nodes": { "10.110.149.175:8089": 1 } } }'
现在就已经移除了 Skywalking 插件了。其他插件的开启和移除也是同样的方法。
package com.lenovo.ai.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; /** * @author cyxinda * @create 2020-05-29 14:02 * @desc skywalking测试中央控制层 **/ @RestController public class TestController { @RequestMapping("/uid/{count}") public String getUidList(@PathVariable("count") String countStr, HttpServletRequest request) { System.out.println("counter:::::"+countStr); return "OK"; } }
启动服务的时候,需要配置skywalking agent, 修改agent/config/agent.config中的配置
agent.service_name=yourservername collector.backend_service=10.110.149.175:11800
启动服务脚本:
nohup java -javaagent:/root/skywalking/app/agent/skywalking-agent.jar \ -jar /root/skywalking/app/app.jar \ --server.port=8089 \ 2>&1 > /root/skywalking/app/logs/nohup.log &