Skip to content

Commit 45bc585

Browse files
committed
优化端口获取不到的情况
1 parent 84af836 commit 45bc585

File tree

7 files changed

+35
-21
lines changed

7 files changed

+35
-21
lines changed

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnLoadBalancerRule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.codingapi.ribbon.loadbalancer;
22

33

4-
import com.alibaba.fastjson.JSONObject;
5-
64
import com.codingapi.tx.aop.bean.TxTransactionLocal;
75
import com.lorne.core.framework.utils.encode.MD5Util;
86
import com.netflix.loadbalancer.Server;

transaction-springcloud/src/main/java/com/codingapi/ribbon/loadbalancer/LcnRibbonConfiguration.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
3232
return new LcnZoneAwareLoadBalancerProxy(config, rule, ping, serverList,
3333
serverListFilter, serverListUpdater);
3434
}
35-
36-
/*@Bean
37-
public IRule ribbonRule(){
38-
return new LcnRandomRuleProxy();
39-
}*/
35+
4036

4137
}

transaction-springcloud/src/main/java/com/codingapi/tx/springcloud/feign/TransactionRestTemplateInterceptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class TransactionRestTemplateInterceptor implements RequestInterceptor {
1616

1717
@Override
1818
public void apply(RequestTemplate requestTemplate) {
19+
1920
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
2021
String groupId = txTransactionLocal == null ? null : txTransactionLocal.getGroupId();
2122
int maxTimeOut = txTransactionLocal == null ? 0 : txTransactionLocal.getMaxTimeOut();

tx-client/src/main/java/com/codingapi/tx/listener/service/impl/InitServiceImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.codingapi.tx.Constants;
44
import com.codingapi.tx.listener.service.InitService;
55
import com.codingapi.tx.listener.service.ModelNameService;
6-
import com.codingapi.tx.listener.service.TimeOutService;
76
import com.codingapi.tx.netty.service.NettyService;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
@@ -21,9 +20,6 @@ public class InitServiceImpl implements InitService {
2120
@Autowired
2221
private NettyService nettyService;
2322

24-
@Autowired
25-
private TimeOutService timeOutService;
26-
2723

2824
@Autowired
2925
private ModelNameService modelNameService;
@@ -41,8 +37,5 @@ public void start() {
4137
nettyService.start();
4238
logger.info("socket-start..");
4339

44-
//timeOutService.loadOutTime();
45-
46-
4740
}
4841
}

tx-client/src/main/java/com/codingapi/tx/netty/handler/TransactionHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,27 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
5858
}
5959

6060
@Override
61-
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
62-
super.channelUnregistered(ctx);
61+
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
62+
super.channelInactive(ctx);
63+
64+
logger.info("connection -->" + ctx);
6365

6466
SocketManager.getInstance().setNetState(false);
6567
//链接断开,重新连接
6668
nettyControlService.restart();
67-
6869
}
6970

7071
@Override
7172
public void channelActive(ChannelHandlerContext ctx) throws Exception {
7273
super.channelActive(ctx);
7374
SocketManager.getInstance().setCtx(ctx);
7475

75-
logger.info("try connection -->" + ctx);
76-
77-
nettyControlService.uploadModelInfo();
76+
logger.info("disconnection -->" + ctx);
7877

7978
//通道激活后进行心跳检查
8079
SocketUtils.sendMsg(ctx, heartJson);
80+
81+
nettyControlService.uploadModelInfo();
8182
}
8283

8384

tx-client/src/main/java/com/codingapi/tx/netty/service/impl/NettyControlServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.alibaba.fastjson.JSONObject;
44
import com.codingapi.tx.control.service.TransactionControlService;
55
import com.codingapi.tx.framework.utils.SocketManager;
6+
import com.codingapi.tx.listener.service.ModelNameService;
67
import com.codingapi.tx.netty.service.MQTxManagerService;
78
import com.codingapi.tx.netty.service.NettyControlService;
89
import com.codingapi.tx.netty.service.NettyService;
10+
import com.codingapi.tx.netty.utils.IpAddressUtils;
911
import com.lorne.core.framework.utils.task.ConditionUtils;
1012
import com.lorne.core.framework.utils.task.IBack;
1113
import com.lorne.core.framework.utils.task.Task;
@@ -36,6 +38,9 @@ public class NettyControlServiceImpl implements NettyControlService {
3638
@Autowired
3739
private MQTxManagerService mqTxManagerService;
3840

41+
@Autowired
42+
private ModelNameService modelNameService;
43+
3944

4045
private Executor threadPool = Executors.newFixedThreadPool(100);
4146

@@ -56,7 +61,7 @@ public void uploadModelInfo() {
5661
new Thread(new Runnable() {
5762
@Override
5863
public void run() {
59-
while (!SocketManager.getInstance().isNetState()) {
64+
while (!SocketManager.getInstance().isNetState()|| !IpAddressUtils.isIpAddress(modelNameService.getIpAddress())) {
6065
try {
6166
Thread.sleep(1000 * 5);
6267
} catch (InterruptedException e) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.codingapi.tx.netty.utils;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* create by lorne on 2017/12/12
8+
*/
9+
public class IpAddressUtils {
10+
11+
private final static String ipAddressRegex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}:([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5]{2}[0-3][0-5])";
12+
13+
private final static Pattern pattern = Pattern.compile(ipAddressRegex);
14+
15+
public static boolean isIpAddress(String ipAddress){
16+
Matcher matcher = pattern.matcher(ipAddress);
17+
return matcher.matches();
18+
}
19+
20+
}

0 commit comments

Comments
 (0)