You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/07/30 10:40:33 UTC

[GitHub] [apisix-java-plugin-runner] Rdebu commented on issue #54: request help: 写的测试插件不起作用

Rdebu commented on issue #54:
URL: https://github.com/apache/apisix-java-plugin-runner/issues/54#issuecomment-889806445


   > can you use "httpbin.org:80" as upstream, and test "http://127.0.0.1:9080/get", then show the response.
   显示503
   ![image](https://user-images.githubusercontent.com/58921097/127641299-f48218e7-7263-492f-b7d9-dcf39888f38d.png)
   
   
   那应该是我插件配置有问题吗?
   
   上游:
   ![image](https://user-images.githubusercontent.com/58921097/127641358-596b857a-5f69-48f1-8da1-9aa70471c554.png)
   
   
   路由:
   ![image](https://user-images.githubusercontent.com/58921097/127641397-6597bc6a-6230-4a26-a27f-844b51e49dec.png)
   
   
   Java测试插件:
   /*
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.
    * The ASF licenses this file to You under the Apache License, Version 2.0
    * (the "License"); you may not use this file except in compliance with
    * the License.  You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
   
   package org.apache.apisix.plugin.runner.filter;
   
   import com.google.gson.Gson;
   import org.apache.apisix.plugin.runner.HttpRequest;
   import org.apache.apisix.plugin.runner.HttpResponse;
   import org.springframework.stereotype.Component;
   import reactor.core.publisher.Mono;
   
   import java.util.HashMap;
   import java.util.Map;
   
   @Component
   public class TestFilter implements PluginFilter {
   
       @Override
       public String name() {
           /* It is recommended to keep the name of the filter the same as the class name.
            Configure the filter to be executed on apisix's routes in the following format
   
           {
               "uri": "/hello",
               "plugins": {
                   "ext-plugin-pre-req": {
                       "conf": [{
                           "name": "TestFilter",
                           "value": "bar"
                       }]
                   }
               },
               "upstream": {
                   "nodes": {
                       "127.0.0.1:1980": 1
                   },
                   "type": "roundrobin"
               }
           }
   
           The value of name in the configuration corresponds to the value of return here.
            */
   
           return "TestFilter";
       }
   
       @Override
       public Mono<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.
            */
   
           // note: the path to the rewrite must start with '/'
   //        request.setPath((String) conf.get("rewrite_path"));
   //        request.setHeader((String) conf.get("conf_header_name"), (String) conf.get("conf_header_value"));
           request.setHeader("para_test", "ning_test");
           /* note: The value of the parameter is currently a string type.
                    If you need the json type, you need the upstream service to parse the string value to json.
                    For example, if the arg is set as below
                    request.setArg("new arg", "{\"key1\":\"value1\",\"key2\":2}");
   
                    The arg received by the upstream service will be as below
                    "new arg": "{\"key1\":\"value1\",\"key2\":2}"
            */
           request.setArg("test_arg", "test_value");
   
           response.setHeader("para_test", "test");
           return chain.filter(request, response);
       }
   }
   
   
   Java插件位置
   ![image](https://user-images.githubusercontent.com/58921097/127641485-7f89b986-faf8-49f0-9961-1e72e6e85247.png)
   
   
   最后也在centos中打了jar包运行了  只有可能上面某一步错了吧  大佬看看


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org