Commit 4924ed56 authored by duanruiming's avatar duanruiming

[update] 配置端口常量

parent 1f4db17f
...@@ -4,6 +4,7 @@ import net.wanji.com.netty.NettyClient; ...@@ -4,6 +4,7 @@ import net.wanji.com.netty.NettyClient;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
...@@ -25,6 +26,10 @@ public class Application implements ApplicationRunner { ...@@ -25,6 +26,10 @@ public class Application implements ApplicationRunner {
@Qualifier(value = "threadPoolExecutor") @Qualifier(value = "threadPoolExecutor")
@Autowired @Autowired
static ThreadPoolTaskExecutor threadPoolExecutor; static ThreadPoolTaskExecutor threadPoolExecutor;
@Value("${portParam.localPort}")
int localPort;
@Value("${portParam.remotePort}")
int remoteProt;
public static void main(String[] args) { public static void main(String[] args) {
...@@ -33,6 +38,6 @@ public class Application implements ApplicationRunner { ...@@ -33,6 +38,6 @@ public class Application implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
NettyClient.start(5050, threadPoolExecutor); NettyClient.start(localPort, remoteProt, threadPoolExecutor);
} }
} }
...@@ -35,15 +35,15 @@ public class NettyClient { ...@@ -35,15 +35,15 @@ public class NettyClient {
/** /**
* 建立连接 * 建立连接
*/ */
public static void start(int port, ThreadPoolTaskExecutor threadPoolExecutor) throws InterruptedException { public static void start(int localPort, int remotePort, ThreadPoolTaskExecutor threadPoolExecutor) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup(); EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group) bootstrap.group(group)
.channel(NioDatagramChannel.class) .channel(NioDatagramChannel.class)
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535)) .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535))
// .localAddress(new InetSocketAddress(5051)) // .localAddress(new InetSocketAddress(5051))
// 绑定bending端口 // 绑定端口
.remoteAddress(new InetSocketAddress(5051)) .remoteAddress(new InetSocketAddress(remotePort))
.handler(new ChannelInitializer<NioDatagramChannel>() { .handler(new ChannelInitializer<NioDatagramChannel>() {
@Override @Override
protected void initChannel(NioDatagramChannel datagramChannel) { protected void initChannel(NioDatagramChannel datagramChannel) {
...@@ -51,13 +51,13 @@ public class NettyClient { ...@@ -51,13 +51,13 @@ public class NettyClient {
.addLast(new NettyServerHandler(threadPoolExecutor)); .addLast(new NettyServerHandler(threadPoolExecutor));
} }
}); });
ChannelFuture channelFuture = bootstrap.bind(5050).sync(); ChannelFuture channelFuture = bootstrap.bind(localPort).sync();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
group.shutdownGracefully(); group.shutdownGracefully();
channelFuture.channel().closeFuture().syncUninterruptibly(); channelFuture.channel().closeFuture().syncUninterruptibly();
log.warn("server is closed"); log.warn("server is closed");
})); }));
log.info("udp application is running. binding port is {}", port); log.info("udp application is running. binding port is {}", remotePort);
NettyClient.udpChannelFuture = channelFuture; NettyClient.udpChannelFuture = channelFuture;
} }
...@@ -98,11 +98,11 @@ public class NettyClient { ...@@ -98,11 +98,11 @@ public class NettyClient {
long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
NettyMessageCache.NETTY_MESSAGE_RESULT_MAP.forEach((k, v) -> { NettyMessageCache.NETTY_MESSAGE_RESULT_MAP.forEach((k, v) -> {
// threadPoolExecutor.execute(() -> { // threadPoolExecutor.execute(() -> {
if (now - v.getStartTime() > v.getWaitMillisecond()) { if (now - v.getStartTime() > v.getWaitMillisecond()) {
log.warn("wait {}ms {} timeout", v.getWaitMillisecond(), k); log.warn("wait {}ms {} timeout", v.getWaitMillisecond(), k);
v.getCountDownLatch().countDown(); v.getCountDownLatch().countDown();
} }
}); });
// }); // });
} }
} }
\ No newline at end of file
...@@ -67,3 +67,7 @@ threadPoolConfig: ...@@ -67,3 +67,7 @@ threadPoolConfig:
queueCapacity: 200 queueCapacity: 200
keepAliveTime: 6000 keepAliveTime: 6000
allowCoreTimeOut: false allowCoreTimeOut: false
#东土通讯端口配置
portParam:
localPort: 5051
remotePort: 5050
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment