Hands-On Microservices with Rust
eBook - ePub

Hands-On Microservices with Rust

Build, test, and deploy scalable and reactive microservices with Rust 2018

Denis Kolodin

Partager le livre
  1. 520 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Hands-On Microservices with Rust

Build, test, and deploy scalable and reactive microservices with Rust 2018

Denis Kolodin

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

A comprehensive guide in developing and deploying high performance microservices with Rust

Key Features

  • Start your microservices journey and get a broader perspective on microservices development using RUST 2018,
  • Build, deploy, and test microservices using AWS
  • Explore advanced techniques for developing microservices such as actor model, Requests Routing, and threads

Book Description

Microservice architecture is sweeping the world as the de facto pattern for building web-based applications. Rust is a language particularly well-suited for building microservices. It is a new system programming language that offers a practical and safe alternative to C.

This book describes web development using the Rust programming language and will get you up and running with modern web frameworks and crates with examples of RESTful microservices creation. You will deep dive into Reactive programming, and asynchronous programming, and split your web application into a set of concurrent actors. The book provides several HTTP-handling examples with manageable memory allocations. You will walk through stateless high-performance microservices, which are ideally suitable for computation or caching tasks, and look at stateful microservices, which are filled with persistent data and database interactions. As we move along, you will learn how to use Rust macros to describe business or protocol entities of our application and compile them into native structs, which will be performed at full speed with the help of the server's CPU.

Finally, you will be taken through examples of how to test and debug microservices and pack them into a tiny monolithic binary or put them into a container and deploy them to modern cloud platforms such as AWS.

What you will learn

  • Get acquainted with leveraging Rust web programming
  • Get to grips with various Rust crates, such as hyper, Tokio, and Actix
  • Explore RESTful microservices with Rust
  • Understand how to pack Rust code to a container using Docker
  • Familiarize yourself with Reactive microservices
  • Deploy your microservices to modern cloud platforms such as AWS

Who this book is for

This book is for developers who have basic knowledge of RUST, and want to learn how to build, test, scale, and manage RUST microservices. No prior experience of writing microservices in RUST is assumed.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Hands-On Microservices with Rust est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Hands-On Microservices with Rust par Denis Kolodin en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming in C++. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2019
ISBN
9781789341980
Édition
1

Reliable Integration with Databases

Persistent microservices have to store and load data. If you want to keep this data organized and reliable, you should use a database. Rust has third-party crates that support popular databases, and in this chapter, you'll learn about how to use different databases with Rust, including the following:
  • PostgreSQL
  • MySQL
  • Redis
  • MongoDB
  • DynamoDB
We will create utilities that will allow you to insert or remove data to and from the database, and to query the data held in the database.

Technical requirements

In this chapter, you'll need database instances to run our examples. The most effective way to run and work with a database for testing purposes is to use Docker. You can install databases locally, but seeing as we'll also need Docker for the remaining chapters, it's best to install and use it from this chapter.
We will use the following official images from Docker Hub:
  • postgres:11
  • mysql:8
  • redis:5
  • mongo:4
  • amazon/dynamodb-local:latest
You can get to know more about these images on the Docker Hub repository pages: https://hub.docker.com/.
We will also use the DynamoDB database, which is provided as part of Amazon Web Services: https://aws.amazon.com/dynamodb/.
If you want to interact with databases to check whether our examples work successfully, you'll also have to install the corresponding clients for each database.
You can find all of the examples for this chapter in the Chapter07 folder on GitHub: https://github.com/PacktPublishing/Hands-On-Microservices-with-Rust-2018/.

PostgreSQL

PostgreSQL is a reliable and mature database. In this section, we will explore how to start an instance of this database in a container, and how to connect to it from Rust using third-party crates. We will look at simple interactions with this database, and at the use of connection pools to get extra performance. We will start an instance of the database with Docker and create a tool to add records to a table and to query the list of added records before printing them to a console.

Setting up a test database

To create our database, you can use Docker, which automatically pulls all the necessary layers of the images containing the preinstalled PostgreSQL database. It's important to note that PostgreSQL has official images on Docker Hub, and you should opt to use these instead of unofficial ones, because the latter have a greater risk of malicious updates.
We need to start a container with a PostgreSQL database instance. You can do this using the following command:
docker run -it --rm --name test-pg -p 5432:5432 postgres
What does this command do? It starts a container from the postgres image (the latest version) and uses port 5432 on the localhost to forward it to the inner port, 5432, of the container (that is, the port exposed by the image). We also set a name with the --name argument. We give the container the name test-pg. You can use this name later to stop the container. The --rm flag will remove the anonymous volumes associated with the container when it's stopped. So that we can interact with the database from a Terminal, we've added -it flags.
The database instance will start and print something like the following to the Terminal:
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
........
The database is now ready for use. You can check it with the psql client, if you have it locally. The default parameters of the image are as follows:
psql --host localhost --port 5432 --username postgres
If you don't need the database anymore, you can use the following command to shut it down:
docker stop test-pg
But don't shut it down yet—let's connect to it with Rust.

Simple interaction with a database

The easiest way to interact with a database is to create a single connection directly to the database. Simple interaction is a straightforward database connection that doesn't use connection pools or other abstractions to maximize performance.
To connect to a PostgreSQL database, we can use two crates: postgres or r2d2_postgres. The first is a generic connection driver. The second, r2d2_postgres, is a crate for the r2d2 connection pools crate. We will start by using the postgres crate directly, without a pool from the r2d2 crate, and work on a simple utility to create a table, before adding commands to manipulate the data in that table.

Adding dependencies

Let's create a new project with all the necessary dependencies. We will create a binary utility for managing users in a database. Create a new binary crate:
cargo new --bin users
Next, add the dependencies:
cargo add clap postgres
But wait! Cargo doesn't contain an add command. I've installed cargo-edit tool for managing dependencies. You can do this with the following command:
cargo install cargo-edit
The preceding command installs the cargo-edit tool. If you don't install it, your local cargo won't have an add command. Install the cargo-edit tool and add the postgres dependency. You can also add dependencies manually by editing the Cargo.toml file, but as we are going to create more complex projects, the cargo-edit tool can be used to save us time.
The Cargo tool can be found here: https://github.com/killercup/cargo-edit. This tool contains three useful commands to manage dependencies: add to add a dependency, rm to remove an unnecessary dependency, and upgrade to upgrade versions of dependencies to their latest versions. Furthermore, with the awesome Edition 2018 of Rust, you don't need to use an extern crate ... declaration. You can simply add or remove any crates and all of them will be available immediately in every module. But what about if you add a crate that you don't need, and end up forgetting about it? Since the Rust compiler allows unused crates, you can add the following crate-wide attribute, #![deny(unused_extern_crates)], to your crate. This is necessary in case you accidentally add a crate that you won't use.
Also, add the clap crate. We need it for parsing arguments for our tool. Add the usages of all the necessary types, as follows:
extern crate clap;
extern crate postgres;

use clap::{
crate_authors, crate_description, crate_name, crate_version,
App, AppSettings, Arg, SubCommand,
};
use postgres::{Connection, Error, TlsMode};
All necessary dependencies have been installed, and our types have been imported, so we can create the first connection to a database.

Creating a connection

Before you can execute any query on a database, you have to establish a connection with the database you started in a container. Create a new Connection instance with the following command:
let conn = Connection::connect("postgres://postgres@localhost:5432", TlsMode::None).unwrap();
The created Connection instance has execute and query methods. The first method is used to execute SQL statements; the second is used to query data wi...

Table des matiĂšres