Hands-On Microservices with Rust | Web Development | Paperback (original) (raw)

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

Subscription Free Trial Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped

Product feature icon Paperback book shipped to your preferred address

Product feature icon Redeem a companion digital copy on all Print orders

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

OR

Contact Details

Modal Close icon

Payment Processing...

tick Completed

Shipping Address

Billing is same as shipping information

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

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:

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

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

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.

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,...

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.

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...

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.

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...

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.

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...

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

Page1 of 7

Right arrow icon

Download code icon Download Code

Key benefits

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

Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95

(Includes tracking information)

Publication date : Jan 31, 2019

Length: 520 pages

Edition : 1st

Language : English

ISBN-13 : 9781789342758

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped

Product feature icon Paperback book shipped to your preferred address

Product feature icon Redeem a companion digital copy on all Print orders

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

OR

Contact Details

Modal Close icon

Payment Processing...

tick Completed

Shipping Address

Billing is same as shipping information

Billing Address

Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95

(Includes tracking information)

Publication date : Jan 31, 2019

Length: 520 pages

Edition : 1st

Language : English

ISBN-13 : 9781789342758