35,99 €
Build enterprise-grade microservice ecosystems with intensive case studies using C#
Key FeaturesLearn to build message-based microservicesPacked with case studies to explain the intricacies of large-scale microservicesBuild scalable, modular, and robust architectures with C#Book Description
C# is a powerful language when it comes to building applications and software architecture using rich libraries and tools such as .NET.
This book will harness the strength of C# in developing microservices architectures and applications.
This book shows developers how to develop an enterprise-grade, event-driven, asynchronous, message-based microservice framework using C#, .NET, and various open source tools. We will discuss how to send and receive messages, how to design many types of microservice that are truly usable in a corporate environment. We will also dissect each case and explain the code, best practices, pros and cons, and more.
Through our journey, we will use many open source tools, and create file monitors, a machine learning microservice, a quantitative financial microservice that can handle bonds and credit default swaps, a deployment microservice to show you how to better manage your deployments, and memory, health status, and other microservices. By the end of this book, you will have a complete microservice ecosystem you can place into production or customize in no time.
What you will learnExplore different open source tools within the context of designing microservicesLearn to provide insulation to exception-prone function callsBuild common messages used between microservices for communicationLearn to create a microservice using our base class and interfaceDesign a quantitative financial machine microserviceLearn to design a microservice that is capable of using Blockchain technology Who this book is for
C# developers, software architects, and professionals who want to master the art of designing the microservice architecture that is scalable based on environment. Developers should have a basic understanding of.NET application development using C# and Visual Studio
Matt R. Cole is a seasoned developer with 30 years' experience in Microsoft Windows, C, C++, C#, and .NET. He previously wrote a speech and audio VOIP system for NASA for use with the Space Shuttle and a space station. He is the owner of Evolved AI Solutions, a premier provider of advanced ML/Bio-AI technologies. He developed the first enterprise-grade microservice framework (written fully in C# and .NET) used by a major hedge fund in NYC and also developed the first Bio-AI Swarm framework, which fully integrates mirror and canonical neurons.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 208
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Pavan RamchandaniAcquisition Editor: Aman SinghContent Development Editor: Mayur PawanikarTechnical Editor: Suwarna PatilCopy Editor: Safis EditingProject Coordinator: Nidhi JoshiProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics: Jason MonterioProduction Coordinator: Nilesh Mohite
First published: June 2018
Production reference: 1290618
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78953-368-2
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and, as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Matt R. Cole is a seasoned developer with 30 years' experience in Microsoft Windows, C, C++, C#, and .NET. He previously wrote a speech and audio VOIP system for NASA for use with the Space Shuttle and a space station. He is the owner of Evolved AI Solutions, a premier provider of advanced ML/Bio-AI technologies. He developed the first enterprise-grade microservice framework (written fully in C# and .NET) used by a major hedge fund in NYC and also developed the first Bio-AI Swarm framework, which fully integrates mirror and canonical neurons.
Giuseppe Ciaburro holds a PhD in environmental technical physics and two master's degrees. His research is on machine learning applications in the study of urban sound environments. He works at the Built Environment Control Laboratory, Università degli Studi della Campania Luigi Vanvitelli (Italy). He has over 15 years' experience in programming Python, R, and MATLAB, first in the field of combustion, and then in acoustics and noise control. He has several publications to his credit.
Doug Ortiz is an experienced enterprise Cloud, big data, data analytics, and solutions architect who has designed, developed, re-engineered, and integrated enterprise solutions. His other expertise is in Amazon Web Services, Azure, Google Cloud, business intelligence, Hadoop, Spark, NoSQL databases, and SharePoint, to mention a few.
He is the founder of Illustris, LLC, and can be reached at [email protected].
Rich Pizzo has many years of experience in the design and development of software and systems. He was a senior architect and project lead, especially in the realm of financial engineering and trading systems. He was the chief technologist at two companies. His knowledge and expertise in digital electronics left its mark in the software domain, as well in providing heterogeneous solutions to tough optimization problems. He has come up with many unique solutions for maximizing computing performance, utilizing Altera FPGAs and the Quartus development environment and test suite.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Hands-On Microservices with C#
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Let's Talk Microservices, Messages, and Tools
What is a microservice?
Service-Oriented Architecture
Microservice architecture
Pros and cons
Pros
Cons
Case study
Messaging concepts
Message queues
Producer consumer queue
Creating common messages
Message subscriptions
The subscription ID that you pass to subscribe is important
Considerations when writing the subscribe callback delegate
Using SubscribeAsync
Canceling subscriptions
Versioning messages
How does message versioning work?
Message versioning guidance
Message publishing
Message flow
Exchanges
Direct exchange
Default exchange
Topic exchange
Fanout exchange
Headers exchange
Common messages
Summary
ReflectInsight – Microservice Logging Redefined
Router
Log Viewer
Live Viewer
Message navigation
Message properties
Watches
Bookmarks
Call Stack
Advanced Search
Quick Search
Advanced Search
Time zone formatting
Auto Save/Purge
Auto Save
Auto Purge
Configuration Editor
Overview
XML configuration
Dynamic configuration
Configuration Editor
Summary
Exercises
Creating a Base Microservice and Interface
Classes versus interfaces
Creating our base project
Creating a base interface
Creating our base microservice class
Variables
Base microservice constructor
Implementing our interface
Publishing a memory update message
Minimal microservice
Summary
Designing a Memory Management Microservice
Creating our microservice
Our code
Reclaiming memory
Dynamically creating an exchange
Dynamically creating a queue
Publishing a message
Summary
Exercises
Designing a Deployment Monitor Microservice
Deployment issues
Installation
Our messages
Deployment start message
Deployment stop message
Our program
Subscribing to messages
Processing messages
Publishing health status messages
Summary
Exercises
Designing a Scheduling Microservice
Installation
Installing Quartz.NET
What is Quartz.NET?
Runtime environments
Job scheduling
Job execution
Job persistence
Clustering
Listeners and plugins
Installing Quartz.NET server as a service
Our program
OnStart
Scheduling multiple jobs
Running our microservice
Summary
Exercises
Designing an Email Microservice
Installation
Our program
OnStart
Subscribing to messages
Processing messages
Finding DNS servers
Subscribing to email requests
Summary
Exercises
Designing a File Monitoring Microservice
Overview of FileSystemWatcher
Internal buffer
Building your microservice – part one
Making sure our message queue and exchange are created
Running the recovery filewatcher
Building your microservice – part two
Our message
Building our main program
Notes on events
Summary
Exercises
Creating a Machine Learning Microservice
Installation
Our program
OnStart
Processing a machine learning request
Creating a layer
Creating a network
Traversing the forward path through the network
Training the network
Getting results
Summary
Creating a Quantitative Financial Microservice
Installation
Our messages
CDS-request message
Bonds request message
Subscribing to messages
Publishing our request message
Publishing our CDS response
Calculating a CDS
Getting bond information
Processing a bond request
Processing a bond response
Summary
Exercises
Trello Microservice – Board Status Updating
Installation
Creating boards, lists, and cards
Summary
Exercises
Microservice Manager – The Nexus
Installation
Our code
Leader election
Subscribing to all messages
Processing bond response messages
Processing deployment messages
Processing CDS messages
Processing memory messages
Processing health status messages
Processing machine learning messages
Processing filesystem messages
Summary
Exercises
Creating a Blockchain Bitcoin Microservice
Blockchain
Bitcoin
Digital signatures
The circle of encryption
Installation
Our main program
Leader election
Creating our digital wallet
Your public address
How do we spend money?
Our code
Processing BitcoinSpendReceipt
Summary
Exercises
Adding Speech and Search to Your Microservice
Text-to-Speech
Installation
Our program
Our messages
Subscribing to messages
Processing speech request messages
Processing Wikipedia search messages
Using Text-to-Speech
Summary
Best Practices
Microservices
Messaging
Scheduling jobs
Recommended reading
Welcome to Hands-On Microservices with C#. In this book, we will develop a microservice ecosystem that you can immediately put in place in your organization to experience the many benefits of a microservice architecture. No advanced math, no formulas, just straight hands-on development (save a bit of introductory material).
This book is for the developer learning to see a different implementation of microservices than the typical web page or Hello World example. In this book, we will build a real-world microservice ecosystem similar to those I have built for large enterprises. You will have the flexibility to customize every aspect of the process if you so desire.
It is expected that the developer has developed applications before, but no familiarity with microservice architecture is assumed or required. If you have 1-3 years of .NET C# development experience, this book will be a perfect fit for you.
I should note that the microservices within this book, like the book itself, are designed to make your mind open to the many possibilities of a microservice architecture. You can transform any of the microservices into what fits your organization. In fact, this is the goal, that the creative juices will flow and you will turn this ecosystem into something that works best for you.
Chapter 1, Let's Talk Microservices, Messages, and Tools, covers all of our introductory material required for the rest of the book.
Chapter 2, ReflectInsight – Microservice Logging Redefined, gives a brief overview of ReflectInsight and the rich, powerful logging capabilities it has to offer.
Chapter 3, Creating a Base Microservice and Interface, creates a base class and interface we can use throughout the book and all of our microservices. We also discuss base classes, interfaces, and inheritance for those who need a quick refresher.
Chapter 4, Designing a Memory Management Microservice, develops a memory management microservice capable of tracing and reporting memory requirements. High CPU, too much RAM, garbage collection, all will be covered in the design. We will also cover why some of these things are important to know in a microservice ecosystem.
Chapter 5, Designing a Deployment Monitor Microservice, helps us develop a microservice capable of knowing about and monitoring deployments.
Chapter 6, Designing a Scheduling Microservice, develops a microservice that can execute scheduled jobs. While scheduling information is built within this microservice, the reader can open it up to use the messages we have shown thus far and drive the scheduled jobs from there.
Chapter 7, Designing an Email Microservice, covers a microservice that can send emails. Exercises are left for the user to expand this microservice to do even more. Separation of concerns is discussed in this chapter as well.
Chapter 8, Designing a File Monitoring Microservice, is where we make two microservices that are capable of listening to filesystem events and then posting those events via our messages. This is our first microservice that does not respond to a message but rather sends one when it needs to. The reader is free to expand this microservice to denote the hyperparameters needed to programmatically drive the file monitoring.
Chapter 9, Creating a Machine Learning Microservice, implements a CNN contained within a microservice. We will show how to control it asynchronously via messages.
Chapter 10, Creating a Quantitative Financial Microservice, develops a quant microservice capable of working with credit default swaps and bonds. This chapter is an exercise for the user to be able to adapt the microservice to their specific needs.
Chapter 11, Trello Microservice – Board Status Updating, works with the famous and intuituive Trello service, a Kanban board that is incredibly easy to use. We will show you how the microservice can programmatically add boards, lists, and cards. This can be adapted to Team Foundation Service, Jira, or an other Kanban board application that has a programmatical interface.
Chapter 12, Microservice Manager – The Nexus, develops a microservice whose job is to manage all the other microservices in the ecosystem. You will see how important it is to have such a microservice in your ecosystem, from health monitoring to job scheduling and more.
Chapter 13, Creating a Blockchain Bitcoin Microservice, implements a small blockchain microservice, and we will spend some of our money here. Cryptocurrency knowledge will not be covered, but it is hoped that again the creative juices will flow.
Chapter 14, Adding Speech and Search to Your Microservice, shows you how to add text-to-speech to your application. We will do this both in terms of speaking text, and also searching Wikipedia for results and then speaking those results aloud.
Appendix A, Best Practices, discusses best practices for designing and using your Microservice ecosystem.
The reader should be a C# .NET developer with at least 1 year of developing applications. The reader should also be familiar with Microsoft Visual Studio.
The reader should have the latest .NET framework installed on their machine as well as the latest version of Microsoft Visual Studio. The community version of Microsoft Visual Studio will accomplish both.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Hands-On-Microservices-with-CSharp. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/HandsOnMicroserviceswithCSharp_ColorImages.pdf.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."
A block of code is set as follows:
public class DeploymentStartMessage{public DateTime Date { get; set; }}public class DeploymentStopMessage{public DateTime Date { get; set; }}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
bus.Subscribe<MyMessage>("my_subscription_id", msg =>Console.WriteLine(msg.Text));
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "A binding is a link that you set up to bind a queue to an exchange."
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
Microservices are all the rage. They are talked about everywhere, and it seems like everyone wants them nowadays. There are probably as many implementations of them as there are words in this paragraph, and we'll add yet another one into the mix. But this comes from several implementations and years of experience developing enterprise grade microservice ecosystems for big clients. Now, I'm letting you in on the same techniques and best practices I've been using in the real world. And thus, you have the logic behind this book. I'm going to show you how to develop a powerful, flexible, and scalable microservice ecosystem, and hopefully along the way spark ideas for you to go off on your own endeavors and create even more. And we're not talking about some skimpy little web page or a single service; I've packed this book full of more microservices than you can shake a stick at, and I am sure your ideas will take shape and you will enhance this ecosystem to meet your needs.
In this chapter, we will cover:
What a microservice is
What a microservice architecture is
Pros and cons of a microservice
Installing and an overview of Topshelf
Installing and an overview of RabbitMQ
Installing and an overview of EasyNetQ
Installing and an overview of Autofac
Installing and an overview of Quartz
Installing and an overview of Noda Time
Ok, let's just go ahead and get this one out of the way. Let's start this book off by talking a bit about exactly what a microservice is, to us at least. Let's start with a simplistic visual diagram of what we're going to accomplish in this book. This diagram says it all, and if this looks too confusing, this might be a good place to stop reading!
Let's next agree to define a microservice as an independently deployable and developable, small, modular service that addresses a specific and unique business process or problem, and communicates via a lightweight event-based, asynchronous, message-based architecture. A lot of words in that one I know, but I promise by this end of the book that the approach will make perfect sense to you. Basically, what we are talking about here is the Messages central component in the previous diagram.
I know that some of you might be asking yourselves, what's the difference between a service and a microservice? That is one very good question. Lord knows I've had some very heated discussions from non-believers over the years, and no doubt you might as well. So, let's talk a bit about what a Service-Oriented Architecture (SOA) is.
The SOA is a software design paradigm where services are the central focus. For the purposes of discussion and clarity, let's define a service as a discrete unit of functionality that can be accessed remotely and acted upon independently. The characteristics of a service in terms of a SOA are:
It represents a specific business function or purpose (hopefully)
It is self-contained
It can and should function as a black box
It may also be comprised of other associated services
There is a hard and dedicated contract for each service (usually)
Some folks like to consider a microservice nothing more than a more formalized and refined version of an SOA. Perhaps in some ways, that could be the case. Many people believe that the SOA just never really formalized, and microservices are the missing formality. And although I am sure an argument could be made for that being true, microservices are usually designed differently, with a response-actor paradigm, and they usually use smaller or siloed databases (when permissible), and smaller and faster messaging protocols versus things like a giant Enterprise Service Bus (ESB).
Let's take a moment and talk about the microservice architecture itself.
Just as there is no one set definition for a microservice, there is also not one set architecture. What we will do is make a list of some of the characteristics that we view a microservice architecture to have. That list would then look something like this:
Each microservice can be deployed, developed, maintained, and then redeployed independently.
Each microservice focuses on a specific business purpose and goal and is non-monolithic.
Each microservice receives requests, processes them, and then may or may not send a response.
Microservices practice decentralized governance and in some cases, when permissible, decentralized data management.
Perhaps most importantly, at least in my mind anyways, I always design a microservice around failure. In fact, they are designed to fail. By following this paradigm, you will always be able to handle failures gracefully and not allow one failing microservice to negatively impact the entire ecosystem. By negatively impact, I mean a state where all other microservices are throwing exceptions due to the one errant microservice. Every microservice needs to be able to gracefully handle not being able to complete its task.
Finally, let's stay flexible and state that our microservice architecture is free to remain fluid and evolutionary.
No microservice talks directly to another microservice. Communication is always done in the form of messages.
