Skip to content

Commit

Permalink
gateway server与 connection server 集成
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Jan 16, 2016
1 parent bd28c09 commit c3b1bfe
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 100 deletions.
Expand Up @@ -86,20 +86,20 @@ private void initServer(){
} }


//step6 启动 netty server //step6 启动 netty server
private void startServer(){ public void startServer(final Server server){
ThreadPoolUtil.newThread(new Runnable() { ThreadPoolUtil.newThread(new Runnable() {
@Override @Override
public void run() { public void run() {
server.init(); server.init();
server.start(new Server.Listener() { server.start(new Server.Listener() {
@Override @Override
public void onSuccess() { public void onSuccess() {
log.error("mpush app start connection server success...."); log.error("mpush app start "+server.getClass().getSimpleName()+" server success....");
} }


@Override @Override
public void onFailure(String message) { public void onFailure(String message) {
log.error("mpush app start connection server failure, jvm exit with code -1"); log.error("mpush app start "+server.getClass().getSimpleName()+" server failure, jvm exit with code -1");
System.exit(-1); System.exit(-1);
} }
}); });
Expand All @@ -109,8 +109,8 @@ public void onFailure(String message) {
} }


//step7 注册应用到zk //step7 注册应用到zk
public void registerServerToZk(){ public void registerServerToZk(String path,String value){
zkRegister.registerEphemeralSequential(application.getServerRegisterZkPath(), Jsons.toJson(application)); zkRegister.registerEphemeralSequential(path, value);
} }


public void start(){ public void start(){
Expand All @@ -119,14 +119,18 @@ public void start(){
registerListeners(); registerListeners();
initListenerData(); initListenerData();
initServer(); initServer();
startServer(); startServer(server);
registerServerToZk(); registerServerToZk(application.getServerRegisterZkPath(),Jsons.toJson(application));
} }


public void stop(){ public void stopServer(Server server){
if(server!=null){ if(server!=null){
server.stop(null); server.stop(null);
} }
} }


public void stop(){
stopServer(server);
}

} }
Expand Up @@ -7,6 +7,7 @@


public class ConnectionServerApplication extends Application{ public class ConnectionServerApplication extends Application{


private transient GatewayServerApplication gatewayServerApplication;


public ConnectionServerApplication() { public ConnectionServerApplication() {
this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp()); this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp());
Expand All @@ -18,4 +19,12 @@ public ConnectionServerApplication(int port,String path,String ip) {
setIp(ip); setIp(ip);
} }


public GatewayServerApplication getGatewayServerApplication() {
return gatewayServerApplication;
}

public void setGatewayServerApplication(GatewayServerApplication gatewayServerApplication) {
this.gatewayServerApplication = gatewayServerApplication;
}

} }
Expand Up @@ -5,37 +5,50 @@
import com.shinemo.mpush.api.Server; import com.shinemo.mpush.api.Server;
import com.shinemo.mpush.core.AbstractServer; import com.shinemo.mpush.core.AbstractServer;
import com.shinemo.mpush.core.server.ConnectionServer; import com.shinemo.mpush.core.server.ConnectionServer;
import com.shinemo.mpush.core.server.GatewayServer;
import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener; import com.shinemo.mpush.cs.zk.listener.impl.ConnectionServerPathListener;
import com.shinemo.mpush.cs.zk.listener.impl.PushServerPathListener; import com.shinemo.mpush.cs.zk.listener.impl.GatewayServerPathListener;
import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.zk.listener.impl.RedisPathListener;


public class ConnectionServerMain extends AbstractServer<ConnectionServerApplication>{ public class ConnectionServerMain extends AbstractServer<ConnectionServerApplication>{


private Server gatewayServer;

private ConnectionServerApplication connectionServerApplication;

private GatewayServerApplication gatewayServerApplication;

public ConnectionServerMain(){ public ConnectionServerMain(){


registerListener(new RedisPathListener());
registerListener(new PushServerPathListener());
registerListener(new ConnectionServerPathListener()); registerListener(new ConnectionServerPathListener());
registerListener(new GatewayServerPathListener());


connectionServerApplication = (ConnectionServerApplication)application;
gatewayServerApplication = new GatewayServerApplication();
connectionServerApplication.setGatewayServerApplication(gatewayServerApplication);
gatewayServer = new GatewayServer(gatewayServerApplication.getPort());
}

@Override
public void start() {
super.start();
startServer(gatewayServer);
registerServerToZk(gatewayServerApplication.getServerRegisterZkPath(), Jsons.toJson(gatewayServerApplication));
} }



@Override @Override
public Server getServer() { public Server getServer() {
final int port = ConfigCenter.holder.connectionServerPort(); final int port = application.getPort();
ConnectionServer connectionServer = new ConnectionServer(port); ConnectionServer connectionServer = new ConnectionServer(port);
return connectionServer; return connectionServer;
} }



public static void main(String[] args) { @Override
final ConnectionServerMain connectionServerMain = new ConnectionServerMain(); public void stop() {
connectionServerMain.start(); super.stop();
Runtime.getRuntime().addShutdownHook(new Thread() { stopServer(gatewayServer);
public void run() {
connectionServerMain.stop();
}
});
} }



} }
@@ -1,16 +1,20 @@
package com.shinemo.mpush.cs; package com.shinemo.mpush.cs;


import com.shinemo.mpush.common.Application;
import com.shinemo.mpush.tools.MPushUtil; import com.shinemo.mpush.tools.MPushUtil;
import com.shinemo.mpush.tools.config.ConfigCenter; import com.shinemo.mpush.tools.config.ConfigCenter;
import com.shinemo.mpush.tools.zk.ZKPath; import com.shinemo.mpush.tools.zk.ZKPath;


public class GatewayServerApplication extends Application{

public class GatewayServerApplication extends ConnectionServerApplication{

public GatewayServerApplication() { public GatewayServerApplication() {
setPort(ConfigCenter.holder.gatewayServerPort()); this(ConfigCenter.holder.gatewayServerPort(),ZKPath.GATEWAY_SERVER.getWatchPath(),MPushUtil.getLocalIp());
setServerRegisterZkPath(ZKPath.GATEWAY_SERVER.getWatchPath());
setIp(MPushUtil.getLocalIp());
} }


public GatewayServerApplication(int port,String path,String ip) {
setPort(port);
setServerRegisterZkPath(path);
setIp(ip);
}

} }
15 changes: 15 additions & 0 deletions mpush-cs/src/main/java/com/shinemo/mpush/cs/Main.java
@@ -0,0 +1,15 @@
package com.shinemo.mpush.cs;

public class Main {

public static void main(String[] args) {
final ConnectionServerMain connectionServerMain = new ConnectionServerMain();
connectionServerMain.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
connectionServerMain.stop();
}
});
}

}

This file was deleted.

This file was deleted.

0 comments on commit c3b1bfe

Please sign in to comment.