Support specified naming UDP push port for client (#5439)

* Nacos client within docker container could not receive service changes via udp

* change code style
This commit is contained in:
张志勇 2021-04-22 15:10:04 +08:00 committed by GitHub
parent e0fe828542
commit 103e671d90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -64,11 +64,13 @@ public class PropertyKeyConst {
public static final String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount"; public static final String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount";
public static final String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount"; public static final String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount";
public static final String NAMING_REQUEST_DOMAIN_RETRY_COUNT = "namingRequestDomainMaxRetryCount"; public static final String NAMING_REQUEST_DOMAIN_RETRY_COUNT = "namingRequestDomainMaxRetryCount";
public static final String NAMING_PUSH_EMPTY_PROTECTION = "namingPushEmptyProtection"; public static final String NAMING_PUSH_EMPTY_PROTECTION = "namingPushEmptyProtection";
public static final String PUSH_RECEIVER_UDP_PORT = "push.receiver.udp.port";
/** /**
* Get the key value of some variable value from the system property. * Get the key value of some variable value from the system property.
*/ */

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.naming.core; package com.alibaba.nacos.client.naming.core;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.IoUtils;
@ -25,6 +26,7 @@ import com.alibaba.nacos.common.utils.ThreadUtils;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
@ -51,10 +53,19 @@ public class PushReceiver implements Runnable, Closeable {
private volatile boolean closed = false; private volatile boolean closed = false;
public static String getPushReceiverUdpPort() {
return System.getenv(PropertyKeyConst.PUSH_RECEIVER_UDP_PORT);
}
public PushReceiver(HostReactor hostReactor) { public PushReceiver(HostReactor hostReactor) {
try { try {
this.hostReactor = hostReactor; this.hostReactor = hostReactor;
this.udpSocket = new DatagramSocket(); String udpPort = getPushReceiverUdpPort();
if (StringUtils.isEmpty(udpPort)) {
this.udpSocket = new DatagramSocket();
} else {
this.udpSocket = new DatagramSocket(new InetSocketAddress(Integer.parseInt(udpPort)));
}
this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {