hf400159 commented on code in PR #1319: URL: https://github.com/apache/apisix-website/pull/1319#discussion_r971605534
########## blog/zh/blog/2022/09/15/apache-apisix-integrat-with-elasticsearch-for-logger.md: ########## @@ -0,0 +1,182 @@ +--- +title: "API 网关 Apache APISIX 集成 Elasticsearch 实现实时日志监控" +author: "王程程" +authorURL: "https://github.com/ccxhwmy" +authorImageURL: "https://github.com/ccxhwmy.png" +keywords: +- API 网关 +- Apache APISIX +- Elasticsearch +- 日志 +- 数据管理 +description: 本文将为你介绍 Apache APISIX 的 elasticsearch-logger 插件的相关信息,以及如何通过此插件获取 APISIX 的实时日志。 +tags: [Plugins,Ecosystem] +--- + +> 本文将为你介绍 Apache APISIX 的 elasticsearch-logger 插件的相关信息,以及如何通过此插件获取 APISIX 的实时日志。 + +<!--truncate--> + +## 背景信息 + +**Apache APISIX** 是一个动态、实时、高性能的 **API** 网关,提供了负载均衡、动态上游、灰度发布、服务熔断、身份认证、可观测性等丰富的流量管理功能。作为 **API** 网关,**Apache APISIX** 不仅拥有丰富的插件,而且支持插件的热加载。 + +**Elasticsearch** 是一个基于 [Lucene](https://zh.m.wikipedia.org/zh-hans/Lucene) 库的搜索引擎。它提供了分布式、RESTful 风格的搜索和数据分析引擎,具有可扩展性、可分布式部署和可进行相关度搜索等特点,能够解决不断涌现出的各种用例。同时还可以集中存储用户数据,帮助用户发现意料之中以及意料之外的情况。 + +## 插件介绍 + +APISIX 以 **HTTP** 请求的方式向 **Elasticsearch** 发送 APISIX 的 **Runtime** 日志。插件 `elasticsearch-logger` 采用 [bulk](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk) 的格式进行日志上报,这允许 APISIX 可以将多条日志合并后再进行上报,这使得 APISIX 在对 **Elasticsearch** 进行日志上报方面更加灵活并且具有较好的性能。你可以参考文档 [APISIX 批处理器](https://apisix.apache.org/zh/docs/apisix/batch-processor/) 对日志合进行更加细致的配置。 + +## 配置步骤 + +首先,你需要安装完成 APISIX,本文所有步骤基于 **Centos 7.5** 系统进行。详细的安装步骤参考 [APISIX 安装指南](https://apisix.apache.org/zh/docs/apisix/installation-guide/)。 + +### 步骤1:启动 Elasticsearch + +本示例只演示了通过 `docker-compose` 启动 **Elasticsearch** 单节点的方式,其它启动方式可参考 [Elasticsearch 官方文档](https://www.elastic.co/cn/downloads/elasticsearch)。 + +```dockerfile +# 使用 docker-compose 启动 1 个 Elasticsearch 节点, 1 个 kibana +version: '3.8' +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.17.1 + container_name: elasticsearch + environment: + ES_JAVA_OPTS: -Xms512m -Xmx512m + discovery.type: single-node + xpack.security.enabled: 'false' + networks: + - es-net + ports: + - "9200:9200" + - "9300:9300" + + kibana: + image: docker.elastic.co/kibana/kibana:7.17.1 + container_name: kibana + environment: + ELASTICSEARCH_HOSTS: http://elasticsearch:9200 + I18N_LOCALE: zh-CN + networks: + - es-net + depends_on: + - elasticsearch + ports: + - "5601:5601" + +networks: + es-net: + driver: bridge +``` + +### 步骤2:创建路由并开启插件 + +APISIX 默认不启用 `elasticsearch-logger` 插件,因此你可以通过下方命令进行路由创建和 `elasticsearch-logger` 插件的开启。 Review Comment: ```suggestion APISIX 默认已经启用 `elasticsearch-logger` 插件,所以你只需要通过下方命令创建路由并开启 `elasticsearch-logger` 插件就可以正常使用了。 ``` ########## blog/zh/blog/2022/09/15/apache-apisix-integrat-with-elasticsearch-for-logger.md: ########## @@ -0,0 +1,182 @@ +--- +title: "API 网关 Apache APISIX 集成 Elasticsearch 实现实时日志监控" +author: "王程程" +authorURL: "https://github.com/ccxhwmy" +authorImageURL: "https://github.com/ccxhwmy.png" +keywords: +- API 网关 +- Apache APISIX +- Elasticsearch +- 日志 +- 数据管理 +description: 本文将为你介绍 Apache APISIX 的 elasticsearch-logger 插件的相关信息,以及如何通过此插件获取 APISIX 的实时日志。 +tags: [Plugins,Ecosystem] +--- + +> 本文将为你介绍 Apache APISIX 的 elasticsearch-logger 插件的相关信息,以及如何通过此插件获取 APISIX 的实时日志。 + +<!--truncate--> + +## 背景信息 + +**Apache APISIX** 是一个动态、实时、高性能的 **API** 网关,提供了负载均衡、动态上游、灰度发布、服务熔断、身份认证、可观测性等丰富的流量管理功能。作为 **API** 网关,**Apache APISIX** 不仅拥有丰富的插件,而且支持插件的热加载。 + +**Elasticsearch** 是一个基于 [Lucene](https://zh.m.wikipedia.org/zh-hans/Lucene) 库的搜索引擎。它提供了分布式、RESTful 风格的搜索和数据分析引擎,具有可扩展性、可分布式部署和可进行相关度搜索等特点,能够解决不断涌现出的各种用例。同时还可以集中存储用户数据,帮助用户发现意料之中以及意料之外的情况。 + +## 插件介绍 + +APISIX 以 **HTTP** 请求的方式向 **Elasticsearch** 发送 APISIX 的 **Runtime** 日志。插件 `elasticsearch-logger` 采用 [bulk](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk) 的格式进行日志上报,这允许 APISIX 可以将多条日志合并后再进行上报,这使得 APISIX 在对 **Elasticsearch** 进行日志上报方面更加灵活并且具有较好的性能。你可以参考文档 [APISIX 批处理器](https://apisix.apache.org/zh/docs/apisix/batch-processor/) 对日志合进行更加细致的配置。 + +## 配置步骤 + +首先,你需要安装完成 APISIX,本文所有步骤基于 **Centos 7.5** 系统进行。详细的安装步骤参考 [APISIX 安装指南](https://apisix.apache.org/zh/docs/apisix/installation-guide/)。 + +### 步骤1:启动 Elasticsearch + +本示例只演示了通过 `docker-compose` 启动 **Elasticsearch** 单节点的方式,其它启动方式可参考 [Elasticsearch 官方文档](https://www.elastic.co/cn/downloads/elasticsearch)。 + +```dockerfile +# 使用 docker-compose 启动 1 个 Elasticsearch 节点, 1 个 kibana +version: '3.8' +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.17.1 + container_name: elasticsearch + environment: + ES_JAVA_OPTS: -Xms512m -Xmx512m + discovery.type: single-node + xpack.security.enabled: 'false' + networks: + - es-net + ports: + - "9200:9200" + - "9300:9300" + + kibana: + image: docker.elastic.co/kibana/kibana:7.17.1 + container_name: kibana + environment: + ELASTICSEARCH_HOSTS: http://elasticsearch:9200 + I18N_LOCALE: zh-CN + networks: + - es-net + depends_on: + - elasticsearch + ports: + - "5601:5601" + +networks: + es-net: + driver: bridge +``` + +### 步骤2:创建路由并开启插件 + +APISIX 默认不启用 `elasticsearch-logger` 插件,因此你可以通过下方命令进行路由创建和 `elasticsearch-logger` 插件的开启。 + +```shell +curl http://127.0.0.1:9180/apisix/admin/routes/1 \ +-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "plugins":{ + "elasticsearch-logger":{ + "endpoint_addr":"http://127.0.0.1:9200", + "field":{ + "index":"services", + "type":"collector" + }, + "ssl_verify":false, + "retry_delay":1, + "buffer_duration":60, + "max_retry_count":0, + "batch_max_size":1000, + "inactive_timeout":5, + "name":"elasticsearch-logger" + } + }, + "upstream":{ + "type":"roundrobin", + "nodes":{ + "127.0.0.1:1980":1 + } + }, + "uri":"/elasticsearch.do" +}' +``` + +上述代码中配置了 **Elasticsearch** 地址、目标 `field`,用户名与密码。 + +通过上述设置,就可以实现将 `/elasticsearch.do` 路径的 **API** 请求日志发送至 **Elasticsearch** 的功能。 + +### 步骤3:发送请求 + +接下来我们通过 **API** 发送一些请求。 + +```shell +curl -i http://127.0.0.1:9080/elasticsearch.do\?q\=hello +HTTP/1.1 200 OK +... +hello, world Review Comment: Separate commands and responses. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
