Search in sources :

Example 6 with Service

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

the class ServiceUtilTest method testPageServiceName.

@Test
public void testPageServiceName() {
    Map<String, Service> input = new HashMap<>();
    input.put("Group@@Service", new Service());
    input.put("Service", new Service());
    List<String> actual = ServiceUtil.pageServiceName(1, 20, input);
    assertEquals(2, actual.size());
    assertEquals("Service", actual.get(0));
    assertEquals("Service", actual.get(1));
}
Also used : HashMap(java.util.HashMap) Service(com.alibaba.nacos.naming.core.Service) Test(org.junit.Test)

Example 7 with Service

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

the class ServiceUtilTest method testSelectServiceWithGroupName.

@Test
public void testSelectServiceWithGroupName() {
    Service service1 = new Service();
    service1.setEnabled(true);
    service1.setName("serviceName");
    service1.setGroupName("groupName");
    service1.setAppName("appName");
    service1.setNamespaceId("namespaceId");
    service1.setResetWeight(true);
    Map<String, Service> services = new HashMap<>();
    services.put("service1", service1);
    Map<String, Service> resultMap = ServiceUtil.selectServiceWithGroupName(services, "groupName");
    assertNotNull(resultMap);
}
Also used : HashMap(java.util.HashMap) Service(com.alibaba.nacos.naming.core.Service) Test(org.junit.Test)

Example 8 with Service

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

the class CatalogController method rt4Service.

/**
 * Get response time of service.
 *
 * @param request http request
 * @return response time information
 */
@RequestMapping("/rt/service")
public ObjectNode rt4Service(HttpServletRequest request) throws NacosException {
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    Service service = serviceManager.getService(namespaceId, serviceName);
    serviceManager.checkServiceIsNull(service, namespaceId, serviceName);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    ArrayNode clusters = JacksonUtils.createEmptyArrayNode();
    for (Map.Entry<String, com.alibaba.nacos.naming.core.Cluster> entry : service.getClusterMap().entrySet()) {
        ObjectNode packet = JacksonUtils.createEmptyJsonNode();
        HealthCheckTask task = entry.getValue().getHealthCheckTask();
        packet.put("name", entry.getKey());
        packet.put("checkRTBest", task.getCheckRtBest());
        packet.put("checkRTWorst", task.getCheckRtWorst());
        packet.put("checkRTNormalized", task.getCheckRtNormalized());
        clusters.add(packet);
    }
    result.replace("clusters", clusters);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CatalogService(com.alibaba.nacos.naming.core.CatalogService) Service(com.alibaba.nacos.naming.core.Service) HealthCheckTask(com.alibaba.nacos.naming.healthcheck.HealthCheckTask) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with Service

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

the class OperatorController method getResponsibleServer4Service.

@GetMapping("/distro/server")
public ObjectNode getResponsibleServer4Service(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName) throws NacosException {
    Service service = serviceManager.getService(namespaceId, serviceName);
    serviceManager.checkServiceIsNull(service, namespaceId, serviceName);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.put("responsibleServer", distroMapper.mapSrv(serviceName));
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Service(com.alibaba.nacos.naming.core.Service) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with Service

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

the class RaftController method onPublish.

/**
 * Commit publish datum.
 *
 * @param request  http request
 * @param response http response
 * @return 'ok' if success
 * @throws Exception exception
 */
@PostMapping("/datum/commit")
public String onPublish(HttpServletRequest request, HttpServletResponse response) throws Exception {
    if (versionJudgement.allMemberIsNewVersion()) {
        throw new IllegalStateException("old raft protocol already stop");
    }
    response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Content-Encode", "gzip");
    String entity = IoUtils.toString(request.getInputStream(), "UTF-8");
    String value = URLDecoder.decode(entity, "UTF-8");
    JsonNode jsonObject = JacksonUtils.toObj(value);
    String key = "key";
    RaftPeer source = JacksonUtils.toObj(jsonObject.get("source").toString(), RaftPeer.class);
    JsonNode datumJson = jsonObject.get("datum");
    Datum datum = null;
    if (KeyBuilder.matchInstanceListKey(datumJson.get(key).asText())) {
        datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Instances>>() {
        });
    } else if (KeyBuilder.matchSwitchKey(datumJson.get(key).asText())) {
        datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<SwitchDomain>>() {
        });
    } else if (KeyBuilder.matchServiceMetaKey(datumJson.get(key).asText())) {
        datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Service>>() {
        });
    }
    raftConsistencyService.onPut(datum, source);
    return "ok";
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) Datum(com.alibaba.nacos.naming.consistency.Datum) Service(com.alibaba.nacos.naming.core.Service) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

Service (com.alibaba.nacos.naming.core.Service)28 Cluster (com.alibaba.nacos.naming.core.Cluster)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Instance (com.alibaba.nacos.naming.core.Instance)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Map (java.util.Map)5 NacosException (com.alibaba.nacos.api.exception.NacosException)4 BaseTest (com.alibaba.nacos.naming.BaseTest)4 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 Datum (com.alibaba.nacos.naming.consistency.Datum)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Instances (com.alibaba.nacos.naming.core.Instances)2 ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2