WSO2 Api Manager 3.2-自定义处理程序,无登录文件,处理程序不起作用

如何解决WSO2 Api Manager 3.2-自定义处理程序,无登录文件,处理程序不起作用

我正在尝试为WSO2 Api Manager v3.2开发自定义处理程序。我使用了WSO2AM的官方文档中提供的教程:https://apim.docs.wso2.com/en/latest/develop/extending-api-manager/extending-gateway/writing-custom-handlers/

我试图使处理程序在发送到示例API(PizzaShack)的请求中添加“ X-Request-ID”标头,但甚至找不到日志。我不确定我的代码或配置是否有问题。我通过osgi控制台检查了WSO2AM中该处理程序是否被视为活动状态。

我的课:

package com.company.wso2.handlers;

import org.apache.synapse.MessageContext;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Random;

import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS;

public class CustomHeaderHandler extends AbstractHandler {

    private static final String REQUEST_ID_HEADER="X-Request-ID";

    private static final Logger LOG = LoggerFactory.getLogger(CustomHeaderHandler.class);

    public boolean handleRequest(MessageContext messageContext) {
        Map<String,String> headers = (Map<String,String>) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
                getProperty(TRANSPORT_HEADERS);

        if (!headers.containsKey(REQUEST_ID_HEADER)) {
            LOG.info("Request-ID missing,adding new Request-ID");
            System.out.println("Request-ID missing,adding new Request-ID");
            headers.put(REQUEST_ID_HEADER,Integer.toHexString(new Random().nextInt(0x9999999) + 0x9999999));
            ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(TRANSPORT_HEADERS,headers);
            return true;
        }

        LOG.debug("Request-ID header present,continuing");
        System.out.println("Request-ID header present,continuing");

        return true;
    }

    public boolean handleResponse(MessageContext messageContext) {
        Map<String,adding new Request-ID in response");
            System.out.println("Request-ID missing,adding new Request-ID in response");
            headers.put(REQUEST_ID_HEADER,continuing in response");
        System.out.println("Request-ID header present,continuing in response");

        return true;
    }

}


我的pom.xml文件负责使用上面显示的类创建一个jar:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.wso2.handlers</groupId>
    <artifactId>custom-header-handler</artifactId>
    <version>0.1</version>
    <packaging>bundle</packaging>

    <name>custom-header-handler</name>

    <dependencies>
        <dependency>
            <groupId>org.apache.synapse</groupId>
            <artifactId>synapse-core</artifactId>
            <version>2.1.7-wso2v10</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>wso2-nexus</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </repository>

        <repository>
            <id>wso2.releases</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </repository>

        <repository>
            <id>wso2.snapshots</id>
            <name>Apache Snapshot Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>wso2-nexus</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </pluginRepository>

        <pluginRepository>
            <id>wso2.releases</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </pluginRepository>

        <pluginRepository>
            <id>wso2.snapshots</id>
            <name>WSO2 Snapshot Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>1.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>com.company.wso2.handlers</Bundle-SymbolicName>
                        <Bundle-Name>com.company.wso2.handlers</Bundle-Name>
                        <Export-Package>
                             com.company.wso2.handlers.*,</Export-Package>
                        <Import-Package>
                            *; resolution:=optional
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

构建jar之后,我将其添加到以下文件夹中:[WSO2AM_HOME]/repository/components/dropins/ 我还手动将此处理程序类添加到文件[WSO2AM_HOME]/repository/deployment/server/synapse-configs/default/api/admin--PizzaShackAPI_v1.0.0.xml中的synapse-config中 “处理程序”部分如下:

<handlers>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.common.APIMgtLatencyStatsHandler">
            <property name="apiUUID" value="303aa4cb-fd51-4194-b319-aa0410a853f0"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler">
            <property name="apiImplementationType" value="ENDPOINT"/>
            <property name="AuthorizationHeader" value="Authorization"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler">
            <property name="RemoveOAuthHeadersFromOutMessage" value="true"/>
            <property name="APILevelPolicy" value=""/>
            <property name="AuthorizationHeader" value="Authorization"/>
            <property name="keyManagers" value="all"/>
            <property name="CertificateInformation" value="{}"/>
            <property name="APISecurity" value="oauth2,oauth_basic_auth_api_key_mandatory"/>
            <property name="apiUUID" value="303aa4cb-fd51-4194-b319-aa0410a853f0"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler"/>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtUsageHandler"/>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler">
            <property name="configKey" value="ga-config-key"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>
        <!-- logging according to docs -->
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.logging.APILogMessageHandler"/>
        <!-- Custom request ID handler -->
        <handler class="com.company.wso2.handlers.CustomHeaderHandler"/>
    </handlers>

最近[WSO2AM_HOME]/repository/conf/log4j2.properties文件的配置(剪切版本):

(...)

loggers = (all the loggers provided by default in config),custom-log-handler

(...)

logger.custom-log-handler.name = com.company.wso2.handlers.CustomHeaderHandler
logger.custom-log-handler.level = DEBUG
logger.custom-log-handler.appenderRef.CARBON_LOGFILE.ref = CARBON_LOGFILE

如果您希望我提供其他任何内容,请随时提出。谢谢您对这个问题的帮助。

解决方法

我对您的自定义处理程序进行了快速检查。一切正常。

[2020-11-10 19:28:05,138]  INFO - CustomHeaderHandler Request-ID missing,adding new Request-ID in response
Request-ID missing,adding new Request-ID in response
[2020-11-10 19:29:24,408]  INFO - CustomHeaderHandler Request-ID missing,adding new Request-ID
Request-ID missing,adding new Request-ID
[2020-11-10 19:29:24,486]  INFO - CustomHeaderHandler Request-ID missing,adding new Request-ID in response

是否可以从新包装中恢复log4j2.properties文件并检查其行为。

这不会有任何区别,但是由于您使用的是apim 3.2.0,请按如下所示更改依赖关系

<dependency>
    <groupId>org.apache.synapse</groupId>
    <artifactId>synapse-core</artifactId>
    <version>2.1.7-wso2v183</version>
</dependency>

此外,您可以尝试使用Apache Commons日志记录吗。

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

private static final Log LOG = LogFactory.getLog(CustomHeaderHandler.class);

尝试上述操作并分享反馈。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-