Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #2992] Use new code style for nacos-address module. #3239

Merged
merged 2 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions address/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.3.1-BETA</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-address</artifactId>
<packaging>jar</packaging>

<name>nacos-address ${project.version}</name>
<url>http://nacos.io</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -62,15 +62,15 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>

<reporting>
<plugins>
<plugin>
Expand All @@ -79,7 +79,7 @@
</plugin>
</plugins>
</reporting>

<profiles>
<profile>
<id>release-address</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.address;

import org.springframework.boot.SpringApplication;
Expand All @@ -26,8 +27,9 @@
*/
@SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
public class AddressServer {

public static void main(String[] args) {

SpringApplication.run(AddressServer.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.address.component;

import com.alibaba.nacos.address.constant.AddressServerConstants;
Expand All @@ -34,32 +35,38 @@
*/
@Component
public class AddressServerGeneratorManager {


/**
* Generate product name.
*
* @param name name
* @return product
*/
public String generateProductName(String name) {

if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {

return AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME;
}

return String.format(AddressServerConstants.ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE, name);
}

/**
* Note: if the parameter inputted is empty then will return the empty list.
*
* @param serviceName
* @param clusterName
* @param ipArray
* @return
* @param serviceName service name
* @param clusterName cluster name
* @param ipArray array of ips
* @return instance list
*/
public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName, String[] ipArray) {
if (StringUtils.isEmpty(serviceName)
|| StringUtils.isEmpty(clusterName)
|| ipArray == null || ipArray.length == 0) {
public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName,
String[] ipArray) {
if (StringUtils.isEmpty(serviceName) || StringUtils.isEmpty(clusterName) || ipArray == null
|| ipArray.length == 0) {
return Collections.emptyList();
}

List<Instance> instanceList = new ArrayList<>(ipArray.length);
for (String ip : ipArray) {
String[] ipAndPort = generateIpAndPort(ip);
Expand All @@ -73,46 +80,50 @@ public List<Instance> generateInstancesByIps(String serviceName, String rawProdu
instance.setEphemeral(false);
instanceList.add(instance);
}

return instanceList;
}

public String[] generateIpAndPort(String ip) {

private String[] generateIpAndPort(String ip) {
int index = ip.indexOf(AddressServerConstants.IP_PORT_SEPARATOR);
if (index != -1) {

return new String[]{ip.substring(0, index), ip.substring(index + 1)};
return new String[] {ip.substring(0, index), ip.substring(index + 1)};
}

return new String[]{ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)};
return new String[] {ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)};
}

/**
* Generate response ips.
*
* @param instanceList a instance set will generate string response to client.
* @return the result of response to client
*/
public String generateResponseIps(List<Instance> instanceList) {

StringBuilder ips = new StringBuilder();
instanceList.forEach(instance -> {
ips.append(instance.getIp() + ":" + instance.getPort());
ips.append("\n");
});

return ips.toString();
}

/**
* @param rawServiceName the raw service name will not contains the {@Constans.DEFAULT_GROUP}
* Generate nacos service name.
*
* @param rawServiceName the raw service name will not contains the {@link Constants#DEFAULT_GROUP}.
* @return the nacos service name
*/
public String generateNacosServiceName(String rawServiceName) {

if (rawServiceName.indexOf(Constants.DEFAULT_GROUP) != -1) {
return rawServiceName;
}

return Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + rawServiceName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.address.component;

import com.alibaba.nacos.address.constant.AddressServerConstants;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

Expand All @@ -28,51 +30,51 @@
*/
@Component
public class AddressServerManager {

public String getRawProductName(String name) {

if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {

return AddressServerConstants.DEFAULT_PRODUCT;
}

return name;
}

/**
* <p>
* if the name is empty then return the default {@UtilAndCommons#DEFAULT_CLUSTER_NAME},
* <p>
* or return the source name by input
* If the name is empty then return the default {@link UtilsAndCommons#DEFAULT_CLUSTER_NAME}, or return the source
* name by input.
*
* @param name
* @return
* @param name name
* @return default cluster name
*/
public String getDefaultClusterNameIfEmpty(String name) {

if (StringUtils.isEmpty(name) || AddressServerConstants.DEFAULT_GET_CLUSTER.equals(name)) {
return AddressServerConstants.DEFAULT_GET_CLUSTER;
}

return name;
}

public String getRawClusterName(String name) {

return getDefaultClusterNameIfEmpty(name);
}

/**
* Split ips.
*
* @param ips multi ip will separator by the ','
* @return
* @return array of ip
*/
public String[] splitIps(String ips) {

if (StringUtils.isBlank(ips)) {

return new String[0];
}

return ips.split(AddressServerConstants.MULTI_IPS_SEPARATOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,65 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.address.constant;

import com.alibaba.nacos.naming.misc.UtilsAndCommons;

/**
* Uniform constant parameter naming for address servers and default values ​​for related parameters
* Uniform constant parameter naming for address servers and default values ​​for related parameters.
*
* @author pbting
* @date 2019-06-17 7:23 PM
* @since 1.1.0
*/
public interface AddressServerConstants {


/**
* the default server port when create the Instance object.
*/
int DEFAULT_SERVER_PORT = 8848;

/**
* when post ips is not given the product,then use the default.
*/
String DEFAULT_PRODUCT = "nacos";

/**
* the separator between ip and port.
*/
String IP_PORT_SEPARATOR = ":";

/**
* the separator for {@Service#name} between raw service name and group
* the separator for service name between raw service name and group.
*/
String GROUP_SERVICE_NAME_SEP = "@@";

/**
* when post ips is not given the cluster,then use the default.
*/
String DEFAULT_GET_CLUSTER = "serverlist";

/**
* post multi ip will use the "," to separator
* post multi ip will use the "," to separator.
*/
String MULTI_IPS_SEPARATOR = ",";

/**
* the default product name when deploy nacos with naming and config
* the default product name when deploy nacos with naming and config.
*/
String ALIWARE_NACOS_DEFAULT_PRODUCT_NAME = "nacos.as.default";

/**
* when the config and naming will separate deploy,then must specify product name by the client
* when the config and naming will separate deploy,then must specify product name by the client.
*/
String ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE = "nacos.as.%s";

/**
* the url for address server prefix
* the url for address server prefix.
*/
String ADDRESS_SERVER_REQUEST_URL =
UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/as";

UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/as";
}
Loading