Skip to content

Commit

Permalink
重构代码
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Jan 16, 2016
1 parent dda4738 commit 5283ecf
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 224 deletions.
20 changes: 10 additions & 10 deletions mpush-client/src/main/java/com/shinemo/mpush/client/PushClient.java
Expand Up @@ -4,12 +4,12 @@
import com.shinemo.mpush.api.Client;
import com.shinemo.mpush.api.PushSender;
import com.shinemo.mpush.api.connection.Connection;
import com.shinemo.mpush.common.Application;
import com.shinemo.mpush.netty.client.NettyClient;
import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.spi.ServiceContainer;
import com.shinemo.mpush.tools.thread.ThreadPoolUtil;
import com.shinemo.mpush.tools.zk.ZKPath;
import com.shinemo.mpush.tools.zk.ServerApp;
import com.shinemo.mpush.tools.zk.ZkRegister;

import org.apache.curator.framework.CuratorFramework;
Expand All @@ -26,16 +26,16 @@ public class PushClient implements PushSender {

private int defaultTimeout = 3000;
private final Map<String, Client> clientMap = new ConcurrentHashMap<>();
private final Map<String, ServerApp> servers = new ConcurrentHashMap<>();
private final Map<String, Application> servers = new ConcurrentHashMap<>();

private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class);

public void init() throws Exception {
initRedisClient();
GatewayServerZKListener listener = new GatewayServerZKListener();
Collection<ServerApp> nodes = listener.getAllServers();
Collection<Application> nodes = listener.getAllServers();
if (nodes == null || nodes.isEmpty()) return;
for (ServerApp server : nodes) {
for (Application server : nodes) {
createClient(server.getIp(), server.getPort());
}
}
Expand Down Expand Up @@ -93,13 +93,13 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event)
if (Strings.isNullOrEmpty(path)) return;

if (TreeCacheEvent.Type.NODE_ADDED == event.getType()) {
ServerApp serverApp = getServer(path);
Application serverApp = getServer(path);
if (serverApp != null) {
createClient(serverApp.getIp(), serverApp.getPort());
servers.put(path, serverApp);
}
} else if (TreeCacheEvent.Type.NODE_REMOVED == event.getType()) {
ServerApp serverApp = servers.remove(path);
Application serverApp = servers.remove(path);
if (serverApp != null) {
Client client = clientMap.remove(serverApp.getIp());
if (client != null) {
Expand All @@ -111,20 +111,20 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event)
}
}

private ServerApp getServer(String path) {
private Application getServer(String path) {
String json = zkRegister.get(path);
if (Strings.isNullOrEmpty(json)) return null;
return Jsons.fromJson(json, ServerApp.class);
return Jsons.fromJson(json, Application.class);
}

private Collection<ServerApp> getAllServers() {
private Collection<Application> getAllServers() {
List<String> list = zkRegister.getChildrenKeys(ZKPath.GATEWAY_SERVER.getPath());
if (list == null || list.isEmpty()) return Collections.EMPTY_LIST;
for (String name : list) {
String fullPath = ZKPath.GATEWAY_SERVER.getFullPath(name);
String json = zkRegister.get(fullPath);
if (com.shinemo.mpush.tools.Strings.isBlank(json)) continue;
ServerApp server = Jsons.fromJson(json, ServerApp.class);
Application server = Jsons.fromJson(json, Application.class);
if (server != null) {
servers.put(fullPath, server);
}
Expand Down
@@ -1,4 +1,4 @@
package com.shinemo.mpush.core;
package com.shinemo.mpush.common;


/**
Expand Down
Expand Up @@ -7,6 +7,7 @@

import com.google.common.collect.Lists;
import com.shinemo.mpush.api.Server;
import com.shinemo.mpush.common.Application;
import com.shinemo.mpush.tools.GenericsUtil;
import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.config.ConfigCenter;
Expand Down
@@ -1,67 +1,67 @@
package com.shinemo.mpush.core.netty;


import com.shinemo.mpush.api.Client;
import com.shinemo.mpush.netty.client.NettyClientFactory;
import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.Strings;
import com.shinemo.mpush.tools.spi.ServiceContainer;
import com.shinemo.mpush.tools.zk.ZKPath;
import com.shinemo.mpush.tools.zk.ServerApp;
import com.shinemo.mpush.tools.zk.ZkRegister;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Created by ohun on 2015/12/24.
*/
public class NettyClientTest {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class);

private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class);

public void setUp() throws Exception {
}

private List<ServerApp> getAllServers() {
List<String> list = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath());
if (list == null || list.isEmpty()) return Collections.EMPTY_LIST;
List<ServerApp> servers = new ArrayList<>();
for (String name : list) {
String json = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(name));
if (Strings.isBlank(json)) continue;
ServerApp server = Jsons.fromJson(json, ServerApp.class);
if (server != null) servers.add(server);
}
return servers;
}

public void testClient() throws Exception {
List<ServerApp> serverApps = getAllServers();
if (serverApps == null || serverApps.isEmpty()) return;
int index = (int) ((Math.random() % serverApps.size()) * serverApps.size());
ServerApp server = serverApps.get(index);
ClientChannelHandler handler = new ClientChannelHandler();
final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler);
client.init();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
client.start();
}
});
t.setDaemon(false);
t.start();
}

