use of org.apache.servicecomb.serviceregistry.api.registry.Microservice in project incubator-servicecomb-java-chassis by apache.
the class LocalServiceRegistryClientImpl method findLatest.
protected Microservice findLatest(String appId, String serviceName, VersionRule versionRule) {
Version latestVersion = null;
Microservice latest = null;
for (Entry<String, Microservice> entry : microserviceIdMap.entrySet()) {
Microservice microservice = entry.getValue();
if (!isSameMicroservice(microservice, appId, serviceName)) {
continue;
}
Version version = VersionUtils.getOrCreate(microservice.getVersion());
if (!versionRule.isAccept(version)) {
continue;
}
if (latestVersion == null || version.compareTo(latestVersion) > 0) {
latestVersion = version;
latest = microservice;
}
}
return latest;
}
use of org.apache.servicecomb.serviceregistry.api.registry.Microservice in project incubator-servicecomb-java-chassis by apache.
the class LocalServiceRegistryClientImpl method initFromData.
private void initFromData(Map<String, Object> data) {
for (Entry<String, Object> entry : data.entrySet()) {
String name = entry.getKey();
@SuppressWarnings("unchecked") List<Map<String, Object>> serviceConfigs = (List<Map<String, Object>>) entry.getValue();
for (Map<String, Object> serviceConfig : serviceConfigs) {
@SuppressWarnings("unchecked") List<Map<String, Object>> instancesConfig = (List<Map<String, Object>>) serviceConfig.get("instances");
String appId = (String) serviceConfig.get("appid");
String version = (String) serviceConfig.get("version");
String serviceId = (String) serviceConfig.get("id");
Microservice microservice = new Microservice();
microservice.setAppId(appId == null ? DEFAULT_APPLICATION_ID : appId);
microservice.setServiceName(name);
microservice.setVersion(version);
microservice.setServiceId(serviceId == null ? UUID.randomUUID().toString() : serviceId);
microserviceIdMap.put(microservice.getServiceId(), microservice);
Map<String, MicroserviceInstance> instanceMap = new ConcurrentHashMap<>();
for (Map<String, Object> instanceConfig : instancesConfig) {
@SuppressWarnings("unchecked") List<String> endpoints = (List<String>) instanceConfig.get("endpoints");
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId(UUID.randomUUID().toString());
instance.setEndpoints(endpoints);
instance.setServiceId(microservice.getServiceId());
instanceMap.put(instance.getInstanceId(), instance);
}
microserviceInstanceMap.put(microservice.getServiceId(), instanceMap);
}
}
if (!data.isEmpty()) {
revision.incrementAndGet();
}
}
use of org.apache.servicecomb.serviceregistry.api.registry.Microservice in project incubator-servicecomb-java-chassis by apache.
the class LocalServiceRegistryClientImpl method registerSchema.
@Override
public boolean registerSchema(String microserviceId, String schemaId, String schemaContent) {
Microservice microservice = microserviceIdMap.get(microserviceId);
if (microservice == null) {
throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
}
microservice.getSchemaMap().put(schemaId, schemaContent);
return true;
}
use of org.apache.servicecomb.serviceregistry.api.registry.Microservice in project incubator-servicecomb-java-chassis by apache.
the class TestConsumerSchemaFactory method init.
@BeforeClass
public static void init() {
ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
serviceRegistry.init();
RegistryUtils.setServiceRegistry(serviceRegistry);
SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
SchemaLoader schemaLoader = new SchemaLoader() {
@Override
public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
}
};
CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
ReflectUtils.setField(consumerSchemaFactory, "schemaListenerManager", schemaListenerManager);
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());
Microservice microservice = new Microservice();
microservice.setAppId("app");
microservice.setServiceId("0");
microservice.setServiceName("ms");
microservice.setVersion("1.0.0");
microservice.addSchema("schema", content);
serviceRegistry.getServiceRegistryClient().registerMicroservice(microservice);
MicroserviceInstance instance = new MicroserviceInstance();
instance.setServiceId("0");
instance.setInstanceId("0");
serviceRegistry.getServiceRegistryClient().registerMicroserviceInstance(instance);
}
use of org.apache.servicecomb.serviceregistry.api.registry.Microservice in project incubator-servicecomb-java-chassis by apache.
the class TestMicroserviceVersionMeta method construct.
@Test
public void construct() {
String microserviceName = "app:ms";
String microserviceId = "id";
Microservice microservice = new Microservice();
microservice.setVersion("1.0.0");
new Expectations(RegistryUtils.class) {
{
RegistryUtils.getMicroservice(microserviceId);
result = microservice;
}
};
List<String> logs = new ArrayList<>();
CseContext.getInstance().setConsumerSchemaFactory(new MockUp<ConsumerSchemaFactory>() {
@Mock
void getOrCreateConsumerSchema(MicroserviceMeta microserviceMeta, Microservice microservice) {
logs.add("getOrCreateConsumerSchema");
}
}.getMockInstance());
CseContext.getInstance().setSchemaListenerManager(new MockUp<SchemaListenerManager>() {
@Mock
void notifySchemaListener(MicroserviceMeta... microserviceMetas) {
logs.add("notifySchemaListener");
}
}.getMockInstance());
MicroserviceVersionMeta microserviceVersionMeta = new MicroserviceVersionMeta(microserviceName, microserviceId, PrivateMicroserviceClassLoaderFactory.INSTANCE);
Assert.assertThat(logs, Matchers.contains("getOrCreateConsumerSchema", "notifySchemaListener"));
Assert.assertEquals(microserviceName, microserviceVersionMeta.getMicroserviceMeta().getName());
Assert.assertThat(microserviceVersionMeta.getMicroserviceMeta().getClassLoader(), Matchers.instanceOf(MicroserviceClassLoader.class));
}
Aggregations