Coder Social home page Coder Social logo

my-rpc-core's Introduction

my-rpc-core

基于netty的异步rpc

Usages

Client

import club.koumakan.rpc.core.RpcConfig;
import club.koumakan.rpc.core.RpcCore;
import club.koumakan.rpc.core.RpcFactory;
import club.koumakan.rpc.core.client.ConnectConfig;
import club.koumakan.rpc.core.client.functional.Callback;
import club.koumakan.rpc.core.exception.RpcCoreException;

public class Client {

    public static void main(String[] args) throws RpcCoreException {
        RpcFactory.createRpcClient(
                RpcCore.client(),
                new RpcConfig()
                        .setClassResolverType(RpcFactory.ClassResolverType.softCachingConcurrentResolver)
                        .setCompression(true)
                        .setKey("123")
                        .setNoDelay(false)
        ).connect(
                new ConnectConfig()
                        .setIpAddress("127.0.0.1")
                        .setPort(19999)
                        .setRetries(-1)
                        .setSleepMs(5000),
                (throwable, sender) -> {
                    if (throwable != null) {
                        throwable.printStackTrace();
                    } else {
                        sender.addInactiveListener(remoteAddress -> {
                            System.out.println(remoteAddress.getHostString());
                        });
                        new Thread(() -> {
                            while (true) {
                                sender.send("test", System.currentTimeMillis(), (Callback<Long>) (throwable2, responseMessage) -> {
                                    if (throwable2 != null) {
                                        throwable2.printStackTrace();
                                    } else {
                                        System.out.println(System.currentTimeMillis() - responseMessage);
                                    }
                                }, 1000);
                                try {
                                    Thread.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    }
                }
        );
    }
}

Server

import club.koumakan.rpc.core.RpcConfig;
import club.koumakan.rpc.core.RpcCore;
import club.koumakan.rpc.core.RpcFactory;
import club.koumakan.rpc.core.exception.ResponseException;
import club.koumakan.rpc.core.exception.RpcCoreException;
import club.koumakan.rpc.core.server.functional.Listener;

public class Server {

    public static void main(String[] args) throws RpcCoreException {
        RpcFactory.createRpcServer(RpcCore.server(), new RpcConfig().setKey("123").setCompression(true))
                .bind(19999, (throwable, receiver) -> {
                    receiver.publish("test", (Listener<Long>) (requestMessage, channel) -> {
                        try {
                            channel.response(requestMessage);
                        } catch (ResponseException e) {
                            e.printStackTrace();
                        }
                    });
                });
    }
}

my-rpc-core's People

Contributors

xutianyi1999 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.