Building javascript microservices with senecajs and compose

Enterprise Pricing Articles Sign in Free 30-Day Trial Building JavaScript Microservices with SenecaJS and Compose Share on Twitter Share on Facebook Share on Google+ Vote on Hacker News Published Jan 12, 2017

Database-backed microservices are powerful and in this article we show how to use SenecaJS, NodeJS and Compose databases to create a virtual product catalog using them. 1 care data recovery software Microservices are making a huge dent in the web development world, with companies like Netflix, Walmart, and IBM embracing microservice architectures for their mission critical applications. Database user interface Introduction to Microservices

First, in case you’ve been hiding in the dungeons of ancient enterprise application development for the past few years, let’s start with a small introduction to microservices. Data recovery jaipur If you’re already comfortable with microservices, you can skip to the next section.


A typical software application consists of a single process that contains all of the instructions you’ll need to execute the application. Database platforms This monolithic architecture allows a process to be quickly and simply executed, but scaling up functionality requires executing multiple copies of the entire process even if you only need to scale up one small piece of functionality (for example, Netflix uses the streaming portion of its application far more than it uses the ‘new user signup’ portion).

One of the challenges that can arise from this “all-in-one” approach is that it’s very difficult to scale your applications. Data recovery group Since monolithic applications are designed to be self-contained, the mechanisms for connecting multiple redundant copies of a monolithic process can be impractical or impossible if the application hasn’t been designed to accommodate such tandem scaling. Database xls Errors in monolithic applications also tend to be catastrophic, requiring a reboot of the entire application and resulting in downtime for end users.

Modern distributed software, such as large web applications, need to be able to scale easily and fail robustly by running processes on multiple machines in multiple locations. C database library Microservices architectures are designed to allow for exactly this type of behavior. 510 k database Each microservice handles a small piece of functionality for the application, and multiple microservices connected together in a network constitutes a complete application. How to become a database administrator Designing applications as a “system of systems” means that developers can add new instances of a microservice to handle the extra load without impacting the existing system. Database xml Developers can also redirect traffic to redundant copies running on different servers in the event of server or application failure.

Microservices aren’t without their faults – while a single microservice might be a small and simple piece of software, the way it interacts with other parts of the application is more complex than a typical monolithic application. Database terminology Frameworks like SenecaJS provide tools to make these interactions easier. Database theory Introduction to SenecaJS

SenecaJS is a NodeJS application framework that allows developers to define microservices and connect them together in different ways. Database 1 to 1 relationship The fundamental unit of communication in SenecaJS is a simple JSON message. Database testing By convention, that message will be an object with at least 2 keys: a role and a cmd. Database graph For example, a message that outputs hello, world might look like the following: { “role”: “hello”, “cmd”: “sayHello” }

SenecaJS looks at the message and compares it to a set of commands that have been registered with it. Database naming conventions When the message matches one of those commands, SenecaJS executes the function associated with that command.

Let’s start with a basic SenecaJS application to see how commands can be registered. Database entity Create a new folder for your project on your system and initialize a new Node module using npm init. Database developer The npm init command will prompt you for options for your new module. Data recovery plan You can use the defaults. Data recovery kansas city Then, use npm install to install SenecaJS. N k database $ mkdir seneca-compose

Now, we’ll create a simple ping application. Data recovery 2016 This application will just respond with a message when we call into it letting us know that the application is Online and working correctly. 510 k database fda // Create a new SenecaJS application

