Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何使用JavaScript Plugin,能否提供一个示例? #178

Closed
helloworld8192 opened this issue Nov 1, 2019 · 8 comments
Closed

Comments

@helloworld8192
Copy link

helloworld8192 commented Nov 1, 2019

尝试在后台添加JavaScript类型的plugin,内容如下:

function NewPlugin(cfg) {
  // import builtin modules
  var JSON = require("json")
  var HTTP = require("http")
  var REDIS = require("redis")
  var LOG = require("log")
  // init plugin
  return {
    // filter pre method
    "pre": function(ctx) {
  return {"code": 403, "error": "not login"}
    }
  }
}

但是没有效果,请求还是被正常转发
能否提供一个plugin_js的示例,谢谢!

@helloworld8192 helloworld8192 changed the title 如何使用JavaScript Plugin,能否提供一个示例? 请问如何使用JavaScript Plugin,能否提供一个示例? Nov 1, 2019
@zhangxu19830126
Copy link
Member

需要在页面上apply这个插件,插件不是创建就生效的。

@helloworld8192
Copy link
Author

@zhangxu19830126 谢谢,但是我已经把要启用的插件拖拽到“已使用插件”下面了,提交保存后仍然没生效。

选区_134

不知道是哪里的问题

@zhangxu19830126
Copy link
Member

把proxy的日志贴上来看看

@helloworld8192
Copy link
Author

helloworld8192 commented Nov 4, 2019

2019/11/04 11:48:13.729554 [info] filter added, filter=<PREPARE>
2019/11/04 11:48:13.729572 [info] filter added, filter=<TIMERANGE>
2019/11/04 11:48:13.729580 [info] filter added, filter=<WHITELIST>
2019/11/04 11:48:13.729586 [info] filter added, filter=<BLACKLIST>
2019/11/04 11:48:13.729593 [info] filter added, filter=<CACHING>
2019/11/04 11:48:13.729599 [info] filter added, filter=<ANALYSIS>
2019/11/04 11:48:13.729604 [info] filter added, filter=<RATE-LIMITING>
2019/11/04 11:48:13.729614 [info] filter added, filter=<CIRCUIT-BREAKER>
2019/11/04 11:48:13.729627 [info] filter added, filter=<HTTP-ACCESS>
2019/11/04 11:48:13.729640 [info] filter added, filter=<HEADER>
2019/11/04 11:48:13.729647 [info] filter added, filter=<XFORWARD>
2019/11/04 11:48:13.729652 [info] filter added, filter=<VALIDATION>
2019/11/04 11:48:13.729657 [info] filter added, filter=<JS-Plugin-Engine>
2019/11/04 11:48:13.729717 [info] start server check worker
2019/11/04 11:48:13.733544 [info] load proxies
2019/11/04 11:48:13.733612 [info] router start watch meta data 
2019/11/04 11:48:13.733660 [info] watch event at: </test>
2019/11/04 11:48:13.736440 [info] proxy <000192.168.1.101:9010> added
2019/11/04 11:48:13.736453 [info] load clusters
2019/11/04 11:48:13.736918 [info] cluster <5> added, data <id:5 name:"clusterOne" >
2019/11/04 11:48:13.736930 [info] load servers
2019/11/04 11:48:13.737378 [info] analysis: added, key=<6> interval=<1s>
2019/11/04 11:48:13.737464 [info] server <6> added, data <id:6 addr:"192.168.1.101:3009" maxQPS:1000 >
2019/11/04 11:48:13.737470 [info] load binds
2019/11/04 11:48:13.737531 [warning] server <6> heath check not setting
2019/11/04 11:48:13.737549 [info] server <6> changed to Up
2019/11/04 11:48:13.737846 [info] bind <5,6> created
2019/11/04 11:48:13.737852 [info] load apis
2019/11/04 11:48:13.738547 [info] api <7> added, data <id:7 name:"test1" urlPattern:"/echo" method:"*" status:Up ipAccessControl:<blacklist:"110.110.110.110" blacklist:"1.2.3.4" > nodes:<clusterID:5 urlRewrite:"/echo?after=10" attrName:"local3009" > >
2019/11/04 11:48:13.738604 [info] api <1001> added, data <id:1001 name:"test2" urlPattern:"/echo2" method:"*" status:Up nodes:<clusterID:5 urlRewrite:"/echo?before=7" > >
2019/11/04 11:48:13.738610 [info] load routings
2019/11/04 11:48:13.739021 [info] load plugins
2019/11/04 11:48:13.739371 [info] plugin <8/TestPlugin:1> added
2019/11/04 11:48:13.739380 [info] load applied plugins
2019/11/04 11:48:13.740239 [info] plugin: 8/TestPlugin:1 applied with index 0
2019/11/04 11:48:13.740255 [info] plugins applied with [8]
2019/11/04 11:48:13.740283 [info] metric: disable prometheus push client 
2019/11/04 11:48:13.740664 [info] start http at 192.168.1.101:9010


2019/11/04 11:48:19.930876 [info] [GET]/echo2: match api test2, has 1 dispatches
2019/11/04 11:48:19.930894 [debug] [GET]/echo2: dipatch node 0 to server 6
2019/11/04 11:48:19.930919 [info] [GET]/echo2: dipatch node 0 rewrite url to /echo?before=7
2019/11/04 11:48:19.930967 [info] [GET]/echo2: dispatch node 0 sent for 0 times
2019/11/04 11:48:19.932170 [debug] [GET]/echo2: dipatch node 0 return by 192.168.1.101:3009 with code 200, body <POST!>
2019/11/04 11:48:19.932301 [info] filter: 192.168.1.101 GET "/echo?before=7" 200 "PostmanRuntime/7.1.1" 192.168.1.101:3009 1.211623ms
2019/11/04 11:48:19.932324 [info] [GET]/echo2: return with raw body
2019/11/04 11:48:19.932355 [debug] [GET]/echo2: dispatch complete

看日志里:
2019/11/04 11:48:13.739021 [info] load plugins
2019/11/04 11:48:13.739371 [info] plugin <8/TestPlugin:1> added
2019/11/04 11:48:13.739380 [info] load applied plugins
2019/11/04 11:48:13.740239 [info] plugin: 8/TestPlugin:1 applied with index 0
2019/11/04 11:48:13.740255 [info] plugins applied with [8]
是不是JS_Plugin应该生效了?

@zhangxu19830126
Copy link
Member

js插件是个试验特性,默认是关闭的,虽然你可以在web上维护,但是proxy不会用js插件。需要在proxy启动的时候加上--js参数手动启用

@helloworld8192
Copy link
Author

我在proxy启动的时候加上--js 或者 --js=true 仍然没有生效

@zhangxu19830126
Copy link
Member

有日志么

@zhangxu19830126
Copy link
Member

Fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants