Skip to content

Commit 1d49a44

Browse files
committed
获取端口号为0时等待
1 parent 6d2ccba commit 1d49a44

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

transaction-springcloud/src/main/java/com/codingapi/tx/springcloud/service/impl/ModelNameServiceImpl.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.codingapi.tx.springcloud.service.impl;
22

3-
import com.lorne.core.framework.utils.encode.MD5Util;
43
import com.codingapi.tx.listener.service.ModelNameService;
54
import com.codingapi.tx.springcloud.listener.ServerListener;
5+
import com.lorne.core.framework.utils.encode.MD5Util;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.beans.factory.annotation.Value;
88
import org.springframework.context.annotation.Configuration;
@@ -32,7 +32,7 @@ public String getModelName() {
3232
return modelName;
3333
}
3434

35-
private String getIp(){
35+
private String getIp() {
3636
if (host == null) {
3737
try {
3838
host = InetAddress.getLocalHost().getHostAddress();
@@ -43,16 +43,36 @@ private String getIp(){
4343
return host;
4444
}
4545

46+
private int getPort() {
47+
int port = serverListener.getPort();
48+
int count = 0;
49+
while (port == 0) {
50+
try {
51+
Thread.sleep(10);
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
port = serverListener.getPort();
56+
count++;
57+
58+
if(count==2000){
59+
throw new RuntimeException("get server port error.");
60+
}
61+
}
62+
63+
return port;
64+
}
65+
4666
@Override
4767
public String getUniqueKey() {
48-
String address = getIp()+serverListener.getPort();
49-
return MD5Util.md5(address.getBytes());
68+
String address = getIp() + getPort();
69+
return MD5Util.md5(address.getBytes());
5070
}
5171

5272

5373
@Override
5474
public String getIpAddress() {
55-
String address = getIp() + ":" + serverListener.getPort();
75+
String address = getIp() + ":" + getPort();
5676
return address;
5777
}
5878
}

0 commit comments

Comments
 (0)