Skip to content

Commit 9a78a63

Browse files
author
夜色
committed
Merge branch 'dev'
2 parents 3f32cc3 + 3c040b7 commit 9a78a63

File tree

7 files changed

+124
-13
lines changed

7 files changed

+124
-13
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.mpush</groupId>
88
<artifactId>alloc</artifactId>
9-
<version>0.7.1</version>
9+
<version>0.8.0</version>
1010
<name>mpush-alloc</name>
1111
<description>mpush Server Allocator</description>
1212
<url>https://github.com/mpusher/mpush</url>
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.github.mpusher</groupId>
4747
<artifactId>mpush-client</artifactId>
48-
<version>0.7.1</version>
48+
<version>0.8.0</version>
4949
</dependency>
5050
</dependencies>
5151

src/main/java/com/shinemo/mpush/alloc/AllocHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
*/
4848
/*package*/ final class AllocHandler implements HttpHandler {
4949

50-
private ScheduledExecutorService scheduledExecutor;
5150
private List<ServerNode> serverNodes = Collections.emptyList();
51+
private ScheduledExecutorService scheduledExecutor;
5252
private final ServiceDiscovery discovery = ServiceDiscoveryFactory.create();
53+
private final UserManager userManager = new UserManager(null);
5354

5455
public void start() {
5556
CacheManagerFactory.create().init(); //启动缓冲服务
@@ -108,11 +109,14 @@ private void refresh() {
108109
}
109110

110111
private long getOnlineUserNum(String publicIP) {
111-
return UserManager.I.getOnlineUserNum(publicIP);
112+
return userManager.getOnlineUserNum(publicIP);
112113
}
113114

114115
private ServerNode convert(ServiceNode node) {
115116
String public_ip = node.getAttr(ServiceNames.ATTR_PUBLIC_IP);
117+
if (public_ip == null) {
118+
public_ip = node.getHost();
119+
}
116120
long onlineUserNum = getOnlineUserNum(public_ip);
117121
return new ServerNode(public_ip, node.getPort(), onlineUserNum);
118122
}

src/main/java/com/shinemo/mpush/alloc/AllocServer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ public final class AllocServer extends BaseService {
4343

4444
@Override
4545
public void init() {
46-
try {
47-
int port = CC.mp.net.cfg.getInt("alloc-server-port");
48-
this.httpServer = HttpServer.create(new InetSocketAddress(port), 0);
49-
this.allocHandler = new AllocHandler();
50-
this.pushHandler = new PushHandler();
51-
} catch (IOException e) {
52-
throw new ServiceException(e);
53-
}
46+
int port = CC.mp.net.cfg.getInt("alloc-server-port");
47+
boolean https = "https".equals(CC.mp.net.cfg.getString("alloc-server-protocol"));
48+
49+
this.httpServer = HttpServerCreator.createServer(port, https);
50+
this.allocHandler = new AllocHandler();
51+
this.pushHandler = new PushHandler();
5452

5553
httpServer.setExecutor(Executors.newCachedThreadPool());//设置线程池,由于是纯内存操作,不需要队列
5654
httpServer.createContext("/", allocHandler);//查询mpush机器
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* Copyright rememberjava.com. Licensed under GPL 3. See http://rememberjava.com/license */
2+
package com.shinemo.mpush.alloc;
3+
4+
import com.sun.net.httpserver.HttpServer;
5+
import com.sun.net.httpserver.HttpsConfigurator;
6+
import com.sun.net.httpserver.HttpsServer;
7+
8+
import javax.net.ssl.KeyManagerFactory;
9+
import javax.net.ssl.SSLContext;
10+
import javax.net.ssl.TrustManagerFactory;
11+
import java.io.File;
12+
import java.io.FileInputStream;
13+
import java.lang.ProcessBuilder.Redirect;
14+
import java.net.InetSocketAddress;
15+
import java.security.KeyStore;
16+
17+
/**
18+
* A HTTPS server using a self-signed TLS 1.2 key and certificate generated by
19+
* the Java keytool command.
20+
* <p>
21+
*/
22+
@SuppressWarnings("restriction")
23+
public class HttpServerCreator {
24+
25+
private static final File KEYSTORE_FILE = new File(System.getProperty("java.io.tmpdir"), "mpush.jks");
26+
27+
private static final String KEYSTORE_PASSWORD = "mpush_2017";
28+
29+
private static final String KEY_PASSWORD = "mpush_2017";
30+
31+
/**
32+
* Generates a new self-signed certificate in /tmp/test.jks, if it does not
33+
* already exist.
34+
*/
35+
private static void generateCertificate() throws Exception {
36+
if (KEYSTORE_FILE.exists()) return;
37+
38+
System.setProperty("javax.net.debug", "all");
39+
40+
File keytool = new File(System.getProperty("java.home"), "bin/keytool");
41+
42+
String[] genkeyCmd = new String[]{
43+
keytool.toString(),
44+
"-genkey",
45+
"-keyalg", "RSA",
46+
"-alias", "mpush.com",
47+
"-validity", "365",
48+
"-keysize", "2048",
49+
"-dname", "cn=mpush.com,ou=mpush,o=OHUN .Inc,c=CN",
50+
"-keystore", KEYSTORE_FILE.getAbsolutePath(),
51+
"-storepass", KEYSTORE_PASSWORD,
52+
"-keypass", KEY_PASSWORD};
53+
54+
System.out.println(String.join(" ", genkeyCmd));
55+
56+
ProcessBuilder processBuilder = new ProcessBuilder(genkeyCmd);
57+
processBuilder.redirectErrorStream(true);
58+
processBuilder.redirectOutput(Redirect.INHERIT);
59+
processBuilder.redirectError(Redirect.INHERIT);
60+
Process exec = processBuilder.start();
61+
exec.waitFor();
62+
63+
System.out.println("Exit value: " + exec.exitValue());
64+
65+
}
66+
67+
private static HttpsServer createHttpsServer(int port) throws Exception {
68+
generateCertificate();
69+
HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(port), 0);
70+
SSLContext sslContext = getSslContext();
71+
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
72+
return httpsServer;
73+
}
74+
75+
private static HttpServer createHttpServer(int port) throws Exception {
76+
HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 0);
77+
return httpServer;
78+
}
79+
80+
private static SSLContext getSslContext() throws Exception {
81+
KeyStore ks = KeyStore.getInstance("JKS");
82+
ks.load(new FileInputStream(KEYSTORE_FILE), KEYSTORE_PASSWORD.toCharArray());
83+
84+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
85+
kmf.init(ks, KEY_PASSWORD.toCharArray());
86+
87+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
88+
tmf.init(ks);
89+
90+
SSLContext sslContext = SSLContext.getInstance("TLS");
91+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
92+
93+
return sslContext;
94+
}
95+
96+
public static HttpServer createServer(int port, boolean https) {
97+
try {
98+
if (https) {
99+
return HttpServerCreator.createHttpsServer(port);
100+
} else {
101+
return HttpServerCreator.createHttpServer(port);
102+
}
103+
} catch (Exception e) {
104+
throw new RuntimeException(e);
105+
}
106+
}
107+
}

src/main/java/com/shinemo/mpush/alloc/PushHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private void sendPush(Map<String, Object> params) {
9494
.setCallback(new PushCallback() {
9595
@Override
9696
public void onResult(PushResult result) {
97-
logger.error(result.toString());
97+
logger.info(result.toString());
9898
}
9999
})
100100
);

src/main/resources/mpush.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mp.log-level=${log.level}
22
mp.net.gateway-server-net=tcp // 网关服务使用的网络 udp/tcp
33
mp.net.alloc-server-port=9999
4+
mp.net.alloc-server-protocol=http
45
mp.zk.server-address="127.0.0.1:2181"
56
mp.redis {// redis 集群配置
67
password:""

src/test/resources/application.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mp.log.level=debug
33
mp.log-conf-path=logback.xml
44
mp.net.gateway-server-net=tcp // 网关服务使用的网络 udp/tcp
55
mp.net.alloc-server-port=9999
6+
mp.net.alloc-server-protocol=https
67
mp.zk.server-address="127.0.0.1:2181"
78
mp.redis {// redis 集群配置
89
password:""

0 commit comments

Comments
 (0)