diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/ext/component/ExtensionComponent.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/ext/component/ExtensionComponent.java index e565e6b72..fb82d20d2 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/ext/component/ExtensionComponent.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/ext/component/ExtensionComponent.java @@ -136,7 +136,31 @@ public HealthResult isHealthy() { healthResult.setHealthy(true); return healthResult; } - return super.isHealthy(); + + HealthResult healthResult = new HealthResult(componentName.getRawName()); + //表示 loadContributions 异常的 Extension + if (e != null) { + healthResult.setHealthy(false); + healthResult.setHealthReport("Extension loadContributions error: " + e.getMessage()); + return healthResult; + } + //表示注册成功的 Extension + if (isActivated()) { + healthResult.setHealthy(true); + return healthResult; + } + //表示对应的 ExtensionPoint 未注册 + if (!isResolved()) { + healthResult.setHealthy(false); + healthResult.setHealthReport("Can not find corresponding ExtensionPoint: " + + extension.getTargetComponentName().getName()); + return healthResult; + } else { + // 表示 registerExtension 异常的 Extension + healthResult.setHealthy(false); + healthResult.setHealthReport("Extension registerExtension error"); + return healthResult; + } } public Extension getExtension() { diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/ExtensionTest.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/ExtensionTest.java index 6acae9ced..9a9149724 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/ExtensionTest.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/ExtensionTest.java @@ -16,25 +16,6 @@ */ package com.alipay.sofa.runtime.test; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.OutputStream; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.ImportResource; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; -import org.w3c.dom.Document; - import com.alipay.sofa.common.xmap.Context; import com.alipay.sofa.common.xmap.DOMSerializer; import com.alipay.sofa.common.xmap.Resource; @@ -43,6 +24,11 @@ import com.alipay.sofa.runtime.api.client.ExtensionClient; import com.alipay.sofa.runtime.api.client.param.ExtensionParam; import com.alipay.sofa.runtime.api.client.param.ExtensionPointParam; +import com.alipay.sofa.runtime.ext.component.ExtensionComponent; +import com.alipay.sofa.runtime.model.ComponentType; +import com.alipay.sofa.runtime.spi.component.ComponentInfo; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; +import com.alipay.sofa.runtime.spi.spring.SofaRuntimeContextAware; import com.alipay.sofa.runtime.test.configuration.RuntimeConfiguration; import com.alipay.sofa.runtime.test.extension.bean.IExtension; import com.alipay.sofa.runtime.test.extension.bean.SimpleSpringBean; @@ -51,6 +37,24 @@ import com.alipay.sofa.runtime.test.extension.descriptor.ClientExtensionDescriptor; import com.alipay.sofa.runtime.test.extension.descriptor.SimpleExtensionDescriptor; import com.alipay.sofa.runtime.test.extension.descriptor.XMapTestDescriptor; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportResource; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.w3c.dom.Document; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.OutputStream; +import java.util.Collection; /** * @author ruoshan @@ -59,10 +63,12 @@ @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(properties = "spring.application.name=ExtensionTest") -public class ExtensionTest implements ExtensionClientAware { +public class ExtensionTest implements ExtensionClientAware, SofaRuntimeContextAware { private ExtensionClient extensionClient; + private SofaRuntimeContext sofaRuntimeContext; + @Autowired private IExtension iExtension; @@ -230,11 +236,31 @@ public void testBad() { Assert.assertNull(iExtension.getBadDescriptor()); } + @Test + public void testNotExist() { + Collection componentInfos = + sofaRuntimeContext.getComponentManager().getComponentInfosByType(new ComponentType("extension")); + componentInfos.forEach(componentInfo -> { + if (componentInfo instanceof ExtensionComponent) { + if (componentInfo.getName().getName().contains("noExist")) { + Assert.assertFalse(componentInfo.isHealthy().isHealthy()); + Assert.assertEquals("Can not find corresponding ExtensionPoint: iExtension$noExist", + componentInfo.isHealthy().getHealthReport()); + } + } + }); + } + @Override public void setExtensionClient(ExtensionClient extensionClient) { this.extensionClient = extensionClient; } + @Override + public void setSofaRuntimeContext(SofaRuntimeContext sofaRuntimeContext) { + this.sofaRuntimeContext = sofaRuntimeContext; + } + @Configuration(proxyBeanMethods = false) @Import(RuntimeConfiguration.class) @ImportResource("classpath*:META-INF/extension/test-extension.xml") diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/resources/META-INF/extension/test-extension.xml b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/resources/META-INF/extension/test-extension.xml index d05417d96..f4de66178 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/resources/META-INF/extension/test-extension.xml +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/resources/META-INF/extension/test-extension.xml @@ -188,4 +188,10 @@ + + + noExist + + + \ No newline at end of file