Search in sources :

Example 1 with JvmBinding

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

the class SofaBindingTest method testServiceBinding.

@Test
public void testServiceBinding() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ServiceComponent serializeFalseViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "serializeFalseViaAnnotation"));
    ServiceComponent defaultSerializeTrueViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "defaultSerializeTrueViaAnnotation"));
    ServiceComponent defaultElement = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "default-element"));
    ServiceComponent element = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "element"));
    ServiceComponent noneUniqueId = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.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());
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with JvmBinding

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

the class SofaEventHandlerTest method testDynamicProxyFinder.

@Test
public void testDynamicProxyFinder() throws Exception {
    SofaFramework.registerSofaRuntimeManager(sofaRuntimeManager);
    new MockUp<DynamicJvmServiceProxyFinder>() {

        @Mock
        public Biz getBiz(SofaRuntimeManager sofaRuntimeManager) {
            return biz;
        }
    };
    new Expectations() {

        {
            biz.getIdentity();
            result = "MockName:MockVersion";
            biz.getBizState();
            result = BizState.ACTIVATED;
            sofaRuntimeManager.getAppClassLoader();
            result = ctx.getClassLoader().getParent();
        }
    };
    new Expectations() {

        {
            sofaRuntimeManager.getComponentManager();
            result = ((SofaRuntimeContext) ctx.getBean("sofaRuntimeContext")).getComponentManager();
            contract.getInterfaceType();
            result = SampleService.class;
            contract.getUniqueId();
            result = "";
            contract.getBinding(JvmBinding.JVM_BINDING_TYPE);
            result = new JvmBinding();
            invocation.getArguments();
            result = new Object[] {};
            invocation.getMethod();
            result = SampleService.class.getMethod("service");
        }
    };
    DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().setHasFinishStartup(true);
    ServiceProxy serviceProxy = DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().findServiceProxy(ctx.getClassLoader(), contract);
    try {
        Assert.assertTrue(SofaEventHandlerTest.class.getName().equals(serviceProxy.invoke(invocation)));
    } catch (Throwable throwable) {
        throw new RuntimeException("testDynamicProxyFinder case failed.", throwable);
    }
}
Also used : Expectations(mockit.Expectations) ServiceProxy(com.alipay.sofa.runtime.spi.service.ServiceProxy) DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) MockUp(mockit.MockUp) SofaRuntimeManager(com.alipay.sofa.runtime.spi.component.SofaRuntimeManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test)

Example 3 with JvmBinding

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

the class ReferenceFactoryBean method doAfterPropertiesSet.

@Override
protected void doAfterPropertiesSet() throws Exception {
    Reference reference = buildReference();
    Assert.isTrue(bindings.size() <= 1, "Found more than one binding in <sofa:reference/>, <sofa:reference/> can only have one binding.");
    // default add jvm binding and reference jvm binding should set serialize as false
    if (bindings.size() == 0) {
        // default reference prefer to ignore serialize
        JvmBindingParam jvmBindingParam = new JvmBindingParam();
        jvmBindingParam.setSerialize(false);
        bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam));
    }
    reference.addBinding(bindings.get(0));
    proxy = ReferenceRegisterHelper.registerReference(reference, bindingAdapterFactory, sofaRuntimeContext, applicationContext);
}
Also used : Reference(com.alipay.sofa.runtime.service.component.Reference) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 4 with JvmBinding

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

the class ServiceClientImpl method service.

@SuppressWarnings("unchecked")
public void service(ServiceParam serviceParam) {
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(serviceParam.getInstance());
    if (serviceParam.getInterfaceType() == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00201"));
    }
    Service service = new ServiceImpl(serviceParam.getUniqueId(), serviceParam.getInterfaceType(), InterfaceMode.api, serviceParam.getInstance(), null);
    for (BindingParam bindingParam : serviceParam.getBindingParams()) {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(false);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        service.addBinding(binding);
    }
    boolean hasJvmBinding = false;
    for (Binding binding : service.getBindings()) {
        if (binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)) {
            hasJvmBinding = true;
            break;
        }
    }
    if (!hasJvmBinding) {
        service.addBinding(new JvmBinding());
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Service(com.alipay.sofa.runtime.service.component.Service) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext)

Example 5 with JvmBinding

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

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)

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