美文网首页
Netty负载均衡

Netty负载均衡

作者: 诺之林 | 来源:发表于2019-03-13 16:07 被阅读0次

本文的示例代码参考NettyLB

目录

Netty

Server1

mkdir NettyLB && cd NettyLB

mkdir NettyServer1 && cd NettyServer1

gradle init --type java-application

gradle run
# Hello world.

记得添加.gitignore => "gi gradle >> .gitignore"

vim build.gradle
# compile 'io.netty:netty-all:4.1.25.Final'
mkdir -p src/main/java/server

vim src/main/java/server/ServerHandler.java
package server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;

public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("Server1: " + byteBuf.toString(Charset.forName("utf-8")));
    }
}
mv src/main/java/App.java src/main/java/server/Server.java

vim src/main/java/server/Server.java
package server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Server {

    private static final int PORT = 8881;

    public static void main(String[] args) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap
                .group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ServerHandler());
                    }
                });

        serverBootstrap.bind(PORT).addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("端口[" + PORT + "] 绑定成功!");
            } else {
                System.err.println("端口[" + PORT + "] 绑定失败!!!");
            }
        });
    }
}
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8881] 绑定成功!
# Server1: hello

telnet 127.0.0.1 8881
# Connected to localhost.
# hello

Server2

cd .. && cp -R NettyServer1 NettyServer2 && cd NettyServer2

sed -i "" "s/8881/8882/g" src/main/java/server/Server.java

sed -i "" "s/Server1/Server2/g" src/main/java/server/ServerHandler.java
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8882] 绑定成功!
# Server2: hello

telnet 127.0.0.1 8882
# Connected to localhost.
# hello

Nginx

nginx -V
# nginx version: nginx/1.13.11
# configure arguments: --with-stream

vim nginx.conf

The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter

stream {
    upstream netty {
        server 127.0.0.1:8881;
        server 127.0.0.1:8882;
    }
    server {
        listen 7000;
        proxy_connect_timeout 1s;
        proxy_pass netty;
    }
}
sudo nginx -t

sudo nginx -s reload
# cd NettyServer1
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8881] 绑定成功!
# Server1: yes

# cd NettyServer2
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8882] 绑定成功!
# Server2: no

telnet 127.0.0.1 7000
# Connected to localhost.
# yes

telnet 127.0.0.1 7000
# Connected to localhost.
# no

参考

相关文章

  • Netty负载均衡

    本文的示例代码参考NettyLB 目录 NettyServer1Server2 Nginx Netty Serve...

  • 阿里云负载均衡+Netty服务器

    一、Netty服务器TCP的负载均衡 Netty服务器对外提供tcp长连接和端口 用Netty可实现高性能高并发的...

  • Netty Tcp 负载均衡

    为什么要负载均衡 随着设备连接数增多,单台服务器支撑的TCP长连接数有限,这个时候程序架构就需要改变,一般都会采取...

  • 如何从零开发58的nodejs-rpc客户端

    参考知识: 1、负载均衡; 2、IO模型; 3、Netty工作原理; 4、TcpClient; 5、如何解码jav...

  • 关于负载均衡的总结

    负载均衡的分类 常见的分为三类,dns负载均衡,硬件负载均衡,软件负载均衡 dns负载均衡 一般实现地域级别的负载...

  • 负载均衡-LVS

    负载均衡lvs by shihang.mai 负载均衡层次 负载均衡拓扑 负载均衡-D-NAT模式 Client发...

  • 当面试官问我Nginx如何实现负载均衡,我这么答让他眼前一亮

    负载均衡介绍 在介绍Nginx的负载均衡实现之前,先简单的说下负载均衡的分类,主要分为硬件负载均衡和软件负载均衡,...

  • 负载均衡

    负载均衡图例负载均衡.png 负载均衡策略 轮询 ip_hash 均衡参数 down 表示当前的服务不参与负载均衡...

  • SpringBoot实现负载均衡(Nginx支撑)

    负载均衡介绍介绍在介绍Nginx的负载均衡实现之前,先简单的说下负载均衡的分类,主要分为硬件负载均衡和软件负载均衡...

  • Nginx-进阶学习

    目录: Nginx集群和负载均衡 一、Nginx集群和负载均衡 1、集群 2、负载均衡-权重(1)负载均衡-轮训:...

网友评论

      本文标题:Netty负载均衡

      本文链接:https://www.haomeiwen.com/subject/tqdnpqtx.html