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();
}
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;
}
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);
}
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());
}
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;
}
Aggregations