Search in sources :

Example 51 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceMetadataProcessorTest method testOnApply.

@Test
public void testOnApply() throws NoSuchFieldException, IllegalAccessException {
    WriteRequest defaultInstance = WriteRequest.getDefaultInstance();
    Class<WriteRequest> writeRequestClass = WriteRequest.class;
    Field operation = writeRequestClass.getDeclaredField("operation_");
    operation.setAccessible(true);
    operation.set(defaultInstance, "ADD");
    MetadataOperation<ServiceMetadata> metadataOperation = new MetadataOperation<>();
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    metadataOperation.setMetadata(serviceMetadata);
    metadataOperation.setServiceName("nacos");
    metadataOperation.setNamespace("namespace");
    metadataOperation.setGroup("group");
    Serializer aDefault = SerializeFactory.getDefault();
    ByteString bytes = ByteString.copyFrom(aDefault.serialize(metadataOperation));
    Field data = writeRequestClass.getDeclaredField("data_");
    data.setAccessible(true);
    data.set(defaultInstance, bytes);
    // ADD
    Response addResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Service service = Service.newService(metadataOperation.getNamespace(), metadataOperation.getGroup(), metadataOperation.getServiceName(), metadataOperation.getMetadata().isEphemeral());
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    namingMetadataManager.updateServiceMetadata(singleton, metadataOperation.getMetadata());
    Assert.assertTrue(addResponse.getSuccess());
    verify(namingMetadataManager).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context).getBean(DoubleWriteEventListener.class);
    // CHANGE
    operation.set(defaultInstance, "CHANGE");
    Response changeResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(changeResponse.getSuccess());
    verify(namingMetadataManager, times(2)).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context, times(2)).getBean(DoubleWriteEventListener.class);
    // DELETE
    operation.set(defaultInstance, "DELETE");
    Response deleteResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(deleteResponse.getSuccess());
    verify(namingMetadataManager).removeServiceMetadata(service);
    verify(serviceStorage).removeData(service);
    // VERIFY
    operation.set(defaultInstance, "VERIFY");
    Response otherResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertFalse(otherResponse.getSuccess());
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Serializer(com.alibaba.nacos.consistency.Serializer) Test(org.junit.Test)

Example 52 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceOperatorV2ImplTest method tearDown.

@After
public void tearDown() {
    Service service = Service.newService("A", "B", "C");
    ServiceManager.getInstance().removeSingleton(service);
}
Also used : NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) After(org.junit.After)

Example 53 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceOperatorV2ImplTest method setUp.

@Before
public void setUp() {
    Service service = Service.newService("A", "B", "C");
    ServiceManager.getInstance().getSingleton(service);
}
Also used : NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Before(org.junit.Before)

Example 54 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ClientServiceIndexesManagerTest method setUp.

@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
    clientServiceIndexesManager = new ClientServiceIndexesManager();
    Class<ClientServiceIndexesManager> clientServiceIndexesManagerClass = ClientServiceIndexesManager.class;
    Field publisherIndexesField = clientServiceIndexesManagerClass.getDeclaredField("publisherIndexes");
    publisherIndexesField.setAccessible(true);
    ConcurrentMap<Service, Set<String>> publisherIndexes = (ConcurrentMap<Service, Set<String>>) publisherIndexesField.get(clientServiceIndexesManager);
    publisherIndexes.put(service, new HashSet<>(Collections.singletonList(NACOS)));
    Field subscriberIndexesField = clientServiceIndexesManagerClass.getDeclaredField("subscriberIndexes");
    subscriberIndexesField.setAccessible(true);
    ConcurrentMap<Service, Set<String>> subscriberIndexes = (ConcurrentMap<Service, Set<String>>) subscriberIndexesField.get(clientServiceIndexesManager);
    subscriberIndexes.put(service, new HashSet<>(Collections.singletonList(NACOS)));
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Before(org.junit.Before)

Example 55 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceController method update.

/**
 * Update service.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception exception
 */
@PutMapping
@Secured(action = ActionTypes.WRITE)
public String update(HttpServletRequest request) throws Exception {
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    serviceMetadata.setProtectThreshold(NumberUtils.toFloat(WebUtils.required(request, "protectThreshold")));
    serviceMetadata.setExtendData(UtilsAndCommons.parseMetadata(WebUtils.optional(request, "metadata", StringUtils.EMPTY)));
    serviceMetadata.setSelector(parseSelector(WebUtils.optional(request, "selector", StringUtils.EMPTY)));
    com.alibaba.nacos.naming.core.v2.pojo.Service service = com.alibaba.nacos.naming.core.v2.pojo.Service.newService(namespaceId, NamingUtils.getGroupName(serviceName), NamingUtils.getServiceName(serviceName));
    getServiceOperator().update(service, serviceMetadata);
    return "ok";
}
Also used : ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

Service (com.alibaba.nacos.naming.core.v2.pojo.Service)75 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)20 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)19 ClientOperationService (com.alibaba.nacos.naming.core.v2.service.ClientOperationService)17 Test (org.junit.Test)16 Instance (com.alibaba.nacos.api.naming.pojo.Instance)13 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)13 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)13 NacosException (com.alibaba.nacos.api.exception.NacosException)11 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)11 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)11 HashSet (java.util.HashSet)11 Secured (com.alibaba.nacos.auth.annotation.Secured)10 Before (org.junit.Before)10 Client (com.alibaba.nacos.naming.core.v2.client.Client)9 ClientOperationEvent (com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent)8 ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)8 InstanceMetadata (com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata)8 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)7 Set (java.util.Set)7