diff --git a/infra-sofa-boot-starter/src/main/resources/META-INF/com/alipay/sofa/infra/config/spring/namespace/schema/rpc.xsd b/infra-sofa-boot-starter/src/main/resources/META-INF/com/alipay/sofa/infra/config/spring/namespace/schema/rpc.xsd index 332a2697d..6d7626b15 100644 --- a/infra-sofa-boot-starter/src/main/resources/META-INF/com/alipay/sofa/infra/config/spring/namespace/schema/rpc.xsd +++ b/infra-sofa-boot-starter/src/main/resources/META-INF/com/alipay/sofa/infra/config/spring/namespace/schema/rpc.xsd @@ -127,6 +127,10 @@ + + + + @@ -141,6 +145,10 @@ + + + + @@ -567,7 +575,7 @@ - + @@ -590,7 +598,7 @@ - + diff --git a/infra-sofa-boot-starter/src/test/java/com/alipay/sofa/infra/base/SofaBootWebSpringBootApplication.java b/infra-sofa-boot-starter/src/test/java/com/alipay/sofa/infra/base/SofaBootWebSpringBootApplication.java index 147011b5d..98183e767 100644 --- a/infra-sofa-boot-starter/src/test/java/com/alipay/sofa/infra/base/SofaBootWebSpringBootApplication.java +++ b/infra-sofa-boot-starter/src/test/java/com/alipay/sofa/infra/base/SofaBootWebSpringBootApplication.java @@ -19,7 +19,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; -import org.springframework.context.annotation.PropertySource; /** * SofaBootWebSpringBootApplication diff --git a/runtime-sofa-boot-plugin/pom.xml b/runtime-sofa-boot-plugin/pom.xml index b08952e31..cb53a2b3f 100644 --- a/runtime-sofa-boot-plugin/pom.xml +++ b/runtime-sofa-boot-plugin/pom.xml @@ -78,7 +78,6 @@ com.alipay.sofa.runtime.spi.health com.alipay.sofa.runtime.spi.log com.alipay.sofa.runtime.spi.binding - com.alipay.sofa.runtime.spi.service com.alipay.sofa.runtime.spi.util org.aopalliance.aop org.aopalliance.intercept @@ -88,6 +87,8 @@ com.alipay.sofa.runtime.service.binding.JvmBinding com.alipay.sofa.runtime.SofaFramework com.alipay.sofa.runtime.SofaRuntimeProperties + com.alipay.sofa.runtime.service.binding.JvmBindingParam + com.alipay.sofa.runtime.spi.service.ServiceProxy diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java index 3de1eda97..1868a03a1 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java @@ -127,4 +127,14 @@ * @return parameters of consumer */ SofaParameter[] parameters() default {}; + + /** + * serialization between biz, default is false. + * only serialize of reference and service is false + * then invocation between biz would skip serialization + * Note that the serialize of {@link SofaServiceBinding} is true + * + * @return + */ + boolean serialize() default false; } diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java index 80c8efbdf..9e2b81964 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java @@ -89,4 +89,15 @@ * @return parameters of service */ SofaParameter[] parameters() default {}; + + /** + * serialization between biz, default is true. + * only serialize of reference and service is false + * then invocation between biz would skip serialization + * + * Note that the serialize of {@link SofaReferenceBinding} is false + * + * @return + */ + boolean serialize() default true; } diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/integration/invoke/DynamicJvmServiceProxyFinder.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/integration/invoke/DynamicJvmServiceProxyFinder.java index 7da86b25b..90498d87d 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/integration/invoke/DynamicJvmServiceProxyFinder.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/integration/invoke/DynamicJvmServiceProxyFinder.java @@ -20,6 +20,7 @@ import com.alipay.sofa.ark.spi.model.BizState; import com.alipay.sofa.ark.spi.service.ArkInject; import com.alipay.sofa.ark.spi.service.biz.BizManagerService; +import com.alipay.sofa.runtime.service.binding.JvmBinding; import com.alipay.sofa.runtime.service.component.ServiceComponent; import com.alipay.sofa.runtime.SofaFramework; import com.alipay.sofa.runtime.spi.binding.Contract; @@ -70,9 +71,15 @@ public ServiceProxy findServiceProxy(ClassLoader clientClassloader, Contract con ServiceComponent serviceComponent = findServiceComponent(uniqueId, interfaceType, sofaRuntimeManager.getComponentManager()); if (serviceComponent != null) { + JvmBinding referenceJvmBinding = (JvmBinding) contract + .getBinding(JvmBinding.JVM_BINDING_TYPE); + JvmBinding serviceJvmBinding = (JvmBinding) serviceComponent.getService() + .getBinding(JvmBinding.JVM_BINDING_TYPE); + boolean serialize = referenceJvmBinding.getJvmBindingParam().isSerialize() + || serviceJvmBinding.getJvmBindingParam().isSerialize(); return new DynamicJvmServiceInvoker(clientClassloader, sofaRuntimeManager.getAppClassLoader(), serviceComponent.getService() - .getTarget(), contract, biz.getIdentity()); + .getTarget(), contract, biz.getIdentity(), serialize); } } } @@ -123,6 +130,7 @@ static class DynamicJvmServiceInvoker extends ServiceProxy { private Object targetService; private String bizIdentity; private ThreadLocal clientClassloader = new ThreadLocal<>(); + private boolean serialize; static protected final String TOSTRING_METHOD = "toString"; static protected final String EQUALS_METHOD = "equals"; @@ -130,12 +138,13 @@ static class DynamicJvmServiceInvoker extends ServiceProxy { public DynamicJvmServiceInvoker(ClassLoader clientClassloader, ClassLoader serviceClassLoader, Object targetService, - Contract contract, String bizIdentity) { + Contract contract, String bizIdentity, boolean serialize) { super(serviceClassLoader); this.clientClassloader.set(clientClassloader); this.targetService = targetService; this.contract = contract; this.bizIdentity = bizIdentity; + this.serialize = serialize; } @Override @@ -148,6 +157,16 @@ protected Object doInvoke(MethodInvocation invocation) throws Throwable { .debug(">> Start in Cross App JVM service invoke, the service interface is - " + getInterfaceType()); + if (!serialize) { + ClassLoader tcl = Thread.currentThread().getContextClassLoader(); + try { + pushThreadContextClassLoader(getServiceClassLoader()); + return targetMethod.invoke(targetService, targetArguments); + } finally { + pushThreadContextClassLoader(tcl); + } + } + if (TOSTRING_METHOD.equalsIgnoreCase(targetMethod.getName()) && targetMethod.getParameterTypes().length == 0) { return targetService.toString(); diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBinding.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBinding.java index 5446aa260..0d2a4ab25 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBinding.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBinding.java @@ -31,10 +31,20 @@ public class JvmBinding extends AbstractBinding { /** * binding type: JVM */ - public static BindingType JVM_BINDING_TYPE = new BindingType("jvm"); + public static BindingType JVM_BINDING_TYPE = new BindingType(XmlConstants.BINDING_TYPE); + + private JvmBindingParam jvmBindingParam = new JvmBindingParam(); public JvmBinding() { + } + + public JvmBindingParam getJvmBindingParam() { + return jvmBindingParam; + } + public JvmBinding setJvmBindingParam(JvmBindingParam jvmBindingParam) { + this.jvmBindingParam = jvmBindingParam; + return this; } /** @@ -85,4 +95,10 @@ public HealthResult healthCheck() { healthResult.setHealthy(isHealthy); return healthResult; } + + public static class XmlConstants { + public static String SERIALIZE = "serialize"; + public static String SUPPORT_TAG_NAME = "binding.jvm"; + public static String BINDING_TYPE = "jvm"; + } } \ No newline at end of file diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBindingConverter.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBindingConverter.java new file mode 100644 index 000000000..1d4b352f0 --- /dev/null +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBindingConverter.java @@ -0,0 +1,84 @@ +/* + * 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 com.alipay.sofa.runtime.service.binding; + +import com.alipay.sofa.runtime.api.annotation.SofaReference; +import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding; +import com.alipay.sofa.runtime.api.annotation.SofaService; +import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding; +import com.alipay.sofa.runtime.api.binding.BindingType; +import com.alipay.sofa.runtime.spi.service.BindingConverter; +import com.alipay.sofa.runtime.spi.service.BindingConverterContext; +import org.w3c.dom.Element; + +/** + * @author qilong.zql + * @since 3.1.3 + */ +public class JvmBindingConverter implements BindingConverter { + @Override + public JvmBinding convert(JvmBindingParam bindingParam, + BindingConverterContext bindingConverterContext) { + return new JvmBinding().setJvmBindingParam(bindingParam); + } + + @Override + public JvmBinding convert(Element element, BindingConverterContext bindingConverterContext) { + JvmBindingParam jvmBindingParam = new JvmBindingParam(); + if (element != null) { + jvmBindingParam.setSerialize(Boolean.TRUE.toString().equalsIgnoreCase( + element.getAttribute(JvmBinding.XmlConstants.SERIALIZE))); + } + return new JvmBinding().setJvmBindingParam(jvmBindingParam); + } + + @Override + public JvmBinding convert(SofaService sofaServiceAnnotation, + SofaServiceBinding sofaServiceBindingAnnotation, + BindingConverterContext bindingConverterContext) { + if (JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaServiceBindingAnnotation.bindingType())) { + JvmBindingParam jvmBindingParam = new JvmBindingParam(); + jvmBindingParam.setSerialize(sofaServiceBindingAnnotation.serialize()); + return new JvmBinding().setJvmBindingParam(jvmBindingParam); + } + return null; + } + + @Override + public JvmBinding convert(SofaReference sofaReferenceAnnotation, + SofaReferenceBinding sofaReferenceBindingAnnotation, + BindingConverterContext bindingConverterContext) { + if (JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBindingAnnotation + .bindingType())) { + JvmBindingParam jvmBindingParam = new JvmBindingParam(); + jvmBindingParam.setSerialize(sofaReferenceBindingAnnotation.serialize()); + return new JvmBinding().setJvmBindingParam(jvmBindingParam); + } + return null; + } + + @Override + public BindingType supportBindingType() { + return JvmBinding.JVM_BINDING_TYPE; + } + + @Override + public String supportTagName() { + return JvmBinding.XmlConstants.SUPPORT_TAG_NAME; + } + +} \ No newline at end of file diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBindingParam.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBindingParam.java new file mode 100644 index 000000000..48bdb1d04 --- /dev/null +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/binding/JvmBindingParam.java @@ -0,0 +1,53 @@ +/* + * 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 com.alipay.sofa.runtime.service.binding; + +import com.alipay.sofa.runtime.api.binding.BindingType; +import com.alipay.sofa.runtime.api.client.param.BindingParam; + +/** + * @author qilong.zql + * @since 3.1.2 + */ +public class JvmBindingParam implements BindingParam { + + private boolean serialize = true; + + @Override + public BindingType getBindingType() { + return JvmBinding.JVM_BINDING_TYPE; + } + + /** + * whether ignore serialize when invoke across ClassLoader. + * + * @return + */ + public boolean isSerialize() { + return serialize; + } + + /** + * Set whether ignore serialize when invoke across ClassLoader. + * + * @param serialize + */ + public JvmBindingParam setSerialize(boolean serialize) { + this.serialize = serialize; + return this; + } +} \ No newline at end of file diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/client/ReferenceClientImpl.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/client/ReferenceClientImpl.java index 1c7c91455..d49a70a6c 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/client/ReferenceClientImpl.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/client/ReferenceClientImpl.java @@ -23,6 +23,7 @@ import com.alipay.sofa.runtime.api.component.ComponentName; import com.alipay.sofa.runtime.model.InterfaceMode; import com.alipay.sofa.runtime.service.binding.JvmBinding; +import com.alipay.sofa.runtime.service.binding.JvmBindingParam; import com.alipay.sofa.runtime.service.component.Reference; import com.alipay.sofa.runtime.service.component.ReferenceComponent; import com.alipay.sofa.runtime.service.component.impl.ReferenceImpl; @@ -63,8 +64,10 @@ private Reference getReferenceFromReferenceParam(ReferenceParam reference referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isJvmFirst(), null); if (bindingParam == null) { - // add JVM Binding Default - reference.addBinding(new JvmBinding()); + // default add jvm binding and reference jvm binding should set serialize as false + JvmBindingParam jvmBindingParam = new JvmBindingParam(); + jvmBindingParam.setSerialize(false); + reference.addBinding(new JvmBinding().setJvmBindingParam(jvmBindingParam)); } else { BindingConverter bindingConverter = bindingConverterFactory .getBindingConverter(bindingParam.getBindingType()); diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/helper/ReferenceRegisterHelper.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/helper/ReferenceRegisterHelper.java index 12aa0e07c..004c5ca0f 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/helper/ReferenceRegisterHelper.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/service/helper/ReferenceRegisterHelper.java @@ -43,6 +43,7 @@ public static Object registerReference(Reference reference, if (!binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE) && !SofaRuntimeProperties.isDisableJvmFirst(sofaRuntimeContext) && reference.isJvmFirst()) { + // as rpc invocation would be serialized, so here would Not ignore serialized reference.addBinding(new JvmBinding()); } diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java index 9e4155565..c9edba3c3 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java @@ -156,28 +156,21 @@ private Object createReferenceProxy(SofaReference sofaReferenceAnnotation, Class interfaceType) { Reference reference = new ReferenceImpl(sofaReferenceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, sofaReferenceAnnotation.jvmFirst()); - if (JvmBinding.JVM_BINDING_TYPE.getType().equals( - sofaReferenceAnnotation.binding().bindingType())) { - reference.addBinding(new JvmBinding()); - } else { - BindingConverter bindingConverter = bindingConverterFactory - .getBindingConverter(new BindingType(sofaReferenceAnnotation.binding() - .bindingType())); - if (bindingConverter == null) { - throw new ServiceRuntimeException( - "Can not found binding converter for binding type " - + sofaReferenceAnnotation.binding().bindingType()); - } - - BindingConverterContext bindingConverterContext = new BindingConverterContext(); - bindingConverterContext.setInBinding(true); - bindingConverterContext.setApplicationContext(applicationContext); - bindingConverterContext.setAppName(sofaRuntimeContext.getAppName()); - bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader()); - Binding binding = bindingConverter.convert(sofaReferenceAnnotation, - sofaReferenceAnnotation.binding(), bindingConverterContext); - reference.addBinding(binding); + BindingConverter bindingConverter = bindingConverterFactory + .getBindingConverter(new BindingType(sofaReferenceAnnotation.binding().bindingType())); + if (bindingConverter == null) { + throw new ServiceRuntimeException("Can not found binding converter for binding type " + + sofaReferenceAnnotation.binding().bindingType()); } + + BindingConverterContext bindingConverterContext = new BindingConverterContext(); + bindingConverterContext.setInBinding(true); + bindingConverterContext.setApplicationContext(applicationContext); + bindingConverterContext.setAppName(sofaRuntimeContext.getAppName()); + bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader()); + Binding binding = bindingConverter.convert(sofaReferenceAnnotation, + sofaReferenceAnnotation.binding(), bindingConverterContext); + reference.addBinding(binding); return ReferenceRegisterHelper.registerReference(reference, bindingAdapterFactory, sofaRuntimeContext); } diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java index cd67187a1..fdde626ef 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java @@ -27,6 +27,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; @@ -240,6 +241,8 @@ private void doGenerateSofaReferenceDefinition(BeanDefinition beanDefinition, builder.addPropertyValue(AbstractContractDefinitionParser.UNIQUE_ID_PROPERTY, uniqueId); builder.addPropertyValue(AbstractContractDefinitionParser.INTERFACE_CLASS_PROPERTY, interfaceType); + builder.addPropertyValue(AbstractContractDefinitionParser.BINDINGS, + getSofaReferenceBinding(sofaReference, sofaReference.binding())); builder.addPropertyValue(AbstractContractDefinitionParser.DEFINITION_BUILDING_API_TYPE, true); ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(referenceId, @@ -318,26 +321,52 @@ private List getSofaServiceBinding(SofaService sofaServiceAnnotation, SofaServiceBinding[] sofaServiceBindings) { List bindings = new ArrayList<>(); for (SofaServiceBinding sofaServiceBinding : sofaServiceBindings) { - if (JvmBinding.JVM_BINDING_TYPE.getType().equals(sofaServiceBinding.bindingType())) { - bindings.add(new JvmBinding()); - } else { - BindingConverter bindingConverter = bindingConverterFactory - .getBindingConverter(new BindingType(sofaServiceBinding.bindingType())); - if (bindingConverter == null) { - throw new ServiceRuntimeException( - "Can not found binding converter for binding type " - + sofaServiceBinding.bindingType()); - } - BindingConverterContext bindingConverterContext = new BindingConverterContext(); - bindingConverterContext.setInBinding(false); - bindingConverterContext.setApplicationContext(applicationContext); - bindingConverterContext.setAppName(sofaRuntimeContext.getAppName()); - bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader()); - Binding binding = bindingConverter.convert(sofaServiceAnnotation, - sofaServiceBinding, bindingConverterContext); - bindings.add(binding); + BindingConverter bindingConverter = bindingConverterFactory + .getBindingConverter(new BindingType(sofaServiceBinding.bindingType())); + if (bindingConverter == null) { + throw new ServiceRuntimeException( + "Can not found binding converter for binding type " + + sofaServiceBinding.bindingType()); } + BindingConverterContext bindingConverterContext = new BindingConverterContext(); + bindingConverterContext.setInBinding(false); + bindingConverterContext.setApplicationContext(applicationContext); + bindingConverterContext.setAppName(sofaRuntimeContext.getAppName()); + bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader()); + Binding binding = bindingConverter.convert(sofaServiceAnnotation, sofaServiceBinding, + bindingConverterContext); + bindings.add(binding); + } + return bindings; + } + + /** + * get sofa reference binding annotated on parameter. At present, only jvm sofa reference is supported . + * @param sofaReferenceAnnotation + * @param sofaReferenceBinding + * @return + */ + private List getSofaReferenceBinding(SofaReference sofaReferenceAnnotation, + SofaReferenceBinding sofaReferenceBinding) { + if (!JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBinding.bindingType())) { + throw new ServiceRuntimeException( + "Only jvm sofa reference binding is supported to annotate on parameter."); + } + List bindings = new ArrayList<>(); + BindingConverter bindingConverter = bindingConverterFactory + .getBindingConverter(new BindingType(sofaReferenceBinding.bindingType())); + if (bindingConverter == null) { + throw new ServiceRuntimeException("Can not found binding converter for binding type " + + sofaReferenceBinding.bindingType()); } + BindingConverterContext bindingConverterContext = new BindingConverterContext(); + bindingConverterContext.setInBinding(true); + bindingConverterContext.setApplicationContext(applicationContext); + bindingConverterContext.setAppName(sofaRuntimeContext.getAppName()); + bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader()); + Binding binding = bindingConverter.convert(sofaReferenceAnnotation, sofaReferenceBinding, + bindingConverterContext); + bindings.add(binding); return bindings; } diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/AbstractContractFactoryBean.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/AbstractContractFactoryBean.java index 551f4d142..572cca8e0 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/AbstractContractFactoryBean.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/AbstractContractFactoryBean.java @@ -24,7 +24,6 @@ import com.alipay.sofa.runtime.api.ServiceRuntimeException; import com.alipay.sofa.runtime.constants.SofaRuntimeFrameworkConstants; -import com.alipay.sofa.runtime.service.binding.JvmBinding; import com.alipay.sofa.runtime.spi.binding.Binding; import com.alipay.sofa.runtime.spi.binding.BindingAdapterFactory; import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; @@ -147,10 +146,7 @@ protected List parseBindings(List parseElements, } protected void dealWithbindingConverterNotExist(String tagName) { - if (!tagName.equals(SofaRuntimeFrameworkConstants.BINDING_PREFIX - + JvmBinding.JVM_BINDING_TYPE.toString())) { - throw new ServiceRuntimeException("Can't find BindingConverter of type " + tagName); - } + throw new ServiceRuntimeException("Can't find BindingConverter of type " + tagName); } @Override diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ReferenceFactoryBean.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ReferenceFactoryBean.java index 5711f6cc4..302a3ded6 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ReferenceFactoryBean.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ReferenceFactoryBean.java @@ -19,6 +19,7 @@ import com.alipay.sofa.runtime.constants.SofaRuntimeFrameworkConstants; import com.alipay.sofa.runtime.model.InterfaceMode; import com.alipay.sofa.runtime.service.binding.JvmBinding; +import com.alipay.sofa.runtime.service.binding.JvmBindingParam; import com.alipay.sofa.runtime.service.component.Reference; import com.alipay.sofa.runtime.service.component.impl.ReferenceImpl; import com.alipay.sofa.runtime.service.helper.ReferenceRegisterHelper; @@ -50,8 +51,12 @@ protected void doAfterPropertiesSet() throws Exception { .isTrue(bindings.size() <= 1, "Found more than one binding in , can only have one binding."); + // default add jvm binding and reference jvm binding should set serialize as false if (bindings.size() == 0) { - bindings.add(new JvmBinding()); + // default reference prefer to ignore serialize + JvmBindingParam jvmBindingParam = new JvmBindingParam(); + jvmBindingParam.setSerialize(true); + bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam)); } reference.addBinding(bindings.get(0)); diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ServiceFactoryBean.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ServiceFactoryBean.java index f04a29b0b..4daad536f 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ServiceFactoryBean.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/factory/ServiceFactoryBean.java @@ -20,6 +20,7 @@ import com.alipay.sofa.runtime.api.annotation.SofaService; import com.alipay.sofa.runtime.model.InterfaceMode; import com.alipay.sofa.runtime.service.binding.JvmBinding; +import com.alipay.sofa.runtime.service.binding.JvmBindingParam; import com.alipay.sofa.runtime.service.component.Service; import com.alipay.sofa.runtime.service.component.ServiceComponent; import com.alipay.sofa.runtime.service.component.impl.ServiceImpl; @@ -56,8 +57,11 @@ protected void doAfterPropertiesSet() { implementation.setTarget(ref); service = buildService(); + // default add jvm binding and service jvm binding should set serialize as true if (bindings.size() == 0) { - bindings.add(new JvmBinding()); + JvmBinding jvmBinding = new JvmBinding(); + JvmBindingParam jvmBindingParam = new JvmBindingParam().setSerialize(true); + bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam)); } for (Binding binding : bindings) { diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/initializer/SofaRuntimeSpringContextInitializer.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/initializer/SofaRuntimeSpringContextInitializer.java index 06cb82a9b..964462fec 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/initializer/SofaRuntimeSpringContextInitializer.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spring/initializer/SofaRuntimeSpringContextInitializer.java @@ -16,9 +16,7 @@ */ package com.alipay.sofa.runtime.spring.initializer; -import com.alipay.sofa.common.log.Constants; import com.alipay.sofa.infra.constants.SofaBootInfraConstants; -import com.alipay.sofa.infra.log.space.SofaBootLogSpaceIsolationInit; import com.alipay.sofa.infra.utils.SOFABootEnvUtils; import com.alipay.sofa.runtime.SofaFramework; import com.alipay.sofa.runtime.api.client.ReferenceClient; @@ -37,7 +35,6 @@ import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager; import com.alipay.sofa.runtime.spi.log.SofaLogger; -import com.alipay.sofa.runtime.spi.log.SofaRuntimeLoggerFactory; import com.alipay.sofa.runtime.spi.service.BindingConverter; import com.alipay.sofa.runtime.spi.service.BindingConverterFactory; import com.alipay.sofa.runtime.spring.*; diff --git a/runtime-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.runtime.spi.service.BindingConverter b/runtime-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.runtime.spi.service.BindingConverter new file mode 100644 index 000000000..5cc029210 --- /dev/null +++ b/runtime-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.runtime.spi.service.BindingConverter @@ -0,0 +1 @@ +com.alipay.sofa.runtime.service.binding.JvmBindingConverter \ No newline at end of file diff --git a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/ark/SofaEventHandlerTest.java b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/ark/SofaEventHandlerTest.java index 28e2583d2..eb65388ba 100644 --- a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/ark/SofaEventHandlerTest.java +++ b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/ark/SofaEventHandlerTest.java @@ -25,6 +25,7 @@ import com.alipay.sofa.runtime.beans.service.SampleService; import com.alipay.sofa.runtime.integration.invoke.DynamicJvmServiceProxyFinder; import com.alipay.sofa.runtime.integration.service.SofaEventHandler; +import com.alipay.sofa.runtime.service.binding.JvmBinding; import com.alipay.sofa.runtime.spi.binding.Contract; import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager; @@ -147,6 +148,8 @@ public Biz getBiz(SofaRuntimeManager sofaRuntimeManager) { result = SampleService.class; contract.getUniqueId(); result = ""; + contract.getBinding(JvmBinding.JVM_BINDING_TYPE); + result = new JvmBinding(); invocation.getArguments(); result = new Object[] {}; diff --git a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/IntegrationTest.java b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/IntegrationTest.java index 9b3ed51fb..54c2b3c32 100644 --- a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/IntegrationTest.java +++ b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/IntegrationTest.java @@ -16,6 +16,14 @@ */ package com.alipay.sofa.runtime.integration; +import com.alipay.sofa.runtime.api.component.ComponentName; +import com.alipay.sofa.runtime.service.binding.JvmBinding; +import com.alipay.sofa.runtime.service.component.ReferenceComponent; +import com.alipay.sofa.runtime.service.component.ServiceComponent; +import com.alipay.sofa.runtime.spi.component.ComponentInfo; +import com.alipay.sofa.runtime.spi.component.ComponentManager; +import com.alipay.sofa.runtime.spi.util.ComponentNameFactory; +import com.alipay.sofa.runtime.util.StringUtils; import org.junit.Assert; import org.junit.Test; import org.springframework.boot.actuate.health.Status; @@ -35,6 +43,8 @@ import com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean; import com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean; +import java.util.Collection; + /** * @author qilong.zql * @since 2.3.1 @@ -232,4 +242,113 @@ public void testServiceFactoryBean() { Assert.assertTrue(key.startsWith("&ServiceFactoryBean#")); }); } + + @Test + public void testReferenceBinding() { + ComponentManager componentManager = sofaRuntimeContext.getComponentManager(); + ReferenceComponent serializeTrueViaAnnotation = null; + ReferenceComponent defaultSerializeFalseViaAnnotation = null; + ReferenceComponent defaultElement = null; + ReferenceComponent element = null; + ReferenceComponent noneUniqueId = null; + Collection componentInfos = componentManager + .getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE); + for (ComponentInfo componentInfo : componentInfos) { + String rawName = componentInfo.getName().getRawName(); + if (rawName.contains(getReferenceComponentName(SampleService.class, + "serializeTrueViaAnnotation").getRawName())) { + serializeTrueViaAnnotation = (ReferenceComponent) componentInfo; + } else if (rawName.contains(getReferenceComponentName(SampleService.class, + "defaultSerializeFalseViaAnnotation").getRawName())) { + defaultSerializeFalseViaAnnotation = (ReferenceComponent) componentInfo; + } else if (rawName.contains(getReferenceComponentName(SampleService.class, + "default-element").getRawName())) { + defaultElement = (ReferenceComponent) componentInfo; + } else if (componentInfo.getName().getRawName() + .contains(getReferenceComponentName(SampleService.class, "element").getRawName())) { + element = (ReferenceComponent) componentInfo; + } else if (rawName.contains(":#") + && rawName.contains(getReferenceComponentName(SampleService.class, "") + .getRawName())) { + noneUniqueId = (ReferenceComponent) componentInfo; + } + } + Assert.assertNotNull(serializeTrueViaAnnotation); + Assert.assertNotNull(defaultSerializeFalseViaAnnotation); + Assert.assertNotNull(defaultElement); + Assert.assertNotNull(element); + Assert.assertNotNull(noneUniqueId); + + JvmBinding jvmBinding; + jvmBinding = (JvmBinding) serializeTrueViaAnnotation.getReference().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) defaultSerializeFalseViaAnnotation.getReference().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) defaultElement.getReference().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) element.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE); + Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) noneUniqueId.getReference().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize()); + } + + @Test + public void testServiceBinding() { + ComponentManager componentManager = sofaRuntimeContext.getComponentManager(); + ServiceComponent serializeFalseViaAnnotation = (ServiceComponent) componentManager + .getComponentInfo(getServiceComponentName(SampleService.class, + "serializeFalseViaAnnotation")); + ServiceComponent defaultSerializeTrueViaAnnotation = (ServiceComponent) componentManager + .getComponentInfo(getServiceComponentName(SampleService.class, + "defaultSerializeTrueViaAnnotation")); + ServiceComponent defaultElement = (ServiceComponent) componentManager + .getComponentInfo(getServiceComponentName(SampleService.class, "default-element")); + ServiceComponent element = (ServiceComponent) componentManager + .getComponentInfo(getServiceComponentName(SampleService.class, "element")); + ServiceComponent noneUniqueId = (ServiceComponent) componentManager + .getComponentInfo(getServiceComponentName(SampleService.class, "")); + + Assert.assertNotNull(serializeFalseViaAnnotation); + Assert.assertNotNull(defaultSerializeTrueViaAnnotation); + Assert.assertNotNull(defaultElement); + Assert.assertNotNull(element); + Assert.assertNotNull(noneUniqueId); + + JvmBinding jvmBinding; + jvmBinding = (JvmBinding) serializeFalseViaAnnotation.getService().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) defaultSerializeTrueViaAnnotation.getService().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) defaultElement.getService().getBinding( + JvmBinding.JVM_BINDING_TYPE); + Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) element.getService().getBinding(JvmBinding.JVM_BINDING_TYPE); + Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize()); + + jvmBinding = (JvmBinding) noneUniqueId.getService().getBinding(JvmBinding.JVM_BINDING_TYPE); + Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize()); + } + + private ComponentName getServiceComponentName(Class clazz, String uniqueId) { + return ComponentNameFactory.createComponentName(ServiceComponent.SERVICE_COMPONENT_TYPE, + clazz, uniqueId); + } + + private ComponentName getReferenceComponentName(Class clazz, String uniqueId) { + return ComponentNameFactory.createComponentName( + ReferenceComponent.REFERENCE_COMPONENT_TYPE, clazz, uniqueId); + } } \ No newline at end of file diff --git a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/base/AbstractTestBase.java b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/base/AbstractTestBase.java index ceffbf76f..7d701bc58 100644 --- a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/base/AbstractTestBase.java +++ b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/base/AbstractTestBase.java @@ -17,13 +17,16 @@ package com.alipay.sofa.runtime.integration.base; import com.alipay.sofa.runtime.api.annotation.SofaReference; +import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding; import com.alipay.sofa.runtime.api.annotation.SofaService; +import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding; import com.alipay.sofa.runtime.beans.impl.MethodBeanClassAnnotationSampleService; import com.alipay.sofa.runtime.beans.impl.MethodBeanMethodAnnotationSampleService; import com.alipay.sofa.runtime.beans.impl.ParameterAnnotationSampleService; import com.alipay.sofa.runtime.beans.impl.SampleServiceImpl; import com.alipay.sofa.runtime.beans.service.SampleService; import com.alipay.sofa.runtime.integration.features.AwareTest; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; import org.junit.Before; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -42,7 +45,8 @@ */ public abstract class AbstractTestBase extends TestBase { - public AwareTest awareTest; + public AwareTest awareTest; + public SofaRuntimeContext sofaRuntimeContext; @Before public void before() { @@ -50,8 +54,10 @@ public void before() { properties.put("spring.application.name", "runtime-test"); properties.put("mix-xml-annotation-unique-id", "xmlAnnotationSampleService"); properties.put("spring.jmx.enabled", "false"); + properties.put("com.alipay.sofa.boot.skipJvmReferenceHealthCheck", true); initApplicationContext(properties, IntegrationTestConfiguration.class); awareTest = applicationContext.getBean(AwareTest.class); + sofaRuntimeContext = applicationContext.getBean(SofaRuntimeContext.class); } @Configuration @@ -72,6 +78,18 @@ SampleService methodBeanMethodAnnotationSampleService() { return new MethodBeanMethodAnnotationSampleService(); } + @Bean({ "name3" }) + @SofaService(uniqueId = "serializeFalseViaAnnotation", bindings = { @SofaServiceBinding(serialize = false) }) + SampleService serializeFalseViaAnnotationSampleService() { + return new MethodBeanMethodAnnotationSampleService(); + } + + @Bean({ "name4" }) + @SofaService(uniqueId = "defaultSerializeTrueViaAnnotation") + SampleService defaultSerializeTrueViaAnnotationSampleService() { + return new MethodBeanMethodAnnotationSampleService(); + } + @Bean("multiService") SampleService service() { return new SampleServiceImpl(""); @@ -81,6 +99,7 @@ SampleService service() { SampleService service(@Value("$spring.application.name") String appName) { return new SampleServiceImpl(""); } + } @Configuration @@ -89,7 +108,9 @@ static class AfterConfiguration { @Bean SampleService parameterAnnotationSampleService(@SofaReference(uniqueId = "${mix-xml-annotation-unique-id}") SampleService service1, @SofaReference(uniqueId = "methodBeanClassAnnotationSampleService") SampleService service2, - @SofaReference(uniqueId = "methodBeanMethodAnnotationSampleService") SampleService service3) { + @SofaReference(uniqueId = "methodBeanMethodAnnotationSampleService") SampleService service3, + @SofaReference(uniqueId = "serializeTrueViaAnnotation", binding = @SofaReferenceBinding(serialize = true)) SampleService service4, + @SofaReference(uniqueId = "defaultSerializeFalseViaAnnotation") SampleService service5) { return new ParameterAnnotationSampleService(service1, service2, service3); } } diff --git a/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml b/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml index 97088ebda..d0fa492bb 100644 --- a/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml +++ b/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml @@ -16,9 +16,21 @@ + + + + + + + + + + + +