Search in sources :

Example 11 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class SchemaLoader method putSelfBasePathIfAbsent.

public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
    if (basePath == null || basePath.length() == 0) {
        return;
    }
    Microservice microservice = RegistryUtils.getMicroservice();
    if (!microservice.getServiceName().equals(microserviceName)) {
        return;
    }
    List<BasePath> paths = microservice.getPaths();
    for (BasePath path : paths) {
        if (path.getPath().equals(basePath)) {
            return;
        }
    }
    BasePath basePathObj = new BasePath();
    basePathObj.setPath(basePath);
    paths.add(basePathObj);
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) BasePath(io.servicecomb.serviceregistry.api.registry.BasePath)

Example 12 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class ConsumerSchemaFactory method getOrCreateConsumer.

// 透明rpc场景,因为每次指定schema调用,所以可以懒加载
// 而rest场景,是根据path反向查找schema,所以必须所有的schema都存在才行
// 两种场景可能都会来初始化,为避免管理复杂度,改为全部全量初始化
//
// 用于rest consumer注册的场景,此时启动流程已经完成,需要主动通知listener
// microserviceName可能是本app内的微服务
// 也可能是appid:name形式的其他app的微服务
public MicroserviceMeta getOrCreateConsumer(String microserviceName, String microserviceVersionRule) {
    MicroserviceMeta microserviceMeta = microserviceMetaManager.findValue(microserviceName);
    if (microserviceMeta != null) {
        return microserviceMeta;
    }
    // 极小概率事件,不必做microservice级的锁分解了
    synchronized (lock) {
        microserviceMeta = microserviceMetaManager.findValue(microserviceName);
        if (microserviceMeta != null) {
            return microserviceMeta;
        }
        // 获取指定服务中有哪些schemaId
        // 先取本地,再从服务中心取,如果服务中心取成功了,则将schema id合并处理
        microserviceMeta = new MicroserviceMeta(microserviceName);
        Set<String> schemaIds = findLocalSchemas(microserviceMeta);
        Microservice microservice = findMicroservice(microserviceMeta, microserviceVersionRule);
        if (microservice != null) {
            schemaIds.addAll(microservice.getSchemas());
        }
        getOrCreateConsumerSchema(microserviceMeta, schemaIds, microservice);
        microserviceMetaManager.register(microserviceName, microserviceMeta);
        schemaListenerManager.notifySchemaListener(microserviceMeta);
        return microserviceMeta;
    }
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta)

Example 13 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestDynamicSchemaLoader method init.

@BeforeClass
public static void init() {
    UnitTestMeta.init();
    SchemaLoader loader = new SchemaLoader();
    loader.setMicroserviceMetaManager(microserviceMetaManager);
    SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
    schemaListenerManager.setSchemaListenerList(Collections.emptyList());
    CseContext context = CseContext.getInstance();
    context.setSchemaLoader(loader);
    context.setSchemaListenerManager(schemaListenerManager);
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceName("ms");
    ReflectUtils.setField(RegistryUtils.class, null, "microservice", microservice);
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) CseContext(io.servicecomb.core.CseContext) BeforeClass(org.junit.BeforeClass)

Example 14 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestDefinition method setupClass.

@BeforeClass
public static void setupClass() throws Exception {
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceName("testname");
    new MockUp<RegistryUtils>() {

        @Mock
        private Microservice createMicroserviceFromDefinition() {
            return microservice;
        }
    };
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) MockUp(mockit.MockUp) BeforeClass(org.junit.BeforeClass)

Example 15 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestConsumerSchemaFactory method init.

@BeforeClass
public static void init() {
    ReflectUtils.setField(RegistryClientFactory.class, null, "registryClient", registryClient);
    SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
    schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
    ConsumerResponseMapperFactory responseMapperFactory = new ConsumerResponseMapperFactory();
    responseMapperFactory.setMapperList(Arrays.asList(new ConsumerResponseSame()));
    ConsumerArgumentsMapperFactory consumerArgsMapperFactory = new ConsumerArgumentsMapperFactory();
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    SchemaLoader schemaLoader = new SchemaLoader();
    CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
    ReflectUtils.setField(consumerSchemaFactory, "schemaListenerManager", schemaListenerManager);
    ReflectUtils.setField(consumerSchemaFactory, "consumerArgsMapperFactory", consumerArgsMapperFactory);
    ReflectUtils.setField(consumerSchemaFactory, "responseMapperFactory", responseMapperFactory);
    ReflectUtils.setField(consumerSchemaFactory, "microserviceMetaManager", microserviceMetaManager);
    ReflectUtils.setField(consumerSchemaFactory, "schemaLoader", schemaLoader);
    ReflectUtils.setField(consumerSchemaFactory, "compositeSwaggerGeneratorContext", compositeSwaggerGeneratorContext);
    SchemaMeta schemaMeta = new UnitTestMeta().getOrCreateSchemaMeta(TestConsumerSchemaFactoryImpl.class);
    String content = UnitTestSwaggerUtils.pretty(schemaMeta.getSwagger());
    Mockito.when(registryClient.getMicroserviceId("app", "ms", "latest")).thenReturn("0");
    Mockito.when(registryClient.getSchema("0", "schema")).thenReturn(content);
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceId("0");
    microservice.addSchema("schema", content);
    Mockito.when(registryClient.getMicroservice("0")).thenReturn(microservice);
}
Also used : ConsumerResponseSame(io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseSame) MicroserviceMetaManager(io.servicecomb.core.definition.MicroserviceMetaManager) SchemaLoader(io.servicecomb.core.definition.loader.SchemaLoader) Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) UnitTestMeta(io.servicecomb.core.unittest.UnitTestMeta) CompositeSwaggerGeneratorContext(io.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) SchemaListenerManager(io.servicecomb.core.definition.loader.SchemaListenerManager) ConsumerResponseMapperFactory(io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory) ConsumerArgumentsMapperFactory(io.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)22 MockUp (mockit.MockUp)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)5 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)4 BeforeClass (org.junit.BeforeClass)4 RegistryThread (io.servicecomb.serviceregistry.RegistryThread)2 HealthCheck (io.servicecomb.serviceregistry.api.registry.HealthCheck)2 ClientException (io.servicecomb.serviceregistry.client.ClientException)2 ServiceRegistryClient (io.servicecomb.serviceregistry.client.ServiceRegistryClient)2 HashMap (java.util.HashMap)2 CseContext (io.servicecomb.core.CseContext)1 Transport (io.servicecomb.core.Transport)1 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)1 MicroserviceMetaManager (io.servicecomb.core.definition.MicroserviceMetaManager)1 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)1 SchemaListenerManager (io.servicecomb.core.definition.loader.SchemaListenerManager)1 SchemaLoader (io.servicecomb.core.definition.loader.SchemaLoader)1 UnitTestMeta (io.servicecomb.core.unittest.UnitTestMeta)1 IpPort (io.servicecomb.foundation.common.net.IpPort)1