Download vert.x를 활용한 대용량 트래픽 처리를 위한 분산 서버 개발하기

Document related concepts
no text concepts found
Transcript
vert.x를 활용한
!
대용량 트래픽 처리를 위한!
분산 서버 개발하기!
!
(vert.x & scalable architecture)
김요한
[email protected]
1. 개발 패러다임의 변화!
!
!
- 비동기 Event Looping 서버의 특징!
- JVM 기반 vert.x 특징!
!
2. 분산 서버 설계 하기!
!
- 대용량 트래픽 처리 / 체팅 서비스 서버 개발 사례!
!
3. 분산 서버 설계시 알아야 할 것들!
!
- Consistent Hashing, Sharding, Proxy, MessageQueue ..!
1. 개발 패러다임의 변화!
!
!
- 비동기 Event Looping 서버의 특징!
- JVM 기반 vert.x 특징
1. 개발 패러다임의 변화
Question
원하는 만큼 확장할 수 있나요 ?!
대용량 트래픽을 처리 할 수 있나요 ?!
기능 추가를 얼마나 빨리 할 수 있나요 ?!
쉽고 빨리 개발할 수 있나요 ?
1. 개발 패러다임의 변화
Answer
background on the C10K problem!
Event Looping!
None Blocking I/O!
Fault Tolerant!
Scalability (Scala-Out)!
......
비동기 서버의 특징
Shifting from !
Threading to Asynchronous
비동기 서버의 특징
비동기 서버의 특징
Apache HTTP Server v2.4 ideally suited for Cloud environments. They include:!
!
•
•
•
•
•
•
•
Improved performance (lower resource utilization and better concurrency)!
Reduced memory usage!
Asyncronous I/O support!
Dynamic reverse proxy configuration!
Asynchronous I/O support
Performance on par, or better, than pure event-driven Web servers!
More granular timeout and rate/resource limiting capability!
More finely-tuned caching support, tailored for high traffic servers and proxies.
http://blogs.apache.org/foundation/entry/the_apache_software_foundation_celebrates
비동기 서버의 특징
Java IO
Stream oriented Blocking IO
Reading data from a blocking stream.
비동기 서버의 특징
Java NIO
Buffer oriented Non blocking IO Selectors
Reading data from a channel until all needed data is in buffer.
http://tutorials.jenkov.com/java-nio/nio-vs-io.html
비동기 서버의 특징
many modularity paradigms
비동기 서버의 특징
비동기 서버의 특징
-
Main.class!
library1.jar!
library2.jar!
library3.jar
ClassLoader
비동기 서버의 특징
EventBus (vert.x)
Module 1
Module 2
Module 3
- Module1.class!
- library1.jar!
- library2.jar
- Module2.class!
- library1.jar!
- library2.jar
- Module2.class!
- library1.jar!
- library2.jar
ClassLoader
ClassLoader
ClassLoader
비동기 서버의 특징
Polyglot or Multilingual !
several programming languages in a single app
비동기 서버의 특징
“ Components + Scripts = Applications ”
see John Ousterhout, IEEE Computer, March ’98
http://www.stanford.edu/~ouster/cgi-bin/papers/scripting.pdf
비동기 서버의 특징
“ Shift from Apache to Node.js ”
JVM 기반 VERT.X 특징
JVM 기반 VERT.X 특징
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;
public class Server extends Verticle {
JAVA 7
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest req) {
String file = req.path().equals("/") ? "index.html" : req.path();
req.response().sendFile("webroot/" + file);
}
}).listen(8080);
}
}
> vertx run Server.java
JVM 기반 VERT.X 특징
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;
public class Server extends Verticle {
JAVA 7
public void start() {
!
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest req) {
vertx.createHttpServer().requestHandler(new
Handler<HttpServerRequest>()
public
{
String void
file = handle(HttpServerRequest
req.path().equals("/") ? "index.html" req)
: req.path();
req.response().sendFile("webroot/" + file);
}
}).listen(8080);
}
}
> vertx run Server.java
{
JVM 기반 VERT.X 특징
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;
public class Server extends Verticle {
JAVA 8
public void start() {
!
vertx.createHttpServer().requestHandler( (HttpServerRequest req) -> {
vertx.createHttpServer().requestHandler((HttpServerRequest
{
String file = req.path().equals("/") ? "index.html" : req.path();
req.response().sendFile("webroot/" + file);
}).listen(8080);
}
}
> vertx run Server.java
req) -> {
JVM 기반 VERT.X 특징
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;
public class Server extends Verticle {
JAVA 8
public void start() {
vertx.createHttpServer().requestHandler( (HttpServerRequest req) -> {
String file = req.path().equals("/") ? "index.html" : req.path();
req.response().sendFile("webroot/" + file);
}).listen(8080);
}
}
> vertx run Server.java
JVM 기반 VERT.X 특징
vert.x instance
Verticle
Verticle
Worker
Verticle
Worker
Verticle
JVM 기반 VERT.X 특징
vert.x instance
Verticle
Verticle
Worker
Verticle
EventBus
Worker
Verticle
JVM 기반 VERT.X 특징
EventBus eb = vertx.eventBus();
Handler<Message> myHandler = new Handler<Message>() {
public voidvert.x
handle(Message
message) {
instance
System.out.println("I received a message " + message.body);
} }; !
Worker
// EventBus 에 이벤트
등록
Verticle
Verticle
Verticle
eb.registerHandler("test.address", myHandler);
EventBus
// EventBus 로 이벤트 실행
eb.send("test.address", "hello world");
Worker
Verticle
EventBus
Worker
Verticle
Worker
Verticle
Verticle
vert.x instance
Verticle
Worker
Verticle
Worker
Verticle
Verticle
Verticle
JVM 기반 VERT.X 특징
vert.x instance
EventBus
Worker
Verticle
Worker
Verticle
Verticle
vert.x instance
Verticle
Worker
Verticle
Worker
Verticle
Verticle
Verticle
JVM 기반 VERT.X 특징
vert.x instance
JVM 기반 VERT.X 특징
http://vertx.io
JVM 기반 VERT.X 특징
http://vertx.io
Distributed Event Bus
JVM 기반 VERT.X 특징
http://vertx.io
WebSockets and SockJS"
support for real-time server-push applications.
JVM 기반 VERT.X 특징
http://vertx.io
embedded "
as a library in your existing Java applications
JVM 기반 VERT.X 특징
http://vertx.io
module system"
components into modules for encapsulation and reuse
JVM 기반 VERT.X 특징
http://vertx.io
Maven archetype
Gradle template
Auto-redeploy
JVM 기반 VERT.X 특징
34
http://www.techempower.com/benchmarks/
JVM 기반 VERT.X 특징
http://nodyn.io/
2. 분산 서버 설계 하기"
"
- 대용량 트래픽 처리 / 체팅 서비스 서버 개발 사례
- 분산 체팅서비스 구축하기 사례
http://stalk.io
<script src="http://www.stalk.io/stalk.js"></script>
<script language="javascript">
STALK.init();
</script>
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Chat Server
ChatsockJS)
Server
(vert.x
Chat Server
(vert.x
sockJS)
Chat
Server
(vert.x sockJS)
(vert.x sockJS)
천승희
도문준
체팅방 KEY : 노량진수산시장/개불
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Chat Server
Chat Server
Chat Server
천승희
Chat Server
. . ."
“Session 정보 공유하기” 문제 !
도문준
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Publish
도문준
천승희
Subscribe
. . ."
Socket Server
김요한
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Restful Server
Restful Server
Restful Server
Restful Server
Restful Server
HAProxy
천승희
Restful Server
도문준
Restful Server
. . ."
Socket Server
김요한
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Restful Server
Restful Server
Restful Server
Socket Server
Publish
천승희
Socket Server
도문준
Socket Server
도문준
Socket Server
Restful Server
Restful
Server
Subscribe
Restful Server
Socket Server
. . ."
김요한
김요한
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Restful Server
Restful Server
Restful Server
Socket Server
Publish
천승희
Socket Server
도문준
Socket Server
도문준
Socket Server
Restful Server
Restful
Server
Subscribe
Restful Server
Socket Server
. . ."
김요한
김요한
- 분산 체팅서비스 구축하기 사례
노량진수산시장/개불
Hash ( input )
192.168.219.1:8080
192.168.219.2:8080
192.168.219.3:8080
192.168.219.4:8080
192.168.219.3:8080
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Restful Server
Restful Server
Restful Server
Hash()
Publish
Hash()
도문준
천승희
Subscribe
Restful Server
Restful Server
Socket Server
Socket Server
. . ."
Hash()
김요한
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Restful Server
Restful Server
Restful Server
Hash()
Publish
도문준
천승희
Subscribe
. . ."
Restful Server
Restful Server
Socket Server
Socket Server
김요한
- 분산 체팅서비스 구축하기 사례
http://노량진수산시장 /개불.html
Restful Server
Restful Server
Restful Server
Restful Server
Hash()
도문준
천승희
. . ."
Restful Server
Restful Server
Socket Server
Socket Server
김요한
- 분산 체팅서비스 구축하기 사례
Restful Server
Startup &
HTTP
configuration
Module
Module
Socket
Module
Distributed Node Manager
Module
vert.x event bus
A instance in JVM
Message Queue
Module
- 분산 체팅서비스 구축하기 사례
Socket Server
Startup &
HTTP
configuration
Module
Module
Socket
Module
Distributed Node Manager
Module
vert.x event bus
A instance in JVM
Message Queue
Module
3. 분산 서버 설계시 알아야 할 것들"
"
- Consistent Hashing, Sharding, Proxy, MessageQueue ..
- Consistent Hashing
Service
Server
A
D
Token
Ring
C
value
=
HASH(key)
B
- Consistent Hashing
A
D
value
=
HASH(key)
B
C
A
-
C
구간이
넓어서
C
에
부하가
많을
수
있다
!
장애
발생
!!
- Consistent Hashing
D A
C
B
D
C
B
A
D
B C
A
D
value
=
HASH(key)
A
B
C
D
B
A
C
A
B
C
D
B
A
D
C
C
B A
D
C
B
A
value
=
HASH(key2)
- Consistent Hashing
D A
C
B
D
C
B
A
D
B C
A
D
value
=
HASH(key)
A
B
C
D
B
A
C
A
B
C
D
B
A
D
C
C
B A
D
C
B
A
value
=
HASH(key2)
- Consistent Hashing
public class ConsistentHash<T>{ !
ConsistentHash.add(‘nodeA’,...);
!
!
ConsistentHash.add(‘nodeB’,...);
!
!
!
!
!
!
!
!
!
!
ConsistentHash.add(‘nodeC’,...);
!
!
!
!
!
ConsistentHash.add(‘nodeD’,...);
!
!
! }
private final HashFunction hashFunction = Hashing.md5(); private final SortedMap<Long, T> circle = new TreeMap<Long, T>(); public void add(String name, T node) { circle.put(hashFunction.hashString(name).asLong(),node); } public void remove(String name) { circle.remove(hashFunction.hashString(name).asLong()); } public T get(String value) { long hash = hashFunction.hashString(value).asLong(); if (!circle.containsKey(hash)) { SortedMap<Long, T> tailMap = circle.tailMap(hash); }
hash = tailMap.isEmpty()?circle.firstKey():tailMap.firstKey(); } return circle.get(hash); - Consistent Hashing
public class ConsistentHash<T>{ !
private final HashFunction hashFunction = Hashing.md5(); private final SortedMap<Long, T> circle = new TreeMap<Long, T>(); private final int numberOfReplicas = 100; public void add(String name, T node) { for (int i = 0; i < numberOfReplicas; i++) { circle.put(hashFunction.hashString(name + i).asLong(),node); } } public void remove(String name) { for (int i = 0; i < numberOfReplicas; i++) { circle.remove(hashFunction.hashString(name + i).asLong()); } } public T get(String value) { long hash = hashFunction.hashString(value).asLong(); if (!circle.containsKey(hash)) { SortedMap<Long, T> tailMap = circle.tailMap(hash); }
} !
hash = tailMap.isEmpty()?circle.firstKey():tailMap.firstKey(); } return circle.get(hash); - Consistent Hashing
replics 수에 따른 표준편차(standard deviation)
서버
노드
10
개를
1
부터
500
개
까지
relicas
로
배치
10,000
번의
consistent
Hashing
결과
https://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html
- Sharding
Sharding, Shared Nothing
데이터 분산 저장
Clients
shard #1
shard #2
shard #3
A0392
E1112
J9918
A2331
G0031
K2218
C1212
G9287
O2290
.....
Replication
Sharding
- Sharding
Sharding, Shared Nothing
데이터 분산 저장
G9287
C1212
O2290
Clients
shard #1
shard #2
shard #3
A0392
E1112
J9918
A2331
G0031
K2218
C1212
G9287
O2290
A- ~ D-
E- ~ I-
J- ~
.....
- Sharding
Sharding, Shared Nothing
데이터 분산 저장
Clients
Fn(key)
shard #1
shard #2
shard #3
G0031
O2290
G9287
K2281
A0392
A2331
C1212
J9918
E1122
Consistent Hashing
.....
- Work Queueing
Message Queue, Workers
Worker Verticle 은 별도의 프로세스로 비동기/분산 실행한다.
Job
Worker
Job
Worker
Job
Broker
Server
Job
Worker
비동기 처리 (Queue)
Job
Worker
병렬 프로세싱
- Caching
Front-End Caching
Static Contents 만 Cache 하고 Dynamic Contents 는 Cache 하지 않는다.
varnish
varnish
HAProxy
Application
Server
HAProxy
HAProxy
HAProxy
HAProxy
- Caching
Data Caching
HAProxy
epilogue
- epilogue
Object Oriented 에서
Programming
Functional "
Programming 로 바뀌고 있는가 ?
XML 말고, JSON 사용해야 하는가 ?
Java 를 배우는가, Spring Framework 를 배우는가 ?
VI 와 TMUX 를 사용하는가 ?
감사합니다.
Related documents