如何解决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 举报,一经查实,本站将立刻删除。