diff --git a/motan-core/src/main/java/com/weibo/api/motan/config/ConfigUtil.java b/motan-core/src/main/java/com/weibo/api/motan/config/ConfigUtil.java index 594982b92..259f96a45 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/config/ConfigUtil.java +++ b/motan-core/src/main/java/com/weibo/api/motan/config/ConfigUtil.java @@ -1,17 +1,15 @@ /* - * Copyright 2009-2016 Weibo, Inc. - * - * Licensed 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. + * Copyright 2009-2016 Weibo, Inc. + * + * Licensed 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 com.weibo.api.motan.config; @@ -59,15 +57,17 @@ public static Map parseExport(String export) { if (ppDetail.length == 2) { pps.put(ppDetail[0], Integer.parseInt(ppDetail[1])); } else if (ppDetail.length == 1) { - if(MotanConstants.PROTOCOL_INJVM.equals(ppDetail[0])){ + if (MotanConstants.PROTOCOL_INJVM.equals(ppDetail[0])) { pps.put(ppDetail[0], MotanConstants.DEFAULT_INT_VALUE); - } else{ + } else { int port = MathUtil.parseInt(ppDetail[0], 0); - if(port <= 0){ + if (port <= 0) { throw new MotanServiceException("Export is malformed :" + export); + } else { + pps.put(MotanConstants.PROTOCOL_MOTAN, port); } } - + } else { throw new MotanServiceException("Export is malformed :" + export); } diff --git a/motan-core/src/main/java/com/weibo/api/motan/exception/MotanAbstractException.java b/motan-core/src/main/java/com/weibo/api/motan/exception/MotanAbstractException.java index 64a364bd2..27307ece7 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/exception/MotanAbstractException.java +++ b/motan-core/src/main/java/com/weibo/api/motan/exception/MotanAbstractException.java @@ -16,6 +16,8 @@ package com.weibo.api.motan.exception; +import com.weibo.api.motan.rpc.RpcContext; + /** * @author maijunsheng @@ -82,9 +84,8 @@ public String getMessage() { message = motanErrorMsg.getMessage(); } - // TODO 统一上下文 requestid return "error_message: " + message + ", status: " + motanErrorMsg.getStatus() + ", error_code: " + motanErrorMsg.getErrorCode() - + ",r="; + + ",r=" + RpcContext.getContext().getRequestId(); } public int getStatus() { diff --git a/motan-core/src/main/java/com/weibo/api/motan/filter/AccessStatisticFilter.java b/motan-core/src/main/java/com/weibo/api/motan/filter/AccessStatisticFilter.java index a86b9b348..682563d0b 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/filter/AccessStatisticFilter.java +++ b/motan-core/src/main/java/com/weibo/api/motan/filter/AccessStatisticFilter.java @@ -65,14 +65,14 @@ public Response filter(Caller caller, Request request) { } Application application; + String statName = + caller.getUrl().getProtocol() + MotanConstants.PROTOCOL_SEPARATOR + MotanFrameworkUtil.getGroupMethodString(request); if (caller instanceof Provider) { application = new Application(ApplicationInfo.STATISTIC, "rpc_service"); - } else { - application = ApplicationInfo.getApplication(caller.getUrl()); + StatsUtil.accessStatistic(statName, application, end, end - start, bizProcessTime, accessStatus); } - StatsUtil.accessStatistic( - caller.getUrl().getProtocol() + MotanConstants.PROTOCOL_SEPARATOR + MotanFrameworkUtil.getFullMethodString(request), - application, end, end - start, bizProcessTime, accessStatus); + application = ApplicationInfo.getApplication(caller.getUrl()); + StatsUtil.accessStatistic(statName, application, end, end - start, bizProcessTime, accessStatus); } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/log/DefaultLogService.java b/motan-core/src/main/java/com/weibo/api/motan/log/DefaultLogService.java index 214fa3c94..4e811b5f4 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/log/DefaultLogService.java +++ b/motan-core/src/main/java/com/weibo/api/motan/log/DefaultLogService.java @@ -112,6 +112,11 @@ public boolean isTraceEnabled() { public boolean isDebugEnabled() { return debug.isDebugEnabled(); } + + @Override + public boolean isInfoEnabled() { + return info.isInfoEnabled(); + } @Override public boolean isWarnEnabled() { @@ -127,4 +132,6 @@ public boolean isErrorEnabled() { public boolean isStatsEnabled() { return serviceStats.isInfoEnabled(); } + + } diff --git a/motan-core/src/main/java/com/weibo/api/motan/log/LogService.java b/motan-core/src/main/java/com/weibo/api/motan/log/LogService.java index df597e4f8..b199f77d3 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/log/LogService.java +++ b/motan-core/src/main/java/com/weibo/api/motan/log/LogService.java @@ -64,6 +64,8 @@ public interface LogService { boolean isTraceEnabled(); boolean isDebugEnabled(); + + boolean isInfoEnabled(); boolean isWarnEnabled(); diff --git a/motan-core/src/main/java/com/weibo/api/motan/protocol/mock/AbstractMockRpcProtocol.java b/motan-core/src/main/java/com/weibo/api/motan/protocol/mock/AbstractMockRpcProtocol.java new file mode 100644 index 000000000..119a86f41 --- /dev/null +++ b/motan-core/src/main/java/com/weibo/api/motan/protocol/mock/AbstractMockRpcProtocol.java @@ -0,0 +1,149 @@ +package com.weibo.api.motan.protocol.mock; + +import com.weibo.api.motan.common.URLParamType; +import com.weibo.api.motan.core.extension.ExtensionLoader; +import com.weibo.api.motan.exception.MotanFrameworkException; +import com.weibo.api.motan.protocol.AbstractProtocol; +import com.weibo.api.motan.rpc.AbstractExporter; +import com.weibo.api.motan.rpc.AbstractReferer; +import com.weibo.api.motan.rpc.Exporter; +import com.weibo.api.motan.rpc.Provider; +import com.weibo.api.motan.rpc.Referer; +import com.weibo.api.motan.rpc.Request; +import com.weibo.api.motan.rpc.Response; +import com.weibo.api.motan.rpc.URL; +import com.weibo.api.motan.transport.Channel; +import com.weibo.api.motan.transport.EndpointFactory; +import com.weibo.api.motan.transport.ProviderMessageRouter; +import com.weibo.api.motan.transport.Server; +import com.weibo.api.motan.util.LoggerUtil; +import com.weibo.api.motan.util.MotanFrameworkUtil; + +/** + * + * @Description:abstract mock protocol, it can mock all rpc from server or client. + * implementation class must implement 'processRequest()' method, and declare SpiMeta annotation. + * @author zhanglei28 + * @date 2016-3-14 + * + */ +public abstract class AbstractMockRpcProtocol extends AbstractProtocol { + private static ProviderMessageRouter mockProviderMessageRouter; + + @Override + protected Exporter createExporter(Provider provider, URL url) { + Exporter exporter = new MockRpcExporter(provider, url); + LoggerUtil.info("create MockRpcExporter: url={}", url); + return exporter; + } + + @Override + protected Referer createReferer(Class clz, URL url, URL serviceUrl) { + Referer referer = new MockRpcReferer(clz, url, serviceUrl); + LoggerUtil.info("create MockRpcReferer: url={}", url); + return referer; + } + + class MockRpcExporter extends AbstractExporter { + private Server server; + private EndpointFactory endpointFactory; + + public MockRpcExporter(Provider provider, URL url) { + super(provider, url); + + ProviderMessageRouter requestRouter = getMockProviderMessageRouter(url); + endpointFactory = + ExtensionLoader.getExtensionLoader(EndpointFactory.class).getExtension( + url.getParameter(URLParamType.endpointFactory.getName(), URLParamType.endpointFactory.getValue())); + server = endpointFactory.createServer(url, requestRouter); + } + + @Override + public void unexport() { + String protocolKey = MotanFrameworkUtil.getProtocolKey(url); + Exporter exporter = (Exporter) exporterMap.remove(protocolKey); + if (exporter != null) { + exporter.destroy(); + } + } + + @Override + public void destroy() { + endpointFactory.safeReleaseResource(server, url); + LoggerUtil.info("MockRpcExporter destory Success: url={}", url); + } + + @Override + protected boolean doInit() { + return server.open(); + } + + @Override + public boolean isAvailable() { + return server.isAvailable(); + } + + } + + + class MockRpcReferer extends AbstractReferer { + + + public MockRpcReferer(Class clz, URL url, URL serviceUrl) { + super(clz, url, serviceUrl); + } + + + @Override + public void destroy() { + LoggerUtil.info("MockRpcReferer destroy Success: url={}", url); + } + + @Override + protected Response doCall(Request request) { + return processRequest(request); + } + + @Override + protected boolean doInit() { + LoggerUtil.info("MockRpcReferer init Success: url={}", url); + return true; + } + + } + + // process all urls。 + public ProviderMessageRouter getMockProviderMessageRouter(URL url) { + if (mockProviderMessageRouter == null) { + //default + mockProviderMessageRouter = new MockProviderMessageRouter(); + } + + return mockProviderMessageRouter; + } + + class MockProviderMessageRouter extends ProviderMessageRouter { + + @Override + public Object handle(Channel channel, Object message) { + if (channel == null || message == null) { + throw new MotanFrameworkException("RequestRouter handler(channel, message) params is null"); + } + + if (!(message instanceof Request)) { + throw new MotanFrameworkException("RequestRouter message type not support: " + message.getClass()); + } + return processRequest((Request) message); + } + + } + + /** + * process request. request is mock processed by client or server + * + * @param request + * @return + */ + protected abstract Response processRequest(Request request); + +} diff --git a/motan-core/src/main/java/com/weibo/api/motan/rpc/ApplicationInfo.java b/motan-core/src/main/java/com/weibo/api/motan/rpc/ApplicationInfo.java index 5cc1adbe0..02a60242b 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/rpc/ApplicationInfo.java +++ b/motan-core/src/main/java/com/weibo/api/motan/rpc/ApplicationInfo.java @@ -38,13 +38,15 @@ public class ApplicationInfo { public static Application getApplication(URL url) { Application application = applications.get(url.getPath()); if (application == null && MotanConstants.NODE_TYPE_REFERER.equals(url.getParameter(URLParamType.nodeType.getName()))) { - String app = url.getParameter(URLParamType.application.getName(), URLParamType.application.getValue()) + CLIENT; - String module = url.getParameter(URLParamType.module.getName(), URLParamType.module.getValue()) + CLIENT; - - applications.putIfAbsent(url.getPath() + CLIENT, new Application(app, module)); application = applications.get(url.getPath() + CLIENT); - } + if(application == null){ + String app = url.getParameter(URLParamType.application.getName(), URLParamType.application.getValue()) + CLIENT; + String module = url.getParameter(URLParamType.module.getName(), URLParamType.module.getValue()) + CLIENT; + applications.putIfAbsent(url.getPath() + CLIENT, new Application(app, module)); + application = applications.get(url.getPath() + CLIENT); + } + } return application; } diff --git a/motan-core/src/main/java/com/weibo/api/motan/rpc/RpcContext.java b/motan-core/src/main/java/com/weibo/api/motan/rpc/RpcContext.java index 22814297c..b55d2fc1d 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/rpc/RpcContext.java +++ b/motan-core/src/main/java/com/weibo/api/motan/rpc/RpcContext.java @@ -19,6 +19,8 @@ import java.util.HashMap; import java.util.Map; +import com.weibo.api.motan.common.URLParamType; + /** * rpc session context * @@ -26,9 +28,12 @@ * */ public class RpcContext { - private Map attribute = new HashMap(1); + private Map attribute = new HashMap(); + private Request request; + private Response response; + private String clientRequestId = null; - private static ThreadLocal localContext = new ThreadLocal() { + private static final ThreadLocal localContext = new ThreadLocal() { protected RpcContext initialValue() { return new RpcContext(); } @@ -37,12 +42,72 @@ protected RpcContext initialValue() { public static RpcContext getContext() { return localContext.get(); } + + /** + * init new rpcContext with request + * @param request + * @return + */ + public static RpcContext init(Request request){ + RpcContext context = new RpcContext(); + if(request != null){ + context.setRequest(request); + context.setClientRequestId(request.getAttachments().get(URLParamType.requestIdFromClient.getName())); + } + localContext.set(context); + return context; + } public static void destroy() { - localContext.set(null); + localContext.remove(); } + /** + * clientRequestId > request.id + * @return + */ + public String getRequestId(){ + if(clientRequestId != null){ + return clientRequestId; + } else{ + return request == null ? null : String.valueOf(request.getRequestId()); + } + } + + public void putAttribute(Object key, Object value){ + attribute.put(key, value); + } + public Object getAttribute(Object key) { return attribute.get(key); } + + public void revomeAttribute(Object key){ + attribute.remove(key); + } + + public Request getRequest() { + return request; + } + + public void setRequest(Request request) { + this.request = request; + } + + public Response getResponse() { + return response; + } + + public void setResponse(Response response) { + this.response = response; + } + + public String getClientRequestId() { + return clientRequestId; + } + + public void setClientRequestId(String clientRequestId) { + this.clientRequestId = clientRequestId; + } + } diff --git a/motan-core/src/main/java/com/weibo/api/motan/rpc/init/Initializable.java b/motan-core/src/main/java/com/weibo/api/motan/rpc/init/Initializable.java new file mode 100644 index 000000000..610a090a8 --- /dev/null +++ b/motan-core/src/main/java/com/weibo/api/motan/rpc/init/Initializable.java @@ -0,0 +1,30 @@ +/* + * Copyright 2009-2016 Weibo, Inc. + * + * Licensed 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 com.weibo.api.motan.rpc.init; + +import com.weibo.api.motan.core.extension.Scope; +import com.weibo.api.motan.core.extension.Spi; + +/** + * + * @Description Initializable. the implement must be thread safe! + * @author zhanglei + * @date 2016-6-15 + * + */ +@Spi(scope = Scope.SINGLETON) +public interface Initializable { + void init(); + +} diff --git a/motan-core/src/main/java/com/weibo/api/motan/rpc/init/InitializationFactory.java b/motan-core/src/main/java/com/weibo/api/motan/rpc/init/InitializationFactory.java new file mode 100644 index 000000000..f635f6cf4 --- /dev/null +++ b/motan-core/src/main/java/com/weibo/api/motan/rpc/init/InitializationFactory.java @@ -0,0 +1,67 @@ +/* + * Copyright 2009-2016 Weibo, Inc. + * + * Licensed 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 com.weibo.api.motan.rpc.init; + +import java.util.List; + +import com.weibo.api.motan.core.extension.ExtensionLoader; +import com.weibo.api.motan.util.LoggerUtil; + +/** + * + * @Description Initializable factory. Initializable.init() will be called before spring bean init. + * U can custom Initializable by SPI to do smthing before rpc start. all Initializable + * will be init in default. U can set sequence by annotation @see + * com.weibo.api.motan.core.extension.Activation + * @author zhanglei + * @date 2016-6-15 + * + */ +public class InitializationFactory { + private static boolean inited = false; + private static Initializable instance = new AllSpiInitialization(); + + public static Initializable getInitialization() { + return instance; + } + + static class AllSpiInitialization implements Initializable { + + @Override + // find all initilizable spi and init it. + public synchronized void init() { + if (!inited) { + try { + LoggerUtil.info("AllSpiInitialization init."); + ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(Initializable.class); + List allInit = extensionLoader.getExtensions(null); + if (allInit != null && !allInit.isEmpty()) { + for (Initializable initializable : allInit) { + try { + initializable.init(); + LoggerUtil.info(initializable.getClass().getName() + " is init."); + } catch (Exception initErr) { + LoggerUtil.error(initializable.getClass().getName() + " init fail!", initErr); + } + } + } + inited = true; + LoggerUtil.info("AllSpiInitialization init finish."); + } catch (Exception e) { + LoggerUtil.error("Initializable spi init fail!", e);; + } + } + } + } +} diff --git a/motan-core/src/main/java/com/weibo/api/motan/util/MotanFrameworkUtil.java b/motan-core/src/main/java/com/weibo/api/motan/util/MotanFrameworkUtil.java index 0b506fb57..4671730db 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/util/MotanFrameworkUtil.java +++ b/motan-core/src/main/java/com/weibo/api/motan/util/MotanFrameworkUtil.java @@ -112,9 +112,13 @@ public static String toString(Request request) { * @return */ public static String getFullMethodString(Request request) { - return getGroupFromRequest(request) + "_" + request.getInterfaceName() + "." + request.getMethodName() + "(" + return request.getInterfaceName() + "." + request.getMethodName() + "(" + request.getParamtersDesc() + ")"; } + + public static String getGroupMethodString(Request request){ + return getGroupFromRequest(request) + "_" + getFullMethodString(request); + } /** diff --git a/motan-core/src/main/java/com/weibo/api/motan/util/StatsUtil.java b/motan-core/src/main/java/com/weibo/api/motan/util/StatsUtil.java index 67755e7d6..92bce4011 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/util/StatsUtil.java +++ b/motan-core/src/main/java/com/weibo/api/motan/util/StatsUtil.java @@ -38,13 +38,14 @@ */ public class StatsUtil { - public static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); - protected static final ConcurrentMap accessStatistics = new ConcurrentHashMap(); - protected static final List statisticCallbacks = new CopyOnWriteArrayList(); - private static String SEPARATE = "\\|"; + public static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); + protected static ConcurrentMap accessStatistics = new ConcurrentHashMap(); + protected static List statisticCallbacks = new CopyOnWriteArrayList(); + public static String SEPARATE = "\\|"; + protected static ScheduledFuture scheduledFuture; static { - executorService.scheduleAtFixedRate(new Runnable() { + scheduledFuture = executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { diff --git a/motan-extension/protocol-extension/motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/YarProtocolUtil.java b/motan-extension/protocol-extension/motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/YarProtocolUtil.java index c9f07e4d6..9e5ef01d6 100644 --- a/motan-extension/protocol-extension/motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/YarProtocolUtil.java +++ b/motan-extension/protocol-extension/motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/YarProtocolUtil.java @@ -209,6 +209,7 @@ private static Object[] adaptParams(Method method, Object[] arguments, Class[ + argumentClazz[i].getName() + ", actual param:" + (arguments[i] == null ? null : arguments[i].getClass().getName() + "-" + arguments[i])); } + } return arguments; } diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java index a3876c49f..44c47eed6 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java @@ -10,8 +10,11 @@ import com.weibo.api.motan.config.springsupport.annotation.MotanReferer; import com.weibo.api.motan.config.springsupport.annotation.MotanService; import com.weibo.api.motan.config.springsupport.util.SpringBeanUtil; +import com.weibo.api.motan.rpc.init.Initializable; +import com.weibo.api.motan.rpc.init.InitializationFactory; import com.weibo.api.motan.util.ConcurrentHashSet; import com.weibo.api.motan.util.LoggerUtil; + import org.apache.commons.lang3.StringUtils; import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; @@ -62,7 +65,11 @@ public AnnotationBean() { private final Set> serviceConfigs = new ConcurrentHashSet>(); private final ConcurrentMap referenceConfigs = new ConcurrentHashMap(); - + static{ + //custom Initializable before motan beans inited + Initializable initialization = InitializationFactory.getInitialization(); + initialization.init(); + } /** * @param beanFactory diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanBeanDefinitionParser.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanBeanDefinitionParser.java index f329c44b1..6976ab2fa 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanBeanDefinitionParser.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanBeanDefinitionParser.java @@ -144,13 +144,6 @@ private static BeanDefinition parse(Element element, ParserContext parserContext continue; } String value = element.getAttribute(property); - if (StringUtils.isBlank(value) && "protocol".equals(property)) { - // srevice中的protocol信息是隐含在export中,所以需要从export中获取protocol来配置 - String exportValue = element.getAttribute(URLParamType.export.getName()); - if (!StringUtils.isBlank(exportValue)) { - value = ConfigUtil.extractProtocols(exportValue); - } - } if ("methods".equals(property)) { parseMethods(id, element.getChildNodes(), bd, parserContext); } @@ -171,7 +164,7 @@ private static BeanDefinition parse(Element element, ParserContext parserContext } } reference = new RuntimeBeanReference(value); - } else if ("protocol".equals(property)) { + } else if ("protocol".equals(property) && !StringUtils.isBlank(value)) { if (!value.contains(",")) { reference = new RuntimeBeanReference(value); } else { diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanNamespaceHandler.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanNamespaceHandler.java index 31ad2677e..5767ecb0e 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanNamespaceHandler.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/MotanNamespaceHandler.java @@ -24,6 +24,8 @@ import com.weibo.api.motan.config.BasicServiceInterfaceConfig; import com.weibo.api.motan.config.ProtocolConfig; import com.weibo.api.motan.config.RegistryConfig; +import com.weibo.api.motan.rpc.init.Initializable; +import com.weibo.api.motan.rpc.init.InitializationFactory; import com.weibo.api.motan.util.ConcurrentHashSet; public class MotanNamespaceHandler extends NamespaceHandlerSupport { @@ -42,6 +44,7 @@ public void init() { registerBeanDefinitionParser("basicReferer", new MotanBeanDefinitionParser(BasicRefererInterfaceConfig.class, true)); registerBeanDefinitionParser("spi", new MotanBeanDefinitionParser(SpiConfigBean.class, true)); registerBeanDefinitionParser("annotation", new MotanBeanDefinitionParser(AnnotationBean.class, true)); - + Initializable initialization = InitializationFactory.getInitialization(); + initialization.init(); } } diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java index 1ba55851e..5efd05141 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java @@ -17,8 +17,12 @@ package com.weibo.api.motan.config.springsupport; import java.util.ArrayList; + +import java.util.List; +import java.util.Map; import java.util.Arrays; + import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; @@ -32,13 +36,13 @@ import com.weibo.api.motan.common.MotanConstants; import com.weibo.api.motan.config.BasicServiceInterfaceConfig; +import com.weibo.api.motan.config.ConfigUtil; import com.weibo.api.motan.config.ProtocolConfig; import com.weibo.api.motan.config.RegistryConfig; import com.weibo.api.motan.config.ServiceConfig; import com.weibo.api.motan.exception.MotanErrorMsgConstant; import com.weibo.api.motan.exception.MotanFrameworkException; import com.weibo.api.motan.util.CollectionUtil; -import com.weibo.api.motan.util.MathUtil; import com.weibo.api.motan.util.MotanFrameworkUtil; public class ServiceConfigBean extends ServiceConfig @@ -132,11 +136,24 @@ private void checkAndConfigExport() { setProtocols(new ArrayList(getBasicServiceConfig().getProtocols())); } } - if (CollectionUtil.isEmpty(getProtocols()) && StringUtils.isNotEmpty(getExport())) { - int port = MathUtil.parseInt(export, 0); - if (port > 0) { - export = MotanConstants.PROTOCOL_MOTAN + ":" + export; - setProtocol(MotanFrameworkUtil.getDefaultProtocolConfig()); + + if(CollectionUtil.isEmpty(getProtocols()) && StringUtils.isNotEmpty(getExport())){ + Map exportMap = ConfigUtil.parseExport(export); + if(!exportMap.isEmpty()){ + List protos = new ArrayList(); + for (String p : exportMap.keySet()) { + ProtocolConfig proto = beanFactory.getBean(p, ProtocolConfig.class); + if(proto == null){ + if(MotanConstants.PROTOCOL_MOTAN.equals(p)){ + proto = MotanFrameworkUtil.getDefaultProtocolConfig(); + } else{ + throw new MotanFrameworkException(String.format("cann't find %s ProtocolConfig bean! export:%s", p, export), + MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR); + } + } + protos.add(proto); + } + setProtocols(protos); } } if (StringUtils.isEmpty(getExport()) || CollectionUtil.isEmpty(getProtocols())) { diff --git a/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannelHandler.java b/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannelHandler.java index 604926f41..1655cd949 100644 --- a/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannelHandler.java +++ b/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannelHandler.java @@ -32,6 +32,7 @@ import com.weibo.api.motan.rpc.Request; import com.weibo.api.motan.rpc.Response; import com.weibo.api.motan.rpc.DefaultResponse; +import com.weibo.api.motan.rpc.RpcContext; import com.weibo.api.motan.transport.Channel; import com.weibo.api.motan.transport.MessageHandler; import com.weibo.api.motan.util.LoggerUtil; @@ -110,21 +111,12 @@ private void processRequest(final ChannelHandlerContext ctx, MessageEvent e) { threadPoolExecutor.execute(new Runnable() { @Override public void run() { - // TODO 线程上下文初始化(requestIdFromClient) - long requestId = 0; - try { - if (request.getAttachments() == null || !request.getAttachments().containsKey(URLParamType.requestIdFromClient.getName())) { - requestId = 0; - } else { - requestId = Long.valueOf(request.getAttachments().get(URLParamType.requestIdFromClient.getName())); - } - } catch (Exception e) { - LoggerUtil.error("Transfer request id error!", e.getCause()); - } - //TODO 上下文 -// RequestTraceContext.init(requestId); - processRequest(ctx, request, processStartTime); -// RequestTraceContext.finish(); + try{ + RpcContext.init(request); + processRequest(ctx, request, processStartTime); + }finally{ + RpcContext.destroy(); + } } }); } catch (RejectedExecutionException rejectException) {