Java Code Examples for com.alibaba.nacos.api.naming.pojo.Instance#setServiceName()

The following examples show how to use com.alibaba.nacos.api.naming.pojo.Instance#setServiceName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NacosSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
public Instance buildSyncInstance(Instance instance, TaskDO taskDO) {
    Instance temp = new Instance();
    temp.setIp(instance.getIp());
    temp.setPort(instance.getPort());
    temp.setClusterName(instance.getClusterName());
    temp.setServiceName(instance.getServiceName());
    temp.setEnabled(instance.isEnabled());
    temp.setHealthy(instance.isHealthy());
    temp.setWeight(instance.getWeight());

    Map<String, String> metaData = new HashMap<>();
    metaData.putAll(instance.getMetadata());
    metaData.put(SkyWalkerConstants.DEST_CLUSTERID_KEY, taskDO.getDestClusterId());
    metaData.put(SkyWalkerConstants.SYNC_SOURCE_KEY,
        skyWalkerCacheServices.getClusterType(taskDO.getSourceClusterId()).getCode());
    metaData.put(SkyWalkerConstants.SOURCE_CLUSTERID_KEY, taskDO.getSourceClusterId());
    temp.setMetadata(metaData);
    return temp;
}
 
Example 2
Source File: ZookeeperSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
/**
 * create Nacos service instance
 *
 * @param queryParam dubbo metadata
 * @param ipAndPortMap dubbo ip and address
 */
protected Instance buildSyncInstance(Map<String, String> queryParam, Map<String, String> ipAndPortMap,
    TaskDO taskDO) {
    Instance temp = new Instance();
    temp.setIp(ipAndPortMap.get(INSTANCE_IP_KEY));
    temp.setPort(Integer.parseInt(ipAndPortMap.get(INSTANCE_PORT_KEY)));
    temp.setServiceName(getServiceNameFromCache(taskDO.getTaskId(), queryParam));
    temp.setWeight(Double.parseDouble(queryParam.get(WEIGHT_KEY) == null ? "1.0" : queryParam.get(WEIGHT_KEY)));
    temp.setHealthy(true);

    Map<String, String> metaData = new HashMap<>(queryParam);
    metaData.put(PROTOCOL_KEY, ipAndPortMap.get(PROTOCOL_KEY));
    metaData.put(SkyWalkerConstants.DEST_CLUSTERID_KEY, taskDO.getDestClusterId());
    metaData.put(SkyWalkerConstants.SYNC_SOURCE_KEY,
        skyWalkerCacheServices.getClusterType(taskDO.getSourceClusterId()).getCode());
    metaData.put(SkyWalkerConstants.SOURCE_CLUSTERID_KEY, taskDO.getSourceClusterId());
    temp.setMetadata(metaData);
    return temp;
}
 
Example 3
Source File: NacosNamingServiceUtils.java    From dubbo-registry-nacos with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the {@link ServiceInstance} to {@link Instance}
 *
 * @param serviceInstance {@link ServiceInstance}
 * @return non-null
 * @since 2.7.5
 */
public static Instance toInstance(ServiceInstance serviceInstance) {
    Instance instance = new Instance();
    instance.setInstanceId(serviceInstance.getId());
    instance.setServiceName(serviceInstance.getServiceName());
    instance.setIp(serviceInstance.getHost());
    instance.setPort(serviceInstance.getPort());
    instance.setMetadata(serviceInstance.getMetadata());
    instance.setEnabled(serviceInstance.isEnabled());
    instance.setHealthy(serviceInstance.isHealthy());
    return instance;
}
 
Example 4
Source File: EurekaSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 5 votes vote down vote up
private Instance buildSyncInstance(InstanceInfo instance, TaskDO taskDO) {
    Instance temp = new Instance();
    temp.setIp(instance.getIPAddr());
    temp.setPort(instance.getPort());
    temp.setServiceName(instance.getAppName());
    temp.setHealthy(true);

    Map<String, String> metaData = new HashMap<>(instance.getMetadata());
    metaData.put(SkyWalkerConstants.DEST_CLUSTERID_KEY, taskDO.getDestClusterId());
    metaData.put(SkyWalkerConstants.SYNC_SOURCE_KEY,
        skyWalkerCacheServices.getClusterType(taskDO.getSourceClusterId()).getCode());
    metaData.put(SkyWalkerConstants.SOURCE_CLUSTERID_KEY, taskDO.getSourceClusterId());
    temp.setMetadata(metaData);
    return temp;
}
 
Example 5
Source File: NacosRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert provider to instances list.
 *
 * @param providerConfig the provider config 
 * @return the list
 */
static List<Instance> convertProviderToInstances(ProviderConfig providerConfig) {
    @SuppressWarnings("unchecked")
    List<ServerConfig> servers = providerConfig.getServer();
    if (servers != null && !servers.isEmpty()) {
        List<Instance> instances = new ArrayList<Instance>();
        for (ServerConfig server : servers) {
            String serviceName = buildServiceName(providerConfig, server.getProtocol());
            Instance instance = new Instance();
            instance.setClusterName(DEFAULT_CLUSTER);
            instance.setServiceName(serviceName);

            // set host port
            String host = server.getVirtualHost();
            if (host == null) {
                host = server.getHost();
                if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) {
                    host = SystemInfo.getLocalHost();
                }
            }
            instance.setIp(host);
            instance.setPort(server.getPort());

            // set meta data
            Map<String, String> metaData = RegistryUtils.convertProviderToMap(providerConfig, server);
            instance.setMetadata(metaData);

            instances.add(instance);
        }
        return instances;
    }
    return null;
}
 
Example 6
Source File: NacosRegistryHelperTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void convertInstancesToProviders() {
    Instance instance = new Instance();
    instance.setClusterName(NacosRegistryHelper.DEFAULT_CLUSTER);
    instance.setIp("1.1.1.1");
    instance.setPort(12200);
    instance.setServiceName("com.alipay.xxx.TestService");

    List<ProviderInfo> providerInfos = NacosRegistryHelper
        .convertInstancesToProviders(Lists.newArrayList(instance));
    assertNotNull(providerInfos);
    assertEquals(1, providerInfos.size());

    ProviderInfo providerInfo = providerInfos.get(0);
    assertNotNull(providerInfo);
    assertEquals(instance.getIp(), providerInfo.getHost());
    assertEquals(instance.getPort(), providerInfo.getPort());

    assertEquals(RpcConfigs.getStringValue(RpcOptions.DEFAULT_PROTOCOL), providerInfo.getProtocolType());

    Map<String, String> metaData = Maps.newHashMap();
    metaData.put(RpcConstants.CONFIG_KEY_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
    instance.setMetadata(metaData);

    providerInfos = NacosRegistryHelper.convertInstancesToProviders(Lists.newArrayList(instance));
    providerInfo = providerInfos.get(0);
    assertEquals(RpcConstants.PROTOCOL_TYPE_REST, providerInfo.getProtocolType());
}
 
Example 7
Source File: NacosMockTest.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public static Instance serviceInstance(String serviceName, boolean isHealthy,
		Map<String, String> metadata) {
	Instance instance = new Instance();
	instance.setInstanceId(UUID.randomUUID().toString());
	instance.setServiceName(serviceName);
	instance.setHealthy(isHealthy);
	instance.setMetadata(metadata);
	return instance;
}
 
Example 8
Source File: NacosMockTest.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public static Instance serviceInstance(String serviceName, boolean isHealthy,
		String host, int port, Map<String, String> metadata) {
	Instance instance = new Instance();
	instance.setIp(host);
	instance.setPort(port);
	instance.setServiceName(serviceName);
	instance.setHealthy(isHealthy);
	instance.setMetadata(metadata);
	return instance;
}