public static void main(String[] args) throws Exception {
NettyClientTest test = new NettyClientTest();
test.setUp();
test.testClient();
}
}
//package com.shinemo.mpush.core.netty;
//
//
//import com.shinemo.mpush.api.Client;
//import com.shinemo.mpush.netty.client.NettyClientFactory;
//import com.shinemo.mpush.tools.Jsons;
//import com.shinemo.mpush.tools.Strings;
//import com.shinemo.mpush.tools.spi.ServiceContainer;
//import com.shinemo.mpush.tools.zk.ZKPath;
//import com.shinemo.mpush.tools.zk.ServerApp;
//import com.shinemo.mpush.tools.zk.ZkRegister;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
///**
// * Created by ohun on 2015/12/24.
// */
//public class NettyClientTest {
// private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientTest.class);
//
// private static final ZkRegister zkRegister = ServiceContainer.getInstance(ZkRegister.class);
//
// public void setUp() throws Exception {
// }
//
// private List<ServerApp> getAllServers() {
// List<String> list = zkRegister.getChildrenKeys(ZKPath.CONNECTION_SERVER.getPath());
// if (list == null || list.isEmpty()) return Collections.EMPTY_LIST;
// List<ServerApp> servers = new ArrayList<>();
// for (String name : list) {
// String json = zkRegister.get(ZKPath.CONNECTION_SERVER.getFullPath(name));
// if (Strings.isBlank(json)) continue;
// ServerApp server = Jsons.fromJson(json, ServerApp.class);
// if (server != null) servers.add(server);
// }
// return servers;
// }
//
// public void testClient() throws Exception {
// List<ServerApp> serverApps = getAllServers();
// if (serverApps == null || serverApps.isEmpty()) return;
// int index = (int) ((Math.random() % serverApps.size()) * serverApps.size());
// ServerApp server = serverApps.get(index);
// ClientChannelHandler handler = new ClientChannelHandler();
// final Client client = NettyClientFactory.INSTANCE.createGet(server.getIp(), server.getPort(), handler);
// client.init();
// Thread t = new Thread(new Runnable() {
// @Override
// public void run() {
// client.start();
// }
// });
// t.setDaemon(false);
// t.start();
// }
//
// public static void main(String[] args) throws Exception {
// NettyClientTest test = new NettyClientTest();
// test.setUp();
// test.testClient();
// }
//}
@@ -1,6 +1,6 @@
package com.shinemo.mpush.cs;

import com.shinemo.mpush.core.Application;
import com.shinemo.mpush.common.Application;
import com.shinemo.mpush.tools.MPushUtil;
import com.shinemo.mpush.tools.config.ConfigCenter;
import com.shinemo.mpush.tools.zk.ZKPath;
Expand All @@ -9,9 +9,13 @@ public class ConnectionServerApplication extends Application{


public ConnectionServerApplication() {
setPort(ConfigCenter.holder.connectionServerPort());
setServerRegisterZkPath(ZKPath.CONNECTION_SERVER.getWatchPath());
setIp(MPushUtil.getLocalIp());
this(ConfigCenter.holder.connectionServerPort(),ZKPath.CONNECTION_SERVER.getWatchPath(),MPushUtil.getLocalIp());
}

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

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

import com.shinemo.mpush.core.Application;
import com.shinemo.mpush.common.Application;
import com.shinemo.mpush.tools.MPushUtil;
import com.shinemo.mpush.tools.config.ConfigCenter;
import com.shinemo.mpush.tools.zk.ZKPath;
Expand Down
Expand Up @@ -11,7 +11,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shinemo.mpush.core.Application;
import com.shinemo.mpush.common.Application;
import com.shinemo.mpush.cs.manage.ServerManage;
import com.shinemo.mpush.tools.GenericsUtil;
import com.shinemo.mpush.tools.Jsons;
Expand Down
@@ -1,4 +1,4 @@
package com.shinemo.mpush.core.zk;
package com.shinemo.mpush.zk;

import org.junit.Test;

Expand Down
@@ -1,4 +1,4 @@
package com.shinemo.mpush.core.zk;
package com.shinemo.mpush.zk;

import java.util.List;

Expand All @@ -8,12 +8,13 @@
import org.junit.Test;

import com.google.common.collect.Lists;
import com.shinemo.mpush.core.server.ConnectionServer;
import com.shinemo.mpush.cs.ConnectionServerApplication;
import com.shinemo.mpush.tools.Constants;
import com.shinemo.mpush.tools.MPushUtil;
import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.redis.RedisGroup;
import com.shinemo.mpush.tools.redis.RedisNode;
import com.shinemo.mpush.tools.zk.ServerApp;
import com.shinemo.mpush.tools.zk.ZKPath;
import com.shinemo.mpush.tools.zk.ZkRegister;
import com.shinemo.mpush.tools.zk.curator.services.ZkRegisterManager;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void testLocalIp() {
@Test
public void testRegisterIp() {
String localIp = MPushUtil.getInetAddress();
ServerApp app = new ServerApp(localIp, 3000);
ConnectionServerApplication app = new ConnectionServerApplication();
zkUtil.registerPersist("/" + localIp, Jsons.toJson(app));
String value = zkUtil.get("/" + localIp);
System.out.println(value);
Expand Down

This file was deleted.

Expand Up @@ -9,7 +9,7 @@


@SPI("zkRegister")
public interface ZkRegister<T> {
public interface ZkRegister {

public void init();

Expand Down
Expand Up @@ -30,7 +30,7 @@
import com.shinemo.mpush.tools.zk.ZkRegister;
import com.shinemo.mpush.tools.zk.listener.DataChangeListener;

public class ZkRegisterManager implements ZkRegister<Object> {
public class ZkRegisterManager implements ZkRegister {

private static final Logger LOGGER = LoggerFactory.getLogger(ZkRegisterManager.class);

Expand Down

0 comments on commit 5283ecf

Please sign in to comment.