Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Hands-On Microservices with Rust
Hands-On Microservices with Rust

Hands-On Microservices with Rust: Build, test, and deploy scalable and reactive microservices with Rust 2018

eBook
$24.99 $35.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Microservices with Rust

Developing a Microservice with the Hyper Crate

This chapter will provide a short introduction to creating microservices using Rust the and hyper crate. We will look at the basics of the HTTP protocol and the principles of routing. We'll also describe a minimal REST service written completely with Rust, using a simple method.

In this chapter, we'll cover the following topics:

  • Using hyper
  • Handling HTTP requests
  • Using regular expressions for routing
  • Getting parameters from the environment

Technical requirements

Because we're starting to write code in this chapter, you'll need to have certain software in order to compile and run examples:

  • I recommend you use the rustup tool, which will keep your Rust instance up to date. If you don't have this tool, you can get it from https://rustup.rs/. When it's installed, run the rustup update command to update the current installation.
  • The Rust compiler, at least version 1.31.
  • The hyper crate, which we'll use to compile the code, requires the OpenSSL (https://www.openssl.org/) library. The most popular operating systems already include the OpenSSL package and you can follow the manual of your package manager to install it.

You can get the examples shown in this chapter from GitHub at https://github.com/PacktPublishing/Hands-On-Microservices-with-Rust/tree/master/Chapter02...

Binding a Tiny Server

In this section, we'll create a Tiny Server from scratch. We'll start with the necessary dependencies, declare a main function, and then try to build and run it.

Adding necessary dependencies

First, we need to create a new folder where we'll add the necessary dependencies to create our first microservice. Use cargo to make a new project called hyper-microservice:

> cargo new hyper-microservice

Open the created folder and add dependencies to your Cargo.toml file:

[dependencies]
hyper = "0.12"

The single dependency is the hyper crate. The latest release of this crate is asynchronous and lies on top of the futures crate. It also uses the tokio crate for runtime,...

Handling incoming requests

We've created a server, but it isn't very useful until it can respond to real requests. In this section, we'll add handlers to the requests and use the principles of REST.

Adding a service function

In the previous section, we implemented simple services based on service_fn_ok functions, which expect the service function not to throw any errors. There are also service_fn functions, which can be used to create handlers that can return an error. These are more suitable for asynchronous Future results. As we saw previously, the Future trait has two associated types: one for a successful result and one for an error. The service_fn function expects the result to be converted...

Implementing the REST principles

If everyone were to create rules of interaction with microservices from scratch, we'd have an excess of private standards of intercommunication. REST isn't a strict set of rules, but it's an architectural style intended to make interacting with microservices simple. It provides a suggested set of HTTP methods to create, read, update, and delete data; and perform actions. We'll add methods to our service and fit them to REST principles.

Adding a shared state

You may have already heard that shared data is a bad thing and a potential cause of bottlenecks, if it has to be changed from separate threads. However, shared data can be useful if we want to share the address of...

Routing advanced requests

In the preceding example, we used pattern matching to detect the destination of a request. This isn't a flexible technique, because the path often contains extra characters that have to be taken into account. The /user/1/ path, for example, contains the trailing slash, / , which can't be parsed with a user ID in the previous version of our microservice. There's a flexible tool to fix this issue: regular expressions.

Defining paths with regular expressions

A regular expression is a sequence of characters that express a pattern to be searched for in a string. Regular expressions provide you with the ability to create tiny parsers that split a text into parts using...

Summary

In this chapter, we created a microservice using a hyper crate. We started with a minimal example that only responds with the Rust Microservice message. Then, we created a microservice that has two distinct paths  the first being the index page request and the second, the NOT_FOUND response.

Once we learned the basics, we then started to use the match expression to make the microservice REST-compliant. We also added the ability to handle users' data with four basic operations—create, read, update, and delete.

To expand the routing capabilities in the last example of the chapter, we implemented routing based on regular expressions. Regular expressions are compact patterns that check and extract data from a text.

In this chapter, we encountered various crates—hyper, futures, slab, regex, and lazy_static. We&apos...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 31, 2019
Length: 520 pages
Edition : 1st
Language : English
ISBN-13 : 9781789341980
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning

Product Details

Publication date : Jan 31, 2019
Length: 520 pages
Edition : 1st
Language : English
ISBN-13 : 9781789341980
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 147.97
Mastering Rust
$54.99
Hands-On Microservices with Rust
$48.99
Hands-On Data Structures and Algorithms with Rust
$43.99
Total $ 147.97 Stars icon

Table of Contents

18 Chapters
Introduction to Microservices Chevron down icon Chevron up icon
Developing a Microservice with the Hyper Crate Chevron down icon Chevron up icon
Logging and Configuring Microservice Chevron down icon Chevron up icon
Data Serialization and Deserialization with the Serde Crate Chevron down icon Chevron up icon
Understanding Asynchronous Operations with Futures Crate Chevron down icon Chevron up icon
Reactive Microservices - Increasing Capacity and Performance Chevron down icon Chevron up icon
Reliable Integration with Databases Chevron down icon Chevron up icon
Interaction to Database with Object-Relational Mapping Chevron down icon Chevron up icon
Simple REST Definition and Request Routing with Frameworks Chevron down icon Chevron up icon
Background Tasks and Thread Pools in Microservices Chevron down icon Chevron up icon
Involving Concurrency with Actors and the Actix Crate Chevron down icon Chevron up icon
Scalable Microservices Architecture Chevron down icon Chevron up icon
Testing and Debugging Rust Microservices Chevron down icon Chevron up icon
Optimization of Microservices Chevron down icon Chevron up icon
Packing Servers to Containers Chevron down icon Chevron up icon
DevOps of Rust Microservices - Continuous Integration and Delivery Chevron down icon Chevron up icon
Bounded Microservices with AWS Lambda Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(9 Ratings)
5 star 66.7%
4 star 11.1%
3 star 0%
2 star 11.1%
1 star 11.1%
Filter icon Filter
Top Reviews

Filter reviews by




sunil Oct 05, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
i recommend this book for every rust developer.
Amazon Verified review Amazon
Denis Nushtaev Jun 17, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Rust for Rust
Amazon Verified review Amazon
Ivan Chepurnyi Apr 24, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am usually hesitating to buy a book before reading a lot of reviews, but this time tutorials on async programming in Rust are very cryptic and hard to wrap your head around. I found this book by googling some topics it covers. This book really enlightened me on the implementation of futures and streams and gave me a solid foundation to start writing my own streams.
Amazon Verified review Amazon
Marco Jul 21, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book covers many different crates that can be used when building microservices in Rust, clearly and with good examples. Given the nature of these repositories (young and not stable yet), it may become outdated soon.
Amazon Verified review Amazon
DaS Dec 12, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great combination of general overview and examples you can follow
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.