Created
October 17, 2014 09:22
-
-
Save olivermt/21ad3895c85f8a370074 to your computer and use it in GitHub Desktop.
udp load balancer in netty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mtdevelopment.loadbalancer; | |
import io.netty.channel.ChannelHandlerContext; | |
import io.netty.channel.SimpleChannelInboundHandler; | |
import io.netty.channel.socket.DatagramPacket; | |
import java.net.InetSocketAddress; | |
import java.util.Random; | |
public class UdpLoadBalancerHandler extends SimpleChannelInboundHandler<DatagramPacket> { | |
private static final Random random = new Random(); | |
private static final String baseStringStart = "logstash-producer0"; | |
private static final String basestringEnd = ".eu.internal.vnor-op.com"; | |
String getNextServer() { | |
return baseStringStart + (random.nextInt(3)+1) + basestringEnd; | |
} | |
InetSocketAddress getServer() { | |
String nextServer = getNextServer(); | |
// System.out.println(nextServer); | |
return new InetSocketAddress(nextServer, 5060); | |
} | |
@Override | |
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) { | |
DatagramPacket newPacket = new DatagramPacket(packet.content(), getServer()); | |
newPacket.retain(); | |
ctx.write(newPacket); | |
} | |
@Override | |
public void channelReadComplete(ChannelHandlerContext ctx) { | |
ctx.flush(); | |
} | |
@Override | |
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | |
cause.printStackTrace(); | |
// We don't close the channel because we can keep serving requests. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mtdevelopment.loadbalancer; | |
import io.netty.bootstrap.Bootstrap; | |
import io.netty.channel.ChannelOption; | |
import io.netty.channel.EventLoopGroup; | |
import io.netty.channel.nio.NioEventLoopGroup; | |
import io.netty.channel.socket.nio.NioDatagramChannel; | |
public class UdpLoadBalancerServer { | |
private static final int PORT = Integer.parseInt(System.getProperty("port", "5060")); | |
public static void main(String[] args) throws Exception { | |
EventLoopGroup group = new NioEventLoopGroup(); | |
System.out.println("Started load balancer on port " + PORT + "...."); | |
try { | |
Bootstrap b = new Bootstrap(); | |
b.group(group) | |
.channel(NioDatagramChannel.class) | |
.option(ChannelOption.SO_BROADCAST, true) | |
.handler(new UdpLoadBalancerHandler()); | |
b.bind(PORT).sync().channel().closeFuture().await(); | |
} finally { | |
group.shutdownGracefully(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment