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

The following examples show how to use com.alibaba.nacos.api.naming.pojo.Instance#setClusterName() . 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: NamingServiceNacosImpl.java    From chronus with Apache License 2.0 6 votes vote down vote up
@Override
public void registerNode() throws Exception {
    Instance instance = new Instance();
    instance.setInstanceId(currentNode.getAddress());
    instance.setIp(currentNode.getIp());
    instance.setClusterName(currentNode.getCluster());
    instance.setPort(currentNode.getPort());
    Map<String, String> metadata = new HashMap<>();
    metadata.put(ChronusConstants.HOST_NAME, currentNode.getHostName());
    metadata.put(ChronusConstants.REGISTER_TIME, currentNode.getVersion());
    metadata.put(ChronusConstants.CLUSTER, currentNode.getCluster());
    if (Objects.equals(ChronusConstants.Y, currentNode.getEnableMaster())) {
        metadata.put(ChronusConstants.IS_MASTER, ChronusConstants.N);
    }
    metadata.put(ChronusConstants.ENABLE_MASTER, currentNode.getEnableMaster());
    metadata.put(ChronusConstants.ENABLE_EXECUTOR, currentNode.getEnableExecutor());
    metadata.put(ChronusConstants.TAG, ChronusConstants.DEF_TAG);
    metadata.put(ChronusConstants.DATA_VERSION, currentNode.getVersion());
    instance.setMetadata(metadata);
    nacosNamingService.registerInstance(ChronusConstants.NODE_NAME_CHRONUS, instance);
    //https://github.com/nacos-group/nacos-spring-project/issues/144
    //namingMaintainService = NamingMaintainFactory.createMaintainService(environment.getProperty("nacos.discovery.server-addr"));
}
 
Example 2
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 3
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 4
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 5
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private Instance getNacosInstanceFromRegistration(Registration registration) {
	Instance instance = new Instance();
	instance.setIp(registration.getHost());
	instance.setPort(registration.getPort());
	instance.setWeight(nacosDiscoveryProperties.getWeight());
	instance.setClusterName(nacosDiscoveryProperties.getClusterName());
	instance.setEnabled(nacosDiscoveryProperties.isInstanceEnabled());
	instance.setMetadata(registration.getMetadata());

	return instance;
}