jmico / beekeeper

Perl framework for building applications with a microservices architecture
Other
9 stars 2 forks source link
anyevent framework json-rpc linux microservices mqtt perl

Beekeeper

Beekeeper is a framework for building applications with a microservices architecture.

A pool of worker processes handle requests and communicate with each other through a common message bus.

Clients send requests through a different set of message buses, which are isolated for security reasons.

Requests and responses are shoveled between buses by a few router processes.

Benefits of this architecture:

Key characteristics:

What does this framework provides:

Getting Started

Creating workers

Workers provide a service accepting certain RPC calls from clients. The base class Beekeeper::Worker provides all the glue needed to accept requests and communicate trough the message bus with clients or another workers.

A worker class just declares on startup which methods it will accept, then implements them:

package MyApp::Worker;

use base 'Beekeeper::Worker';

sub on_startup {
    my $self = shift;

    $self->accept_remote_calls(
        'myapp.str.uc' => 'uppercase',
    );
}

sub uppercase {
    my ($self, $params) = @_;

    return uc $params->{'string'};
}

Creating clients

Clients of the service need an interface to use it without knowledge of the underlying RPC mechanisms. The class Beekeeper::Client provides methods to connect to the broker and make RPC calls.

This is the interface of the above service:

package MyApp::Client;

use Beekeeper::Client;

sub uppercase {
    my ($class, $str) = @_;

    my $client = Beekeeper::Client->instance;

    my $resp = $client->call_remote(
        method => 'myapp.str.uc',
        params => { string => $str },
    );

    return $resp->result;
}

Then other workers or clients can just:

use MyApp::Client;

print MyApp::Client->uppercase("hello!");

Configuring

Beekeeper applications use two config files to define how clients, workers and brokers connect to each other. These files are looked for in ENV BEEKEEPER_CONFIG_DIR, ~/.config/beekeeper and then /etc/beekeeper. File format is relaxed JSON, which allows comments and trailing commas.

The file pool.config.json defines all worker pools running on a host, specifying which logical bus should be used and which services it will run. For example:

[{
    "pool_id" : "myapp",
    "bus_id"  : "backend",
    "workers" : {
        "MyApp::Worker" : { "worker_count" : 4 },
    },
}]

The file bus.config.json defines all logical buses used by the application, specifying the connection parameters to the brokers that will service them. For example:

[{
    "bus_id"   : "backend",
    "host"     : "localhost",
    "username" : "backend",
    "password" : "def456",
}]

Neither the worker code nor the client code have hardcoded references to the logical message bus or the broker connection parameters, these communicate to each other using the definitions in these two files.

Running

To start or stop a pool of workers you use the bkpr command. Given the above example config, this will start 4 processes running MyApp::Worker code:

bkpr --pool "myapp" start

When started it daemonizes itself and forks all worker processes, then continues monitoring those forked processes and immediately respawns defunct ones.

The framework includes these command line tools to manage worker pools:

Performance

Beekeeper is pretty lightweight for being pure Perl, but the performance depends mostly on the broker performance, particularly on the broker introduced latency. The following are conservative performance estimations:

Examples

This distribution includes some examples that can be run out of the box using an internal ToyBroker (so no install of a proper broker is needed):

examples/basic is a barebones example of the usage of Beekeper.

examples/flood allows to estimate the performance of a Beekeper setup.

examples/scraper demonstrates asynchronous workers and clients.

examples/websocket uses a service from a browser using WebSockets. (live demo)

examples/chat implements a real world setup with isolated buses and redundancy. (live demo)

examples/dashboard is an HTML dashboard for Beekeeper projects. (live demo)

See also

Dependencies

This framework requires Anyevent, JSON::XS, Net::SSLeay, Term::ReadKey and ps.

To install these dependencies on a Debian system run:

apt install libanyevent-perl libjson-xs-perl libnet-ssleay-perl libterm-readkey-perl procps

License

Copyright 2015-2023 José Micó.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language itself.

CI - master CI - devel