Open Jimy-Guo opened 1 year ago
it seems that all are successful,but the plugins are not working
- please adjust the format of the different contents of the issue (code, issue description, etc.), it's too confusing now.
it seems that all are successful,but the plugins are not working
- what is the route you added on APISIX (route to configure java plugin runner)
- how do you verify that the plugin is not working? I need this reproduction process like: bug: 使用apisix的ext-plugin-post-resp插件配合apisix-java-plugin-runner0.3.0支持过滤上游响应标头时,上游返回状态码为204时,会重复发送http的请求的问题 #189 (comment)
我已经修改了,请您再看一看
Possible errors I see.
I suggest you
Possible errors I see.
- the built jar does not contain your custom filter;
- 0.3.1-SNAPSHOT contains errors.
I suggest you
- use 0.4.0 (already released)
- try with https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/installation-guide.md#install, example: https://github.com/tzssangglass/java-plugin-runner-demo-1
原来的apache-apisix-java-plugin-runner已经更新代码生成使用apache-apisix-java-plugin-runner-0.4.0-bin.tar.gz,插件仍然没有起效:
"plugins": {
"ext-plugin-pre-req": {
"conf": [
{
"name": "StopRequestDemoFilter",
"value": "{\"stop_response_body\":\"{\"hello\":\"success\"}\",\"stop_response_header_name\":\"x-test-header\",\"stop_response_header_value\":\"success\",\"stop_response_code\":\"401\"}"
}
]
},
对应的StopRequestDemoFilter为
@Component
public class StopRequestDemoFilter implements PluginFilter {
@Override
public String name() {
return "StopRequestDemoFilter";
}
@Override
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
/*
* If the conf you configured is of type json, you can convert it to Map or json.
*/
String configStr = request.getConfig(this);
Gson gson = new Gson();
Map<String, Object> conf = new HashMap<>();
conf = gson.fromJson(configStr, conf.getClass());
/*
* You can use the parameters in the configuration.
*/
response.setStatusCode(Double.valueOf(conf.get("stop_response_code").toString()).intValue());
response.setHeader((String) conf.get("stop_response_header_name"), (String) conf.get("stop_response_header_value"));
/* note: The body is currently a string type.
If you need the json type, you need to escape the json content here.
For example, if the body is set as below
"{\"key1\":\"value1\",\"key2\":2}"
The body received by the client will be as below
{"key1":"value1","key2":2}
*/
response.setBody((String) conf.get("stop_response_body"));
/* Using the above code, the client side receives the following
header:
HTTP/1.1 401 Unauthorized
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
new-header: header_by_runner
Server: APISIX/2.6
body:
{"key1":"value1","key2":2}
*/
chain.filter(request, response);
}
@Override
public List<String> requiredVars() {
return null;
}
@Override
public Boolean requiredBody() {
return null;
}
}
按照逻辑来讲是无论成功与否都是返回401的,但是仍然返回:
curl -i "http://0.0.0.0:9080/v1/admin/routes"
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Date: Thu, 08 Dec 2022 05:59:21 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: *
Access-Control-Max-Age: 3600
Server: APISIX/2.15.1
{"action":"get","count":2,"node":{"key":"\/apisix\/routes","nodes":[{"modifiedIndex":224,"createdIndex":36,"key":"\/apisix\/routes\/435797591813260093","value":{"labels":{"clife-user":"user","API_VERSION":"v1"},"upstream_id":"435797316717249341","methods":["GET","POST","PUT","DELETE","PATCH","HEAD","OPTIONS","CONNECT","TRACE"],"update_time":1670210706,"plugins":{"limit-conn":{"default_conn_delay":1.5,"key_type":"var","only_use_default_delay":false,"key":"remote_addr","rejected_msg":"conn too more","rejected_code":503,"disable":false,"allow_degradation":false,"burst":5,"conn":2},"ext-plugin-pre-req":{"conf":[{"value":"{\"validate_header\":\"token\",\"rejected_code\":\"403\"}","name":"TokenCheckFilter2"}]},"proxy-rewrite":{"uri":"\/clife-user-app-api\/test","headers":{"Host":"clife-user-app-api.clife-public"}}},"status":1,"create_time":1669285206,"uri":"\/v1\/account\/test","id":"435797591813260093","name":"testApi"}},{"modifiedIndex":1079,"createdIndex":390,"key":"\/apisix\/routes\/437376598874784573","value":{"labels":{"API_VERSION":"v1"},"methods":["GET","POST","PUT","DELETE","PATCH","HEAD","OPTIONS","CONNECT","TRACE"],"update_time":1670478982,"plugins":{"ext-plugin-pre-req":{"conf":[{"value":"{\"stop_response_body\":\"{\"hello\":\"success\"}\",\"stop_response_header_name\":\"x-test-header\",\"stop_response_header_value\":\"success\",\"stop_response_code\":\"401\"}","name":"StopRequestDemoFilter"}]},"proxy-rewrite":{"uri":"\/apisix\/admin\/routes","headers":{"X-API-KEY":" edd1c9f034335f136f87ad84b625c8f1"}}},"status":1,"uri":"\/v1\/admin\/routes","create_time":1670226368,"id":"437376598874784573","name":"test","upstream":{"scheme":"http","type":"roundrobin","pass_host":"pass","keepalive_pool":{"idle_timeout":60,"requests":1000,"size":320},"nodes":[{"port":9180,"host":"localhost","weight":1}],"timeout":{"connect":6,"read":6,"send":6}}}}],"dir":true}}
明显没有生效,这是还有什么其他我需要添加的内容吗? 文档中https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/installation-guide.md#install%EF%BC%8C的 Add the apisix-java-plugin-runner dependency in your POM, like:
这一部分是加在哪里的呢?
Possible errors I see.
- the built jar does not contain your custom filter;
- 0.3.1-SNAPSHOT contains errors.
I suggest you
- use 0.4.0 (already released)
- try with https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/installation-guide.md#install, example: https://github.com/tzssangglass/java-plugin-runner-demo-1
我将https://github.com/tzssangglass/java-plugin-runner-demo-1的程序打包后,修改了以下地方的配置 java-plugin-runner-demo的application.yaml:
logging:
level:
root: debug
cache.config:
expired: ${APISIX_CONF_EXPIRE_TIME}
capacity: 1000
socket:
file: ${APISIX_LISTEN_ADDRESS}
pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>11</java.version>
<compiler.version>11</compiler.version>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.apisix</groupId>
<artifactId>apisix-runner-starter</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.apisix</groupId>
<artifactId>apisix-runner-plugin-sdk</artifactId>
<version>0.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.8</version>
</plugin>
</plugins>
</build>
</project>
使用的DockerFile为:
#进入文件后
#依赖镜像名称和ID
FROM apache/apisix:2.15.1-debian
#指定镜像创建者信息
MAINTAINER jimyguo
#将宿主机的文件拷贝到容器的具体目录中。这里使用ADD,拷贝后自动解压,如果不需要解压,可以使用COPY
ADD jdk-11.0.16.1_linux-x64_bin.tar.gz /jdk
#ADD apache-apisix-java-plugin-runner-0.3.1-SNAPSHOT-bin.tar.gz /usr/local
#RUN apt -y install openjdk-11-jdk
#ADD apache-apisix-java-plugin-runner-0.4.0-bin.tar.gz /usr/local
ADD demo-0.0.1-SNAPSHOT.jar /usr/local
#ADD apisix-java-plugin-runner-0.4.0-SNAPSHOT-src.tgz /usr/local
#
### 配置jdk环境
ENV JAVA_HOME /jdk/jdk-11.0.16.1
ENV PATH ${JAVA_HOME}/bin:$PATH
apisix的config.yaml:
ext-plugin:
cmd: ['java', '-jar', '-Xmx4g', '-Xms4g', '/usr/local/demo-0.0.1-SNAPSHOT.jar']
路由上配置为:
"plugins": {
"ext-plugin-pre-req": {
"conf": [
{
"name": "DemoFilter",
"value": "{\"enable\":\"feature\"}"
}
]
},
配置完之后日志可见
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.047 DEBUG 58 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'demoApplication'
, context: ngx.timer
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.049 DEBUG 58 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'demoFilter'
, context: ngx.timer
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.061 DEBUG 58 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'a6HandlerConfiguration'
, context: ngx.timer
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.062 DEBUG 58 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'applicationRunner'
调用后可见
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.397 DEBUG 58 --- [ main] i.n.b.ByteBufUtil : -Dio.netty.threadLocalDirectBufferSize: 0
, context: ngx.timer
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.397 DEBUG 58 --- [ main] i.n.b.ByteBufUtil : -Dio.netty.maxThreadLocalCharBufferSize: 16384
, context: ngx.timer
2022/12/08 06:16:33 [warn] 57#57: *247 [lua] init.lua:913: 2022-12-08 06:16:33.419 WARN 58 --- [ main] o.a.a.p.r.s.ApplicationRunner : java runner is listening on the socket file: /usr/local/apisix/conf/apisix-1.sock
, context: ngx.timer
127.0.0.1 - - [08/Dec/2022:06:20:34 +0000] 0.0.0.0:9080 "GET /apisix/admin/routes HTTP/1.1" 200 1734 0.002 "-" "curl/7.74.0" - - - "http://0.0.0.0:9080"
127.0.0.1 - - [08/Dec/2022:06:20:34 +0000] 0.0.0.0:9080 "GET /v1/admin/routes HTTP/1.1" 200 1734 0.005 "-" "curl/7.74.0" 127.0.0.1:9180 200 0.004 "http://0.0.0.0:9080/apisix/admin/routes"
可见未打印logger.warn("DemoFilter is running");,需确定是还有什么地方漏了吗?
ref: https://github.com/apache/apisix-java-plugin-runner/issues/219#issuecomment-1341872721
my full route config is:
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
"ext-plugin-pre-req": {
"conf" : [
{"name": "DemoFilter", "value": "{\"enable\":\"feature\"}"}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
and test it like:
curl http://127.0.0.1:9080/get
send request to hit route which has ext-plugin-pre-req
plugin.
ref: #219 (comment)
my full route config is:
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/get", "plugins": { "ext-plugin-pre-req": { "conf" : [ {"name": "DemoFilter", "value": "{\"enable\":\"feature\"}"} ] } }, "upstream": { "type": "roundrobin", "nodes": { "httpbin.org:80": 1 } } }'
and test it like:
curl http://127.0.0.1:9080/get
send request to hit route which has
ext-plugin-pre-req
plugin.
好的,我这边尝试ref: https://github.com/apache/apisix-java-plugin-runner/issues/219#issuecomment-1341872721 查看回复请求头
ref: #219 (comment)
my full route config is:
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/get", "plugins": { "ext-plugin-pre-req": { "conf" : [ {"name": "DemoFilter", "value": "{\"enable\":\"feature\"}"} ] } }, "upstream": { "type": "roundrobin", "nodes": { "httpbin.org:80": 1 } } }'
and test it like:
curl http://127.0.0.1:9080/get
send request to hit route which has
ext-plugin-pre-req
plugin.
创建相同route的时候报错,为以下报错: {"error_msg":"unknown plugin [ext-plugin-pre-req]"}
{"error_msg":"unknown plugin [ext-plugin-pre-req]"}
@Jimy-Guo I have the same problem as you,please modify apisix-docker/example/apisix_conf/config.yml,enbaled plugin,like this: plugins:
Issue description
Environment
使用的dockerfile为
Minimal test code / Steps to reproduce the issue
1.在正常打包出来的jar包按照https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/how-it-works.md 启动之后以下代码中检出System.getProperty("user.dir")为/usr/local/apisix导致HotReloadProcess.hotReloadFilter()报错
2、修改启动成功后,使用的插件对应的配置为
启动时以下程序的代码没有打印出对应plugin 的日志,如 logger.debug("get plugins List:{}",pluginFilterList),是否就是没有检测到插件并加以使用
日志如下:
调用了接口之后时应该会使用到TokenCheckFilter2
在容器调用链接后返回结果为
对应的日志为
能够直接访问且没有出现对应插件中的日志打印,故判断插件确实没有生效
What's the actual result? (including assertion message & call stack if applicable)
请求没有通过插件的检测返回配置的数据,能够访问成功
What's the expected result?
当请求没有待遇偶需要的appId和accessToken会返回接口配置的rejected_code:403.