11package com .codingapi .tx .springcloud .service .impl ;
22
3- import com .lorne .core .framework .utils .encode .MD5Util ;
43import com .codingapi .tx .listener .service .ModelNameService ;
54import com .codingapi .tx .springcloud .listener .ServerListener ;
5+ import com .lorne .core .framework .utils .encode .MD5Util ;
66import org .springframework .beans .factory .annotation .Autowired ;
77import org .springframework .beans .factory .annotation .Value ;
88import 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