简体   繁体   English

wso2 API Manager和BAM-如何控制API调用?

[英]wso2 API Manager and BAM - How to control API invocation?

How can I retrieve the number of API invocations? 如何获取API调用次数? I know the data has to be somewhere because wso2 BAM shows piecharts with similar data... 我知道数据必须在某处,因为wso2 BAM显示具有类似数据的饼图...

I would like to get that number in a mediation sequencel; 我想在调解序列中获得该号码; is that possible? 那可能吗? Might this might be achieved via a DB-lookup? 可以通过数据库查找来实现吗?

The way how API Usage Monitoring in WSO2 API Manager works is, there is an API handler ( org.wso2.carbon.apimgt.usage.publisher.APIUsageHandler ) that gets invoked for each request and response passing through the API gateway. WSO2 API Manager中API使用情况监视的工作方式是,有一个API处理程序org.wso2.carbon.apimgt.usage.publisher.APIUsageHandler ),该处理程序针对通过API网关传递的每个请求和响应进行调用。 In this handler all pertinent information with regard to API usage is published to the WSO2 BAM server. 在此处理程序中,所有与API使用有关的信息都发布到WSO2 BAM服务器。 The WSO2 BAM server persists this data in Cassandra database that is shipped with it. WSO2 BAM服务器将该数据保留在其随附的Cassandra数据库中。 Then there is a BAM Toolbox that has been packaged with required analytic scripts written using Apache Hive that can be installed on the BAM server. 然后是一个BAM工具箱,该工具箱与使用Apache Hive编写的必需分析脚本打包在一起,可以安装在BAM服务器上。 These scripts would summarize the data periodically and persist the summarized data to an sql database. 这些脚本将定期汇总数据,并将汇总的数据持久保存到sql数据库中。 So the graphs and charts shown in the API Publisher web application are created using the summarized data from the sql database. 因此, API Publisher Web应用程序中显示的图形和图表是使用sql数据库中的汇总数据创建的。

Now, if what you require is extractable from these summarized sql tables then i suppose the process is very straight forward. 现在,如果您需要从这些汇总的sql表中提取内容,那么我认为该过程非常简单。 You could use the DBLookup mediator for this. 您可以为此使用DBLookup介体。 But if some dimension of the data which you need has been lost due to the summarizing, then you will have a little more work to do. 但是,如果由于汇总而丢失了所需数据的某个维度,那么您将需要做更多的工作。

You have two options. 您有两个选择。

  1. The easiest approach which involves no coding at all would be to write a custom Hive script that suits your requirement and summarize data to a sql table. 最简单的方法,根本不需要编码,这是编写适合您需求的自定义Hive脚本并将数据汇总到sql表中。 Then, like before use a DBLookup mediator to read the data. 然后,就像之前使用DBLookup中介程序一样读取数据。 You can look at the existing Hive scripts that are shipped with the product to get an idea of how it is written. 您可以查看产品随附的现有Hive脚本,以了解其编写方式。
  2. If you dont want BAM in the picture, you still can do it with minimal coding as follows. 如果您不希望BAM出现在图片中,您仍然可以通过最少的编码来做到这一点,如下所示。 The implementation class which performs the publishing is org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher . 执行发布的实现类是org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher This class implements the interface org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataPublisher . 此类实现接口org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataPublisher The interface has three instace methods as follows. 该接口具有以下三种安装方法。

    public void init() 公共无效init()

    public void publishEvent(RequestPublisherDTO requestPublisherDTO) 公共无效publishEvent(RequestPublisherDTO requestPublisherDTO)

    public void publishEvent(ResponsePublisherDTO responsePublisherDTO) 公共无效publishEvent(ResponsePublisherDTO responsePublisherDTO)

The init() method runs just once during server startup. init()方法在服务器启动期间仅运行一次。 Here is where you can add all your logic which is needed to bootstrap the class. 在这里,您可以添加引导类所需的所有逻辑。 The publishEvent(RequestPublisherDTO) is where you publish request events and publishEvent(ResponsePublisherDTO) is where you publish response events. publishEvent(RequestPublisherDTO)是发布请求事件的地方,而publishEvent(ResponsePublisherDTO)是发布响应事件的地方。 The DTO objects are encapsulated representations of the request and response data respectively. DTO对象分别是请求和响应数据的封装表示。

What you will have to do is write a new implementation for this interface and configure it as the value for DataPublisherImpl property in api-manager.xml . 您需要做的是为此接口编写一个新的实现,并将其配置为api-manager.xml中 DataPublisherImpl属性的值。 To make things easier you can simply extends the exsiting org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher , write your necessary logic to persist usage data to an sql database within the init(), publishEvent(RequestPublisherDTO) and publishEvent(ResponsePublisherDTO) and at the end of each method just call its respective super class method. 为了使事情变得更容易,您可以简单地扩展现有的org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher ,编写必要的逻辑以将使用情况数据持久保存到init(),publishEvent(RequestPublisherDTO)和publishEvent(ResponsePublisherDTO)中的sql数据库中),然后在每个方法的末尾仅调用其各自的超类方法。 Eg the overriding init() will call super().init(). 例如,重载的init()将调用super()。init()。 This way you are only adding the neccessary code for your requirement, and leaving the BAM stat collection requirement to the super class. 这样,您只需要添加所需的代码即可,并将BAM统计信息收集要求留给超类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM