WATCH服务增加功能:更改Server的监听地址和端口
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
*/
|
||||
package org.redkale.boot.watch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
import javax.annotation.Resource;
|
||||
@@ -24,6 +26,9 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
@Comment("不存在的Server节点")
|
||||
public static final int RET_SERVER_NOT_EXISTS = 1602_0001;
|
||||
|
||||
@Comment("更改Server监听地址端口失败")
|
||||
public static final int RET_SERVER_CHANGEPORT_ERROR = 1602_0002;
|
||||
|
||||
@Resource
|
||||
protected Application application;
|
||||
|
||||
@@ -45,6 +50,25 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
return new RetResult(rs);
|
||||
}
|
||||
|
||||
@RestMapping(name = "changeaddress", comment = "更改Server的监听地址和端口")
|
||||
public RetResult changeAddress(@RestParam(name = "#port:") final int oldport,
|
||||
@RestParam(name = "#newhost:") final String newhost, @RestParam(name = "#newport:") final int newport) {
|
||||
if (oldport < 1) return new RetResult(RET_WATCH_PARAMS_ILLEGAL, "not found param `oldport`");
|
||||
if (newport < 1) return new RetResult(RET_WATCH_PARAMS_ILLEGAL, "not found param `newport`");
|
||||
Stream<NodeServer> stream = application.getNodeServers().stream();
|
||||
NodeServer node = stream.filter(ns -> ns.getServer().getSocketAddress().getPort() == oldport).findFirst().orElse(null);
|
||||
if (node == null) return new RetResult(RET_SERVER_NOT_EXISTS, "Server(port=" + oldport + ") not found");
|
||||
final Server server = node.getServer();
|
||||
InetSocketAddress newAddr = new InetSocketAddress(newhost == null || newhost.isEmpty() ? server.getSocketAddress().getHostString() : newhost, newport);
|
||||
try {
|
||||
server.changeAddress(newAddr);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new RetResult(RET_SERVER_CHANGEPORT_ERROR, "changeaddress error");
|
||||
}
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
private Map<String, Object> formatToMap(NodeServer node) {
|
||||
Server server = node.getServer();
|
||||
Map<String, Object> rs = new LinkedHashMap<>();
|
||||
|
||||
@@ -47,27 +47,6 @@ public class Context {
|
||||
//服务的根Servlet
|
||||
protected final PrepareServlet prepare;
|
||||
|
||||
//服务的监听地址
|
||||
private final InetSocketAddress address;
|
||||
|
||||
//字符集
|
||||
protected final Charset charset;
|
||||
|
||||
//最大连接数, 为0表示没限制
|
||||
protected final int maxconns;
|
||||
|
||||
//请求内容的大小上限, 默认64K
|
||||
protected final int maxbody;
|
||||
|
||||
//keep alive IO读取的超时时间
|
||||
protected final int aliveTimeoutSeconds;
|
||||
|
||||
//IO读取的超时时间
|
||||
protected final int readTimeoutSeconds;
|
||||
|
||||
//IO写入的超时时间
|
||||
protected final int writeTimeoutSeconds;
|
||||
|
||||
//日志Logger
|
||||
protected final Logger logger;
|
||||
|
||||
@@ -80,6 +59,27 @@ public class Context {
|
||||
//依赖注入工厂类
|
||||
protected final ResourceFactory resourceFactory;
|
||||
|
||||
//最大连接数, 为0表示没限制
|
||||
protected int maxconns;
|
||||
|
||||
//请求内容的大小上限, 默认64K
|
||||
protected int maxbody;
|
||||
|
||||
//keep alive IO读取的超时时间
|
||||
protected int aliveTimeoutSeconds;
|
||||
|
||||
//IO读取的超时时间
|
||||
protected int readTimeoutSeconds;
|
||||
|
||||
//IO写入的超时时间
|
||||
protected int writeTimeoutSeconds;
|
||||
|
||||
//服务的监听地址
|
||||
protected InetSocketAddress address;
|
||||
|
||||
//字符集
|
||||
protected Charset charset;
|
||||
|
||||
public Context(ContextConfig config) {
|
||||
this(config.serverStartTime, config.logger, config.executor, config.sslContext,
|
||||
config.bufferCapacity, config.bufferPool, config.responsePool, config.maxconns, config.maxbody,
|
||||
|
||||
@@ -12,8 +12,9 @@ import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.*;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import org.redkale.net.Filter;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -287,6 +288,74 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
||||
+ ", started in " + (System.currentTimeMillis() - context.getServerStartTime()) + " ms");
|
||||
}
|
||||
|
||||
public void changeAddress(final InetSocketAddress addr) throws IOException {
|
||||
long s = System.currentTimeMillis();
|
||||
Objects.requireNonNull(addr);
|
||||
final InetSocketAddress oldAddress = context.address;
|
||||
final ProtocolServer oldServerChannel = this.serverChannel;
|
||||
context.address = addr;
|
||||
ProtocolServer newServerChannel = null;
|
||||
try {
|
||||
newServerChannel = ProtocolServer.create(this.protocol, context, this.serverClassLoader, config == null ? null : config.getValue("netimpl"));
|
||||
newServerChannel.open(config);
|
||||
newServerChannel.bind(addr, backlog);
|
||||
newServerChannel.accept();
|
||||
} catch (IOException e) {
|
||||
context.address = oldAddress;
|
||||
throw e;
|
||||
}
|
||||
this.address = context.address;
|
||||
this.serverChannel = newServerChannel;
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol))
|
||||
+ " change address listen: " + address + ", started in " + (System.currentTimeMillis() - s) + " ms");
|
||||
if (oldServerChannel != null) {
|
||||
new Thread() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(10_000);
|
||||
oldServerChannel.close();
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, "Server.changeInetSocketAddress(addr=" + addr + ") error", e);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void changeMaxconns(final int newmaxconns) {
|
||||
this.maxconns = newmaxconns;
|
||||
if (this.context != null) this.context.maxconns = newmaxconns;
|
||||
if (this.serverChannel != null) this.serverChannel.maxconns = newmaxconns;
|
||||
}
|
||||
|
||||
public void changeCharset(final Charset newcharset) {
|
||||
this.charset = newcharset;
|
||||
if (this.context != null) this.context.charset = newcharset;
|
||||
}
|
||||
|
||||
public void changeMaxbody(final int newmaxbody) {
|
||||
this.maxbody = newmaxbody;
|
||||
if (this.context != null) this.context.maxbody = newmaxbody;
|
||||
}
|
||||
|
||||
public void changeReadTimeoutSeconds(final int newReadTimeoutSeconds) {
|
||||
this.readTimeoutSeconds = newReadTimeoutSeconds;
|
||||
if (this.context != null) this.context.readTimeoutSeconds = newReadTimeoutSeconds;
|
||||
}
|
||||
|
||||
public void changeWriteTimeoutSeconds(final int newWriteTimeoutSeconds) {
|
||||
this.writeTimeoutSeconds = newWriteTimeoutSeconds;
|
||||
if (this.context != null) this.context.writeTimeoutSeconds = newWriteTimeoutSeconds;
|
||||
}
|
||||
|
||||
public void changeAliveTimeoutSeconds(final int newAliveTimeoutSeconds) {
|
||||
this.aliveTimeoutSeconds = newAliveTimeoutSeconds;
|
||||
if (this.context != null) this.context.aliveTimeoutSeconds = newAliveTimeoutSeconds;
|
||||
}
|
||||
|
||||
protected abstract C createContext();
|
||||
|
||||
public void shutdown() throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user