Search in sources :

Example 31 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding 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 32 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.

the class ReferenceComponent method unregister.

@Override
public void unregister() throws ServiceRuntimeException {
    super.unregister();
    if (reference.hasBinding()) {
        for (Binding binding : reference.getBindings()) {
            BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
            if (bindingAdapter == null) {
                throw new ServiceRuntimeException(ErrorCode.convert("01-00100", binding.getBindingType(), reference));
            }
            SofaLogger.info(" >>Un-in Binding [{}] Begins - {}.", binding.getBindingType(), reference);
            try {
                bindingAdapter.unInBinding(reference, binding, sofaRuntimeContext);
            } finally {
                SofaLogger.info(" >>Un-in Binding [{}] Ends - {}.", binding.getBindingType(), reference);
            }
        }
    }
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 33 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.

the class XsdTimeoutTest method testReferenceTimeout.

@Test
public void testReferenceTimeout() {
    Collection<ComponentInfo> c = sofaRuntimeContext.getComponentManager().getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : c) {
        if (componentInfo instanceof ReferenceComponent) {
            ReferenceComponent referenceComponent = (ReferenceComponent) componentInfo;
            if (!referenceComponent.getReference().getInterfaceType().equals(WhateverInterface.class)) {
                continue;
            }
            Binding binding = referenceComponent.getReference().getBinding(RpcBindingType.BOLT_BINDING_TYPE);
            if (binding instanceof RpcBinding) {
                RpcBinding rpcBinding = (RpcBinding) binding;
                Assert.assertEquals((long) rpcBinding.getRpcBindingParam().getTimeout(), 10000);
            }
        }
    }
}
Also used : RpcBinding(com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) RpcBinding(com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 34 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.

the class SofaBootRpcAllTest method testLoadBalancerAnnotation.

@Test
public void testLoadBalancerAnnotation() throws NoSuchFieldException, IllegalAccessException {
    Field consumerConfigMapField = ConsumerConfigContainer.class.getDeclaredField("consumerConfigMap");
    consumerConfigMapField.setAccessible(true);
    ConcurrentMap<Binding, ConsumerConfig> consumerConfigMap = (ConcurrentMap<Binding, ConsumerConfig>) consumerConfigMapField.get(consumerConfigContainer);
    boolean found = false;
    for (ConsumerConfig consumerConfig : consumerConfigMap.values()) {
        if ("loadbalancer".equals(consumerConfig.getUniqueId()) && AnnotationService.class.getName().equals(consumerConfig.getInterfaceId())) {
            found = true;
            Assert.assertEquals("roundRobin", consumerConfig.getLoadBalancer());
        }
    }
    Assert.assertTrue("Found roundrobin reference", found);
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) Field(java.lang.reflect.Field) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 35 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.

the class ServiceBeanFactoryPostProcessor method getSofaReferenceBinding.

/**
 * get sofa reference binding annotated on parameter. At present, only jvm sofa reference is supported .
 * @param sofaReferenceAnnotation
 * @param sofaReferenceBinding
 * @return
 */
private List<Binding> getSofaReferenceBinding(SofaReference sofaReferenceAnnotation, SofaReferenceBinding sofaReferenceBinding) {
    if (!JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBinding.bindingType())) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-02005"));
    }
    List<Binding> bindings = new ArrayList<>();
    BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaReferenceBinding.bindingType()));
    if (bindingConverter == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00200", 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;
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ArrayList(java.util.ArrayList) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Aggregations

Binding (com.alipay.sofa.runtime.spi.binding.Binding)42 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)23 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)22 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)12 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)12 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)10 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)10 ArrayList (java.util.ArrayList)10 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)6 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)6 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)4 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)4 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)4 Reference (com.alipay.sofa.runtime.service.component.Reference)4 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)4 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)4 ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)4 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)4 HealthResult (com.alipay.sofa.runtime.spi.health.HealthResult)4