Repository: huaweicloud/sermant
Issue: 【bug】fix the registry plugin bugs

### What happened?

feign core 10.12无法正常工作，resttemplate 5.3.13/5.1.2.RELEASE无法正确处理异常

### How can we reproduce it (as minimally and precisely as possible)?

no

### Anything else we need to know?

_No response_

### Sermant version

0.6.1

### OS version

n/a

Changed files:
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/test/java/com/huawei/discovery/interceptors/RestTemplateInterceptorTest.java

diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java
index 423a720a..d9c5d91a 100644
--- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java
+++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java
@@ -113,7 +113,7 @@ public class PluginManager {
     private static void initPlugin(String pluginName, String pluginPackage, Instrumentation instrumentation) {
         final String pluginPath = pluginPackage + File.separatorChar + pluginName;
         if (!new File(pluginPath).exists()) {
-            LOGGER.warning(String.format(Locale.ROOT,
+            LOGGER.severe(String.format(Locale.ROOT,
                     "Plugin directory %s does not exist, so skip initializing %s. ", pluginPath, pluginName));
             return;
         }
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
index 8a481081..498ff966 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
@@ -64,7 +64,7 @@ public class FeignInvokeInterceptor extends MarkInterceptor {
         return invokerContext -> {
             context.getArguments()[0] = Request.create(request.httpMethod(),
                 RequestInterceptorUtils.buildUrl(urlInfo, invokerContext.getServiceInstance()),
-                request.headers(), request.requestBody());
+                request.headers(), request.body(), request.charset());
             return RequestInterceptorUtils.buildFunc(context, invokerContext).get();
         };
     }
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
index 9497ad8a..080eeaca 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
@@ -22,21 +22,19 @@ import com.huawei.discovery.utils.HttpConstants;
 import com.huawei.discovery.utils.PlugEffectWhiteBlackUtils;
 import com.huawei.discovery.utils.RequestInterceptorUtils;
 
+import com.huaweicloud.sermant.core.common.LoggerFactory;
 import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
 import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
 
-import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.client.ClientHttpResponse;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Optional;
 import java.util.function.Function;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * 拦截获取服务列表
@@ -45,23 +43,31 @@ import java.util.function.Function;
  * @since 2022-09-27
  */
 public class RestTemplateInterceptor extends MarkInterceptor {
+    private static final Logger LOGGER = LoggerFactory.getLogger();
 
     @Override
     protected ExecuteContext doBefore(ExecuteContext context) throws Exception {
         final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class);
-        URI uri = (URI)context.getArguments()[0];
-        HttpMethod httpMethod = (HttpMethod)context.getArguments()[1];
+        URI uri = (URI) context.getArguments()[0];
+        HttpMethod httpMethod = (HttpMethod) context.getArguments()[1];
         Map<String, String> hostAndPath = RequestInterceptorUtils.recoverHostAndPath(uri.getPath());
         if (!PlugEffectWhiteBlackUtils.isAllowRun(uri.getHost(), hostAndPath.get(HttpConstants.HTTP_URI_HOST),
             true)) {
             return context;
         }
         RequestInterceptorUtils.printRequestLog("restTemplate", hostAndPath);
-        invokerService.invoke(
-                buildInvokerFunc(uri, hostAndPath, context, httpMethod),
-                this::buildErrorResponse,
-                hostAndPath.get(HttpConstants.HTTP_URI_HOST))
-                .ifPresent(context::skip);
+        Optional<Object> result = invokerService.invoke(
+            buildInvokerFunc(uri, hostAndPath, context, httpMethod),
+            this::buildErrorResponse,
+            hostAndPath.get(HttpConstants.HTTP_URI_HOST));
+        if (result.isPresent()) {
+            Object obj = result.get();
+            if (obj instanceof Exception) {
+                LOGGER.log(Level.SEVERE, "request is error, uri is " + uri, (Exception) obj);
+                return context;
+            }
+            context.skip(obj);
+        }
         return context;
     }
 
@@ -89,7 +95,7 @@ public class RestTemplateInterceptor extends MarkInterceptor {
         ExecuteContext context, HttpMethod httpMethod) {
         return invokerContext -> {
             String url = RequestInterceptorUtils.buildUrlWithIp(uri, invokerContext.getServiceInstance(),
-                    hostAndPath.get(HttpConstants.HTTP_URI_PATH), httpMethod.name());
+                hostAndPath.get(HttpConstants.HTTP_URI_PATH), httpMethod.name());
             context.getArguments()[0] = rebuildUri(url, uri);
             return RequestInterceptorUtils.buildFunc(context, invokerContext).get();
         };
@@ -101,44 +107,8 @@ public class RestTemplateInterceptor extends MarkInterceptor {
      * @param ex
      * @return 响应
      */
-    private ClientHttpResponse buildErrorResponse(Exception ex) {
-        return new ClientHttpResponse() {
-
-            @Override
-            public HttpHeaders getHeaders() {
-                return new HttpHeaders();
-            }
-
-            @Override
-            public InputStream getBody() throws IOException {
-                return new InputStream() {
-                    @Override
-                    public int read() throws IOException {
-                        return 0;
-                    }
-                };
-            }
-
-            @Override
-            public HttpStatus getStatusCode() throws IOException {
-                return HttpStatus.INTERNAL_SERVER_ERROR;
-            }
-
-            @Override
-            public int getRawStatusCode() throws IOException {
-                return HttpStatus.INTERNAL_SERVER_ERROR.value();
-            }
-
-            @Override
-            public String getStatusText() throws IOException {
-                return "";
-            }
-
-            @Override
-            public void close() {
-
-            }
-        };
+    private Exception buildErrorResponse(Exception ex) {
+        return ex;
     }
 
     @Override
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/test/java/com/huawei/discovery/interceptors/RestTemplateInterceptorTest.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/test/java/com/huawei/discovery/interceptors/RestTemplateInterceptorTest.java
index cd390e92..c6024f6b 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/test/java/com/huawei/discovery/interceptors/RestTemplateInterceptorTest.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/test/java/com/huawei/discovery/interceptors/RestTemplateInterceptorTest.java
@@ -16,11 +16,13 @@
 
 package com.huawei.discovery.interceptors;
 
-import java.io.IOException;
-import java.lang.reflect.Method;
-import java.net.URI;
-import java.util.Map;
-import java.util.Optional;
+import com.huawei.discovery.config.PlugEffectWhiteBlackConstants;
+import com.huawei.discovery.entity.PlugEffectStategyCache;
+import com.huawei.discovery.service.InvokerService;
+
+import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
+import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
+import com.huaweicloud.sermant.core.utils.ReflectUtils;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -28,15 +30,12 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.springframework.http.HttpMethod;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.client.ClientHttpResponse;
 
-import com.huawei.discovery.config.PlugEffectWhiteBlackConstants;
-import com.huawei.discovery.entity.PlugEffectStategyCache;
-import com.huawei.discovery.service.InvokerService;
-import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
-import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
-import com.huaweicloud.sermant.core.utils.ReflectUtils;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.Map;
+import java.util.Optional;
 
 /**
  * RestTemplate调用测试
@@ -74,10 +73,10 @@ public class RestTemplateInterceptorTest extends BaseTest {
         interceptor = new RestTemplateInterceptor();
         MockitoAnnotations.openMocks(this);
         pluginServiceManagerMockedStatic.when(() -> PluginServiceManager.getPluginService(InvokerService.class))
-                .thenReturn(invokerService);
+            .thenReturn(invokerService);
     }
 
-    private URI createURI(String url){
+    private URI createURI(String url) {
         return URI.create(url);
     }
 
@@ -99,27 +98,27 @@ public class RestTemplateInterceptorTest extends BaseTest {
         //含域名，设置多个域名，未设置黑白名单
         discoveryPluginConfig.setRealmName(realmNames);
         interceptor.doBefore(context);
-        URI uriNew = (URI)context.getArguments()[0];
+        URI uriNew = (URI) context.getArguments()[0];
         Assert.assertEquals(url, uriNew.toString());
 
         discoveryPluginConfig.setRealmName(realmName);
         //含域名，设置全部通过策略
         initStrategy(PlugEffectWhiteBlackConstants.STRATEGY_ALL, "zookeeper-provider-demo");
         Mockito.when(invokerService.invoke(null, null, "zookeeper-provider-demo"))
-                .thenReturn(Optional.ofNullable(new Object()));
+            .thenReturn(Optional.ofNullable(new Object()));
         interceptor.doBefore(context);
-        uriNew = (URI)context.getArguments()[0];
+        uriNew = (URI) context.getArguments()[0];
         Assert.assertEquals(url, uriNew.toString());
     }
 
     @Test
     public void rebuildUriTest() {
         Optional<Method> method = ReflectUtils.findMethod(RestTemplateInterceptor.class, "rebuildUri",
-                new Class[] {String.class, URI.class});
+            new Class[]{String.class, URI.class});
         URI uri = createURI(url);
         if (method.isPresent()) {
             Optional<Object> uriNew = ReflectUtils
-                    .invokeMethod(interceptor, method.get(), new Object[] {convertUrl, uri});
+                .invokeMethod(interceptor, method.get(), new Object[]{convertUrl, uri});
             Assert.assertEquals(convertUrl, uriNew.get().toString());
         }
     }
@@ -127,12 +126,12 @@ public class RestTemplateInterceptorTest extends BaseTest {
     @Test
     public void buildErrorResponseTest() throws IOException {
         Optional<Method> method = ReflectUtils.findMethod(RestTemplateInterceptor.class, "buildErrorResponse",
-                new Class[] {Exception.class});
+            new Class[]{Exception.class});
         Exception ex = new Exception();
         if (method.isPresent()) {
             Optional<Object> exception = ReflectUtils
-                    .invokeMethod(interceptor, method.get(), new Object[] {ex});
-            Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ((ClientHttpResponse)exception.get()).getStatusCode());
+                .invokeMethod(interceptor, method.get(), new Object[]{ex});
+            Assert.assertEquals(ex, exception.get());
         }
     }
 }