Skip to content

Commit

Permalink
获取端口号为0时等待
Browse files Browse the repository at this point in the history
  • Loading branch information
xlorne committed Dec 20, 2017
1 parent 6d2ccba commit 1d49a44
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.codingapi.tx.springcloud.service.impl;

import com.lorne.core.framework.utils.encode.MD5Util;
import com.codingapi.tx.listener.service.ModelNameService;
import com.codingapi.tx.springcloud.listener.ServerListener;
import com.lorne.core.framework.utils.encode.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -32,7 +32,7 @@ public String getModelName() {
return modelName;
}

private String getIp(){
private String getIp() {
if (host == null) {
try {
host = InetAddress.getLocalHost().getHostAddress();
Expand All @@ -43,16 +43,36 @@ private String getIp(){
return host;
}

private int getPort() {
int port = serverListener.getPort();
int count = 0;
while (port == 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
port = serverListener.getPort();
count++;

if(count==2000){
throw new RuntimeException("get server port error.");
}
}

return port;
}

@Override
public String getUniqueKey() {
String address = getIp()+serverListener.getPort();
return MD5Util.md5(address.getBytes());
String address = getIp() + getPort();
return MD5Util.md5(address.getBytes());
}


@Override
public String getIpAddress() {
String address = getIp() + ":" + serverListener.getPort();
String address = getIp() + ":" + getPort();
return address;
}
}

0 comments on commit 1d49a44

Please sign in to comment.