Skip to content

Commit

Permalink
add zookeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
scorp2010 committed Dec 13, 2017
1 parent f48c363 commit d14b214
Show file tree
Hide file tree
Showing 43 changed files with 5,079 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@
<artifactId>motan-registry-zookeeper</artifactId>
<version>0.2.1</version>
</dependency>


<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>

<!-- spring vault -->
<dependency>
<groupId>org.springframework.vault</groupId>
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/sem/io/bio/TimeClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.sem.io.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
* Created by xuyao on 2017/8/7.
*/
public class TimeClient {

public static void main(String[] args) {
int port = 8090;
if (args != null && args.length >= 1) {

try {
port = Integer.valueOf( args[0] );
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try {
socket = new Socket("127.0.0.1", port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("QUERY TIME ORDER");
System.out.println("send order 2 server succed.");
String resp = in.readLine();
System.out.println("now is:" + resp);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (out != null) {
out.close();
out = null;
}
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
socket = null;
}

}
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/sem/io/bio/TimeServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.sem.io.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Created by xuyao on 2017/8/7.
*/
public class TimeServer {

public static void main(String[] args) throws IOException {
int port = 8090;
if (args != null && args.length >= 1) {

try {
port = Integer.valueOf(args[0]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
ServerSocket server = null;
try {
server = new ServerSocket(port);
System.out.println("start prot:" + port);
Socket socket = null;
while (true) {
socket = server.accept();
new Thread(new TimerServerHandler(socket)).start();
}
} finally {
if (server != null) {
System.out.println("server close");
server.close();
server = null;
}
}
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/sem/io/bio/TimerServerHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.sem.io.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
import org.springframework.util.StringUtils;

/**
* Created by xuyao on 2017/8/7.
*/
public class TimerServerHandler implements Runnable {

private Socket socket;

public TimerServerHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
String currentTime = null;
String body = null;
while (true) {
body = in.readLine();
if (body == null) {
break;
}
System.out.println("server receive order:"+body);
currentTime = ("QUERY TIME ORDER".equalsIgnoreCase(body)) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";
out.println(currentTime);
}

} catch (Exception e) {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e1) {
e1.printStackTrace();
}
if (out != null) {
out.close();
out = null;
}
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
socket = null;
}
}
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/sem/io/nio/MultiplexerTimeServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.sem.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
* Created by xuyao on 2017/8/8.
*/
public class MultiplexerTimeServer implements Runnable {

private Selector selector;
private ServerSocketChannel servChannel;
private volatile boolean stop;

/**
* 初始化多路复用器、绑定监听端口
*
* @param port
*/
public MultiplexerTimeServer(int port) {
try {
selector = Selector.open();
servChannel = ServerSocketChannel.open();
servChannel.configureBlocking(false);
servChannel.socket().bind(new InetSocketAddress(port), 1024);
servChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("he time server is start in port : " + port);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

public void stop() {
this.stop = true;
}

@Override
public void run() {
while (!stop) {
try {
selector.select(1000);

} catch (Exception e) {

}
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/sem/io/nio/TimeServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sem.io.nio;

import com.sem.io.bio.TimerServerHandler;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Created by xuyao on 2017/8/7.
*/
public class TimeServer {

public static void main(String[] args) throws IOException {
int port = 8090;
if (args != null && args.length >= 1) {

try {
port = Integer.valueOf(args[0]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
// MultiplexerTimeServer
}
}
Loading

0 comments on commit d14b214

Please sign in to comment.