First, we’ll create a new SenecaJS application by calling the “seneca()” function. Database programmer This provides us with a place to register new commands and functionality. Data recovery osx seneca.add({“role”: “compose”, “cmd”: “ping”}, (args, done) => {

Next, we’ll call the seneca.add method. Database integrity seneca.add is used to register a new command with the seneca system, and to define the function that should be executed when that command it sent. Database backup It takes two arguments: a command pattern and the function that executes when the pattern is matched. Hollywood u database The function that executes also takes two arguments: an args object which contains the entire message that was sent (including the command pattern and any extra parameters) and a done function which is executed once the service is ready to send a response. Data recovery ipad The done function itself also takes two arguments in typical node.js fashion: an error as the first argument (or null if there’s no error) and the results of any operations as the second argument. Database vs server seneca.listen({“type”: “http”, “port”: 8080});

Finally, we’ll have the application to listen for connections using the seneca.listen method. Database is in transition Since SenecaJS uses simple JSON messages, there are many different ways you can communicate between microservices (called the transport in SenecaJS lingo). Data recovery ios There are transport plugins for Redis, PubNub, TCP, and RabbitMQ among others. Database data types In our example, we’ll use HTTP which has the added benefit of allowing us to test our microservices using the curl utility. Data recovery johannesburg The HTTP transport exposes a special route, /act, for this purpose and also useful for connecting other applications and external systems (for example, those designed in another programming language) to SenecaJS-designed microservices. Iphone 5 data recovery software If your platform has an HTTP library, it can communicate with a SenecaJS-based Microservice.

To test our microservice, you’ll need to open two terminal windows. Database operations In the first window, you’ll run your SenecaJS microservice. Database index $ node service.js

In the second window, we’ll use the curl utility to send a message to our running service. Database crud $ curl -d ‘{“role”:”compose”,”cmd”:”ping”}’ http://localhost:8080/act

That’s about it! All SenecaJS applications are designed in a similar fashion: use .add to register command patterns and tie that command to a function, and then execute that function by sending a message into SenecaJS matching the pattern. Drupal 8 database Storing Data Using Seneca Entities

Now that you have a feel for creating a microservice in SenecaJS, let’s do something a little more interesting: save data in a database. Data recovery disk SenecaJS by itself is minimally functional, but there is an ecosystem of plugins (many developed by the SenecaJS team itself) that can be used to make SenecaJS far more powerful. Database 3 tier architecture We’ll use a plugin called seneca-entity that makes storing and retrieving data in SenecaJS a snap. Data recovery orlando If you’ve ever used an ORM like ActiveRecord or an ODM like Mongoose, you’ll feel right at home with Seneca entities.

As a demo, we’ll create a catalog for a nerdy clothing store. Database cardinality Our clothing store contains products and we’ll represent these products as entities. Database unit testing To create a new entity, you’ll first need to add the seneca-entity and seneca-basic plugins. I data recovery software free download npm install –save seneca-entity seneca-basic

Then, you can tell SenecaJS to use those plugins with the .use command. O review database If you have experience with NodeJS development with ExpressJS you’ll recognize the syntax: seneca

The seneca-entity plugin decorates our SenecaJS application with a new method, .make$, which we’ll use to create a new entity type. Database in recovery Call seneca.make$ and pass in the name of the entity you’d like to create. Data recovery wizard professional seneca.make$ will return a new instance of that entity that you can now modify as you would any other JavaScript object. Data recovery open source var product = seneca.make$(“Product”);

Now, you can save the new product using the save$ function. Gif database save$ takes a callback with the function signature of (err, savedProduct) where err contains any errors encountered while saving (or null if there are no errors) and savedProduct contains the entity that was persisted to the database. Data recovery lifehacker product.save$((err, savedProduct) => {

Let’s bring this all together with a new command we can add to SenecaJS. Top 10 data recovery software 2014 This new command will add a product to our virtual catalog. Database gale We’ll also demonstrate how to take the name, price, and description for the product from arguments passed into the service. Database life cycle // service.js

You’ll notice that we’ve passed in far more data than we were listening for with the command. Data recovery dallas SenecaJS is only concerned with matching all the parts of it’s command to your message (ie: role: product, cmd: create). Data recovery usb Since those two fields are present in our message, it’s considered a match. Database 4th normal form The extra data is sent into the function in the args object, which we used to send in extra information about our product in the catalog.

Since we sent back the savedProduct as our success response, we should see something like the following (notice the new entity$ and id fields added by the entity system): {

You might be wondering where the entities are currently being saved. V database in oracle By default, SenecaJS provides a memory store plugin that saves the entities using an in-memory database. Data recovery tampa This default behavior allows us to create some entities and test out operations on them during early development. R studio data recovery with crack This is also useful for creating test drivers that can validate entity logic without connecting to a persistent database.

Using this plugin concept, we can now swap out the in-memory database with a plugin that adds a persistent backing data store without having to change any of our application or entity logic. Database uses Storing Entities in Mongo for Compose

Once you’ve created a new database, we’ll use mongo-store to tell SenecaJS to use MongoDB as the persistent backing store for our application. Database history First, we’ll install the seneca-mongo-store node package: npm install –save seneca-mongo-store

Next, we’ll modify our SenecaJS application to use the mongo-store plugin. Database b tree Since SenecaJS plugins are added in the order your add them in your application, we’ll .use the mongo-store plugin after we .use the entity plugin (otherwise the entity plugin will default back to memory store): seneca

The final program looks like the following (notice that the only thing we’ve done from the previous example is to add the extra .use(‘mongo-store’, …) after .use(‘entity’)): // service.js

Our entities should now persist to our new Mongo database. Database optimization To test this out, run your service like before in one terminal, and run the curl command to add a product in the other: $ node service.js

Now, when you navigate to the database browser in Compose, you’ll see a new collection in your database called “Product” and a new item in that collection:

If you’ve been following along, you’ll notice that it’s the same basic structure as the mongo-store configuration above. Data recovery software reviews The final service looks like this: // service.js

Writing microservice applications in JavaScript doesn’t have to be a hassle. Cnet data recovery With SenecaJS and Compose, you can quickly spin up new server-side applications and swap out databases as your needs change. Database systems SenecaJS also allows you to use multiple transport mechanisms, and in a future article, we’ll discuss how to use RabbitMQ and Redis on Compose to create stable and secure distributed microservices applications.

If you have bits you think should be in NewsBits, or any feedback about any Compose articles, drop the Compose Articles team a line at articles@compose.com. Data recovery for mac We’re happy to hear from you.

banner