Search in sources :

Example 21 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class ReferenceComponent method activate.

@Override
public void activate() throws ServiceRuntimeException {
    if (reference.hasBinding()) {
        Binding candidate = null;
        Set<Binding> bindings = reference.getBindings();
        if (bindings.size() == 1) {
            candidate = bindings.iterator().next();
        } else if (bindings.size() > 1) {
            Object backupProxy = null;
            for (Binding binding : bindings) {
                if (JvmBinding.JVM_BINDING_TYPE.getType().equals(binding.getName())) {
                    candidate = binding;
                } else {
                    // Under normal RPC reference (local-first/jvm-first is not set to false) binding,
                    // backup proxy is the RPC proxy, which will be invoked if Jvm service is not found
                    backupProxy = createProxy(reference, binding);
                }
            }
            if (candidate != null) {
                ((JvmBinding) candidate).setBackupProxy(backupProxy);
            }
        }
        Object proxy = null;
        if (candidate != null) {
            proxy = createProxy(reference, candidate);
        }
        this.implementation = new DefaultImplementation();
        implementation.setTarget(proxy);
    }
    super.activate();
    latch.countDown();
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation)

Example 22 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class ReferenceComponent method isHealthy.

@Override
public HealthResult isHealthy() {
    HealthResult result = new HealthResult(componentName.getRawName());
    List<HealthResult> bindingHealth = new ArrayList<>();
    JvmBinding jvmBinding = null;
    HealthResult jvmBindingHealthResult = null;
    if (reference.hasBinding()) {
        for (Binding binding : reference.getBindings()) {
            bindingHealth.add(binding.healthCheck());
            if (JvmBinding.JVM_BINDING_TYPE.equals(binding.getBindingType())) {
                jvmBinding = (JvmBinding) binding;
                jvmBindingHealthResult = bindingHealth.get(bindingHealth.size() - 1);
            }
        }
    }
    // check reference has a corresponding service
    if (!SofaRuntimeProperties.isSkipJvmReferenceHealthCheck(sofaRuntimeContext) && jvmBinding != null) {
        Object serviceTarget = getServiceTarget();
        if (serviceTarget == null && !jvmBinding.hasBackupProxy()) {
            jvmBindingHealthResult.setHealthy(false);
            jvmBindingHealthResult.setHealthReport("can not find corresponding jvm service");
        }
    }
    List<HealthResult> failedBindingHealth = new ArrayList<>();
    for (HealthResult healthResult : bindingHealth) {
        if (healthResult != null && !healthResult.isHealthy()) {
            failedBindingHealth.add(healthResult);
        }
    }
    result.setHealthy(failedBindingHealth.size() == 0);
    String report = aggregateBindingHealth(reference.getBindings());
    if (e != null) {
        report += " [" + e.getMessage() + "]";
        result.setHealthy(false);
    }
    result.setHealthReport(report);
    return result;
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ArrayList(java.util.ArrayList) HealthResult(com.alipay.sofa.runtime.spi.health.HealthResult) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 23 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class DynamicJvmServiceProxyFinder method findServiceProxy.

public ServiceProxy findServiceProxy(ClassLoader clientClassloader, Contract contract) {
    ServiceComponent serviceComponent = findServiceComponent(clientClassloader, contract);
    if (serviceComponent == null) {
        return null;
    }
    SofaRuntimeManager sofaRuntimeManager = serviceComponent.getContext().getSofaRuntimeManager();
    Biz biz = getBiz(sofaRuntimeManager);
    if (biz == null) {
        return null;
    }
    JvmBinding referenceJvmBinding = (JvmBinding) contract.getBinding(JvmBinding.JVM_BINDING_TYPE);
    JvmBinding serviceJvmBinding = (JvmBinding) serviceComponent.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    boolean serialize;
    if (serviceJvmBinding != null) {
        serialize = referenceJvmBinding.getJvmBindingParam().isSerialize() || serviceJvmBinding.getJvmBindingParam().isSerialize();
    } else {
        // Service provider don't intend to publish JVM service, serialize is considered to be true in this case
        serialize = true;
    }
    return new DynamicJvmServiceInvoker(clientClassloader, sofaRuntimeManager.getAppClassLoader(), serviceComponent.getService().getTarget(), contract, biz.getIdentity(), serialize);
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) SofaRuntimeManager(com.alipay.sofa.runtime.spi.component.SofaRuntimeManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 24 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.

the class SofaBindingTest method testReferenceBinding.

@Test
public void testReferenceBinding() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ReferenceComponent serializeTrueViaAnnotation = null;
    ReferenceComponent defaultSerializeFalseViaAnnotation = null;
    ReferenceComponent defaultElement = null;
    ReferenceComponent element = null;
    ReferenceComponent noneUniqueId = null;
    Collection<ComponentInfo> componentInfos = componentManager.getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : componentInfos) {
        String rawName = componentInfo.getName().getRawName();
        if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "serializeTrueViaAnnotation").getRawName())) {
            serializeTrueViaAnnotation = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "defaultSerializeFalseViaAnnotation").getRawName())) {
            defaultSerializeFalseViaAnnotation = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "default-element").getRawName())) {
            defaultElement = (ReferenceComponent) componentInfo;
        } else if (componentInfo.getName().getRawName().contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "element").getRawName())) {
            element = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(":#") && rawName.contains(ComponentNameUtil.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());
}
Also used : DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 25 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by sofastack.

the class ReferenceClientImpl method getReferenceFromReferenceParam.

@SuppressWarnings("unchecked")
private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> referenceParam) {
    BindingParam bindingParam = referenceParam.getBindingParam();
    Reference reference = new ReferenceImpl(referenceParam.getUniqueId(), referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isJvmFirst(), null);
    if (bindingParam == null) {
        // 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());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(true);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        reference.addBinding(binding);
    }
    return reference;
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) Reference(com.alipay.sofa.runtime.service.component.Reference) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Aggregations

JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)27 Binding (com.alipay.sofa.runtime.spi.binding.Binding)14 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)11 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)10 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)9 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)8 ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)8 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)6 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 Test (org.junit.Test)6 Reference (com.alipay.sofa.runtime.service.component.Reference)5 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)5 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)4 ServiceImpl (com.alipay.sofa.runtime.service.component.impl.ServiceImpl)4 SofaRuntimeManager (com.alipay.sofa.runtime.spi.component.SofaRuntimeManager)4 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)4 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)4 SampleService (com.alipay.sofa.runtime.test.beans.facade.SampleService)4 DefaultSampleService (com.alipay.sofa.runtime.test.beans.service.DefaultSampleService)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4