提交 4e0fa1e3 编写于 作者: 小张

增加退出handler

上级 e968e762
......@@ -4,7 +4,6 @@ key=ztgreat
#proxy-server地址
server.host=127.0.0.1
#server.host=67.216.203.138
#server.host=47.97.111.38
#proxy-server 服务端口
server.port=6666
\ No newline at end of file
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 10M;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65s 65s;
......@@ -35,14 +20,10 @@ http {
# 配置dsn 服务器,后面解析host的时候需要
resolver 8.8.8.8 ipv6=off;
server {
listen 80;
server_name windows10.microdone.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
location /{
proxy_pass http://$host:9090; # 需要配置dns
......
......@@ -2,28 +2,12 @@
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
......@@ -36,82 +20,10 @@ http {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
location / {
proxy_pass http://127.0.0.1:9091;
#proxy_http_version 1.1;
proxy_redirect ~^http://127.0.0.1:9090(.*) http://127.0.0.1$1;
}
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
port: 6666
server: 67.216.203.138
server: 127.0.0.1
#每秒 并发量
concurrent: 1000
httpPort: 9090
client:
ztgreat:
- domain: www.ztgreat.cn
- domain: windows10.microdone.cn
proxyType: http
realhost: 127.0.0.1
realhostport: 8080
......
port: 6666
server: 67.216.203.138
#每秒 并发量
concurrent: 1000
httpPort: 9090
client:
ztgreat:
- domain: proxy.ztgreat.cn
proxyType: http
realhost: 127.0.0.1
realhostport: 8000
description: http代理
- serverport: 9091
proxyType: http
realhost: 127.0.0.1
realhostport: 8081
description: http代理
- serverport: 3307
proxyType: tcp
realhost: 127.0.0.1
realhostport: 3306
description: mysql 代理
- serverport: 2222
proxyType: tcp
realhost: 172.16.254.63
realhostport: 22
description: ssh 代理
......@@ -6,9 +6,11 @@ import com.proxy.common.entity.server.ClientNode;
import com.proxy.common.entity.server.ProxyRealServer;
import com.proxy.common.protocol.CommonConstant;
import com.proxy.server.handler.*;
import com.proxy.server.service.LifeCycle;
import com.proxy.server.service.LogBackConfigLoader;
import com.proxy.server.service.ServerBeanManager;
import com.proxy.server.service.SharableHandlerManager;
import com.proxy.server.task.ExitHandler;
import com.proxy.server.util.ProxyUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
......@@ -25,8 +27,9 @@ import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
public class ProxyServer {
public class ProxyServer implements LifeCycle{
private static Logger logger = LoggerFactory.getLogger(ProxyServer.class);
/**
* 最大帧长度
......@@ -52,7 +55,7 @@ public class ProxyServer {
* 并发量
*/
public static int concurrent = 1000;
private static Logger logger = LoggerFactory.getLogger(ProxyServer.class);
/**
* 绑定端口,默认6666
*/
......@@ -62,7 +65,10 @@ public class ProxyServer {
*/
public Integer httpPort;
/**
* 服务端channel
*/
public Channel channel;
......@@ -76,12 +82,14 @@ public class ProxyServer {
public static void main(String[] args)throws Exception{
//加载日志
LogBackConfigLoader.load();
try {
new ProxyServer().start();
//退出钩子
Runtime.getRuntime().addShutdownHook(new ExitHandler());
ProxyServer proxyServer = new ProxyServer();
ServerBeanManager.setProxyServer(proxyServer);
proxyServer.start();
}catch (Exception e){
e.printStackTrace();
}
......@@ -144,7 +152,6 @@ public class ProxyServer {
this.httpPort = (int) ServerBeanManager.getConfigService().getConfigure("httpPort");
}
try {
//配置代理信息
configurProxy();
......@@ -178,10 +185,9 @@ public class ProxyServer {
public ChannelFuture startMainServer(){
//根据配置文件启动服务
ChannelFuture future=bind();
this.channel = future.channel();
return future;
}
......@@ -297,4 +303,13 @@ public class ProxyServer {
return null;
}
@Override
public void shutDown() {
try {
this.channel.close();
logger.debug("{}端口:代理服务退出:",this.port);
}catch (Exception e){
}
}
}
......@@ -12,6 +12,8 @@ import io.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* 本地 端口(tcp)/域名 绑定 具体操作层
* @author ztgreat
......@@ -71,7 +73,9 @@ public class ProxyChannelDao {
ProxyChannel proxyChannel= proxyChannelCache.get(serverPort);
if (proxyChannel==null)
return false;
proxyChannel.getChannel().close();
if(proxyChannel.getChannel()!=null && proxyChannel.getChannel().isActive()){
proxyChannel.getChannel().close();
}
//不移除
// proxyChannelCache.remove(serverPort);
return true;
......@@ -136,4 +140,8 @@ public class ProxyChannelDao {
public ProxyChannel getServerProxy(Object key) {
return proxyChannelCache.get(key);
}
public Map<Object,ProxyChannel>getAll(){
return proxyChannelCache.getAll();
}
}
package com.proxy.server.service;
/**
* 生命周期控制
*/
public interface LifeCycle {
public void shutDown();
}
......@@ -5,13 +5,18 @@ import com.proxy.common.entity.server.ProxyRealServer;
import com.proxy.server.dao.ProxyChannelDao;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* 本地 端口(tcp)/域名 绑定服务层
* @author ztgreat
*/
public class ProxyChannelService {
public class ProxyChannelService implements LifeCycle{
private static Logger logger = LoggerFactory.getLogger(ProxyChannelService.class);
private static ProxyChannelDao proxyChannelDao =new ProxyChannelDao();
......@@ -80,4 +85,21 @@ public class ProxyChannelService {
proxyChannelDao.addByServerdomain(domain,proxyRealServer);
}
public Map<Object,ProxyChannel> getAll(){
return proxyChannelDao.getAll();
}
@Override
public void shutDown() {
try {
Map<Object,ProxyChannel> map = this.getAll();
for(Map.Entry<Object,ProxyChannel>entry:map.entrySet()){
entry.getValue().getChannel().close();
logger.debug("代理服务(端口/域名):{} 退出",entry.getKey());
}
}catch (Exception e){
}
}
}
......@@ -2,6 +2,7 @@ package com.proxy.server.service;
import com.proxy.common.util.NumberGenerate;
import com.proxy.common.util.SessionIDGenerate;
import com.proxy.server.ProxyServer;
import com.proxy.server.handler.traffic.handler.TrafficLimitHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
......@@ -20,6 +21,11 @@ public class ServerBeanManager {
*/
private static ServerBootstrap bootstrap;
/**
* 代理服务端实例
*/
private static ProxyServer proxyServer;
/**
* 用于生成sessionID
*/
......@@ -93,4 +99,12 @@ public class ServerBeanManager {
public static TrafficLimitHandler getTrafficLimitHandler() {
return trafficLimitHandler;
}
public static ProxyServer getProxyServer() {
return proxyServer;
}
public static void setProxyServer(ProxyServer proxyServer) {
ServerBeanManager.proxyServer = proxyServer;
}
}
package com.proxy.server.task;
import com.proxy.server.service.ServerBeanManager;
/**
* ctrl +c
* 当在控制台使用 ctrl+c退出时,清理数据,准备退出
*/
public class ExitHandler extends Thread{
@Override
public void run() {
System.out.println("正在退出...");
try {
ServerBeanManager.getProxyServer().shutDown();
ServerBeanManager.getProxyChannelService().shutDown();
}catch (Exception e){
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册