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

The following examples show how to use com.alibaba.nacos.api.naming.pojo.Instance#setPort() . 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: 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 4
Source File: DubboNacosRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected Instance createInstance(URL url) {
    MapParametric<String, Object> context = new MapParametric(GlobalContext.getContext());
    //metadata
    Map<String, String> meta = new HashMap<>();
    String side = url.getString(ROLE_OPTION);
    meta.put(ROLE_OPTION.getName(), side);
    meta.put(DUBBO_RELEASE_KEY, DUBBO_RELEASE_VALUE);
    meta.put(DUBBO_PROTOCOL_VERSION_KEY, DUBBO_PROTOCOL_VERSION_VALUE);
    meta.put(DUBBO_PID_KEY, context.getString(KEY_PID));
    meta.put(DUBBO_INTERFACE_KEY, url.getPath());
    String serviceVersion = url.getString(SERVICE_VERSION_OPTION);
    if (serviceVersion != null && !serviceVersion.isEmpty()) {
        meta.put(DUBBO_SERVICE_VERSION_KEY, serviceVersion);
        meta.put(DUBBO_SERVICE_REVERSION_KEY, serviceVersion);
    }
    meta.put(DUBBO_GENERIC_KEY, String.valueOf(url.getBoolean(GENERIC_OPTION)));
    meta.put(DUBBO_PATH_KEY, url.getPath());
    meta.put(DUBBO_DEFAULT_KEY, "true");
    meta.put(DUBBO_PROTOCOL_KEY, DUBBO_PROTOCOL_VALUE);
    meta.put(DUBBO_APPLICATION_KEY, context.getString(KEY_APPNAME));
    meta.put(DUBBO_DYNAMIC_KEY, String.valueOf(url.getBoolean(DYNAMIC_OPTION)));
    meta.put(DUBBO_CATEGORY_KEY, SIDE_PROVIDER.equals(side) ? DUBBO_CATEGORY_PROVIDERS : DUBBO_CATEGORY_CONSUMERS);
    meta.put(DUBBO_ANYHOST_KEY, "true");
    meta.put(DUBBO_GROUP_KEY, url.getString(ALIAS_OPTION));
    meta.put(DUBBO_TIMESTAMP_KEY, String.valueOf(SystemClock.now()));
    String tagKey = url.getString(TAG_KEY_OPTION);
    String tag = url.getString(tagKey);
    if (tag != null && !tag.isEmpty()) {
        meta.put(tagKey, tag);
    }
    //创建instace
    Instance instance = new Instance();
    instance.setIp(url.getHost());
    instance.setPort(url.getPort());
    instance.setMetadata(meta);
    return instance;
}
 
Example 5
Source File: NacosRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 生成实例对象
 *
 * @param url
 * @return
 */
protected Instance createInstance(final URL url) {
    Instance instance = new Instance();
    instance.setIp(url.getHost());
    instance.setPort(url.getPort());
    instance.setMetadata(PARAMETER_FUNCTION.apply(url));
    return instance;
}
 
Example 6
Source File: NacosRegistry.java    From dubbo-registry-nacos with Apache License 2.0 5 votes vote down vote up
private Instance createInstance(URL url) {
    // Append default category if absent
    String category = url.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY);
    URL newURL = url.addParameter(CATEGORY_KEY, category);
    newURL = newURL.addParameter(PROTOCOL_KEY, url.getProtocol());
    newURL = newURL.addParameter(PATH_KEY, url.getPath());
    String ip = url.getHost();
    int port = url.getPort();
    Instance instance = new Instance();
    instance.setIp(ip);
    instance.setPort(port);
    instance.setMetadata(new HashMap<>(newURL.getParameters()));
    return instance;
}
 
Example 7
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 8
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 9
Source File: ConsulSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 5 votes vote down vote up
private Instance buildSyncInstance(HealthService instance, TaskDO taskDO) {
    Instance temp = new Instance();
    temp.setIp(instance.getService().getAddress());
    temp.setPort(instance.getService().getPort());
    Map<String, String> metaData = new HashMap<>(ConsulUtils.transferMetadata(instance.getService().getTags()));
    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 10
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 11
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 12
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;
}
 
Example 13
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;
}