Netty - anfix tech&beers

Post on 09-Jul-2015

96 views 1 download

transcript

¿Qué  narices  es  esto?

Siempre  hay  un  villano

Suppose  you  go  to  +Starbucks  Coffee    ,  you  EnQueue  (brush  up  Data  Structures  :P  )    to  order  your  coffee,  you  stay  in  the  queue  unFl  you  get  your  chance  to  order,  and  once  you  order  and  pay,  you  sFll  stay  there  blocking  the  queue  unFl  you  get  your  coffee,  won't  that  be  awkward  ??  (if  No  then  go  Synchronous,  that's  best  for  you  :)  else  come  to  the  world  of  NeKy  )  because  we  love  Asynchronous  things  in  life  e.g  Chat  vs  Call.  

Síncrono  no....  asíncrono!!!

IO  vs  NIO

1)  IO  streams  versus  NIO  blocksposiblidad  de  recorrer  el  buffer

2)  Synchronous  vs.  Asynchronous  IO  stream  -­‐>  hilo  bloqueado  nio  -­‐>  mismo  hilo  sirve  diferentes  canales

3)  IO  Versus  NIO  APIs  apis  más  sencillas,  no  necesidad  de  crear  buffer

ServidorClienteHTTP

FTP

Mail

Finanzas  -­‐  real  Fme,  otros

Streaming

Ficheros  grandes

Cliente  HTTP

Cliente  FTP

Cliente  Mail

Cliente  Ficheros

Cliente  Finanzas

Cliente  Streaming

-­‐ Complicado  de  programar  por  la  heterogeneidad

-­‐ Mal  rendimiento

-­‐ Mal  escalado

:((((

Framework  para  el  desarrollo  de  clientes  y  servidores.  Facilita  toda  la  programación  del  protocolo.  API  unificada  para  diferentes  transportes  y  protocolos.

Diseñado  para  crear  clientes-­‐servidores  altamente  escalables  y  de  alto  rendimiento.

Desarrollo  fácil  y  rápido  :)

hKp://neKy.io  -­‐  proyecto  de  código  abierto  disponible  en  github

Soporta  SPDY!!!

Features

Design

• Unified  API  for  various  transport  types  -­‐  blocking  and  non-­‐blocking  socket• Based  on  a  flexible  and  extensible  event  model  which  allows  clear  separaBon  of  concerns• Highly  customizable  thread  model  -­‐  single  thread,  one  or  more  thread  pools  such  as  SEDA• True  connecBonless  datagram  socket  support  (since  3.1)

Ease  of  use

• Well-­‐documented  Javadoc,  user  guide  and  examples• No  addiBonal  dependencies  but  JDK  1.5  (or  above)

Performance

• BeTer  throughput,  lower  latency• Less  resource  consumpBon• Minimized  unnecessary  memory  copy

Security

• Complete  SSL/TLS  and  StartTLS  support

Community

• Release  early,  release  oZen• The  author  has  been  wriBng  similar  frameworks  since  2003  and  he  sBll  finds  your  feed  back  precious!

Para  empezar  a  cocinar  con  neKy...

-­‐ Ingredientes:

-­‐ Tener  Java  instalado  (JDK  6)  

-­‐ Tener  descargado  el  proyecto  de  neKy.io

-­‐ 5.0.0  versión  alpha  (jdk  1.7)

-­‐ 4.0.17  la  úlFma  estable  (jdk  1.6)

-­‐ También  en  modo  “maven”

-­‐ Montarlo  en  Eclipse  o  Netbeans  para  un  proyecto

<dependencies>    ...    <dependency>        <groupId>io.neKy</groupId>        <arFfactId>neKy</arFfactId>  <!-­‐-­‐  Use  'neKy-­‐all'  for  4.0  or  above  -­‐-­‐>        <version>X.Y.Z.Q</version>        <scope>compile</scope>    </dependency>    ...</dependencies>

Terminología:

Channel: a abstraction for communication or transfer of data (byte array)

ChannelBuffer: Byte array which can provide random or direct data storage mechanism.

ChannelPipeline: collection of handlers to process your events in the way you

want. ChannelFactory: Resource allocators, like thread pools.

ChannelHandler: Provides method to handle events on channel.ChannelEvents: Events like connected, exception raised, message recieved

etc.

o  lo  vemos  en  acción  o  no  me  entero...

¿y  esto  se  usa?

Twitter

hKps://blog.twiKer.com/2014/neKy-­‐at-­‐twiKer-­‐with-­‐finagle

143,199 TPS

Facebookpublic  void  startServer()  {        //  Create  the  handler        MyService.Iface  serviceInterface  =  new  MyServiceHandler();

       //  Create  the  processor        TProcessor  processor  =  new  MyService.Processor<>(serviceInterface);

       //  Build  the  server  definiFon        ThrisServerDef  serverDef  =  new  ThrisServerDefBuilder().withProcessor(processor)                                                                                                                        .build();

       //  Create  the  server  transport        final  NeKyServerTransport  server  =  new  NeKyServerTransport(serverDef,                                                                                                                                  new  NeKyConfigBuilder(),                                                                                                                                  new  DefaultChannelGroup(),                                                                                                                                  new  HashedWheelTimer());

       //  Create  neKy  boss  and  executor  thread  pools        ExecutorService  bossExecutor  =  Executors.newCachedThreadPool();        ExecutorService  workerExecutor  =  Executors.newCachedThreadPool();

       //  Start  the  server        server.start(bossExecutor,  workerExecutor);

       //  Arrange  to  stop  the  server  at  shutdown        RunFme.getRunFme().addShutdownHook(new  Thread()  {                @Override                public  void  run()  {                        try  {                                server.stop();                        }  catch  (InterruptedExcepFon  e)  {                                Thread.currentThread().interrupt();                        }                }        });}

hKps://github.com/facebook/nisy

Remember  the  milk

hKp://blog.rememberthemilk.com/2011/08/real-­‐Fme-­‐updaFng-­‐comes-­‐to-­‐the-­‐remember-­‐the-­‐milk-­‐web-­‐app/

Gracias!(por  aguantar...)