All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.nacos.api.naming.utils.NamingUtils Maven / Gradle / Ivy

There is a newer version: 2.0.4.RELEASE
Show newest version
/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.nacos.api.naming.utils;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.utils.StringUtils;

/**
 * NamingUtils.
 *
 * @author nkorange
 * @since 1.0.0
 */
public class NamingUtils {
    
    /**
     * Returns a combined string with serviceName and groupName. serviceName can not be nil.
     *
     * 

In most cases, serviceName can not be nil. In other cases, for search or anything, See {@link * com.alibaba.nacos.api.naming.utils.NamingUtils#getGroupedNameOptional(String, String)} * *

etc: *

serviceName | groupName | result

*

serviceA | groupA | groupA@@serviceA

*

nil | groupA | threw IllegalArgumentException

* * @return 'groupName@@serviceName' */ public static String getGroupedName(final String serviceName, final String groupName) { if (StringUtils.isBlank(serviceName)) { throw new IllegalArgumentException("Param 'serviceName' is illegal, serviceName is blank"); } if (StringUtils.isBlank(groupName)) { throw new IllegalArgumentException("Param 'groupName' is illegal, groupName is blank"); } final String resultGroupedName = groupName + Constants.SERVICE_INFO_SPLITER + serviceName; return resultGroupedName.intern(); } public static String getServiceName(final String serviceNameWithGroup) { if (StringUtils.isBlank(serviceNameWithGroup)) { return StringUtils.EMPTY; } if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) { return serviceNameWithGroup; } return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[1]; } public static String getGroupName(final String serviceNameWithGroup) { if (StringUtils.isBlank(serviceNameWithGroup)) { return StringUtils.EMPTY; } if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) { return Constants.DEFAULT_GROUP; } return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[0]; } /** * check combineServiceName format. the serviceName can't be blank. *
     * serviceName = "@@";                 the length = 0; illegal
     * serviceName = "group@@";            the length = 1; illegal
     * serviceName = "@@serviceName";      the length = 2; illegal
     * serviceName = "group@@serviceName"; the length = 2; legal
     * 
* * @param combineServiceName such as: groupName@@serviceName */ public static void checkServiceNameFormat(String combineServiceName) { String[] split = combineServiceName.split(Constants.SERVICE_INFO_SPLITER); if (split.length <= 1) { throw new IllegalArgumentException( "Param 'serviceName' is illegal, it should be format as 'groupName@@serviceName'"); } if (split[0].isEmpty()) { throw new IllegalArgumentException("Param 'serviceName' is illegal, groupName can't be empty"); } } /** * Returns a combined string with serviceName and groupName. Such as 'groupName@@serviceName' *

This method works similar with {@link com.alibaba.nacos.api.naming.utils.NamingUtils#getGroupedName} But not * verify any parameters. * *

etc: *

serviceName | groupName | result

*

serviceA | groupA | groupA@@serviceA

*

nil | groupA | groupA@@

*

nil | nil | @@

* * @return 'groupName@@serviceName' */ public static String getGroupedNameOptional(final String serviceName, final String groupName) { return groupName + Constants.SERVICE_INFO_SPLITER + serviceName; } /** *

Check instance param about keep alive.

* *
     * heart beat timeout must > heart beat interval
     * ip delete timeout must  > heart beat interval
     * 
* * @param instance need checked instance * @throws NacosException if check failed, throw exception */ public static void checkInstanceIsLegal(Instance instance) throws NacosException { if (instance.getInstanceHeartBeatTimeOut() < instance.getInstanceHeartBeatInterval() || instance.getIpDeleteTimeout() < instance.getInstanceHeartBeatInterval()) { throw new NacosException(NacosException.INVALID_PARAM, "Instance 'heart beat interval' must less than 'heart beat timeout' and 'ip delete timeout'."); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy