Saturday, May 22, 2021

Monolithic vs Microservices

👉    Monolithic Architecture.

*    Monolithic architecture was there before Microservices came into action. 

*    All functionalities or services are implemented or deployed into a single server or the software application.

*    For example lets consider a shopping platform.


👉    Characteristics of Monolithic Architecture.

*    Developed and deployed as a single unit.

*    Overwhelmingly complex.

*    To update a service need to redeploy the entire server/application.

👉    Problems in Monolithic Architecture.

*    Complexity of the architecture.

*    Unable to scale the server/application.

*    Non-reliable.

👉    Messaging in Microservice Architecture.

*    Function calls/language level methods.

*    SOAP and WS with HTTP, JMS.

👉    Microservice Architecture.

*    Developing a single application as a suite of fine-grained and independent services which runs on its own process, developed and deployed independently.

*    For example consider the same shopping platform application.



👉    Characteristics of Microservice Architecture.

*    Single responsibility.

*    Independent development and deployment.

*    Focus on the scope of microservice.

👉    Messaging in Microservice Architecture.

*    Simple and lightweight messaging mechanism.

*    Both synchronous and asynchronous messaging.

*    Message formats are JSON, XML.

*    Service contracts like Swagger, RAML, Thrift.

👉    Integrating Microservice Architecture.

*    SOA/web services use ESB

*    But in microservices it is different.

      1.    Point to Point style.


      2.    API Gateway Style.

      3.    Message Broker.


👉    Governance in Microservice Architecture.

*    No centralized governance in microservices.

*    Make their own decisions about design and implementation.

👉    Service Registry in Microservice Architecture.

*    Holds the microservice instances and their locations.

👉    Service Discovery in Microservice Architecture.

*    Find the available microservices and their corresponding locations.

*    There are two types:
    1.    Client side discovery.
    2.    Server side discovery.

👉    Deployment in Microservice Architecture.

*    Ability to deploy/undeployed independent of other microservices.

*    Example : Docker.

👉    Security in Microservice Architecture.

*    Security component implemented at each microservice level.

*    OAuth : Access token with no user identification information.

*    OpenID connect : ID token with user infromation.

*    Microservices do  not support transactions due to complexity.


Sunday, May 9, 2021

Creation of the backend with REST API and MongoDB in the MERN Project

 👉    Hope you have read and tried out connecting mongoDB with your application through the steps available in my previous blog post.

 👉    Today lets move one step forward and create the backend services [without the frontend UI] and execute them through postman.

 👉    Lets follow the same procedure of listing the steps in point form which is easy to catch-up things.

 👉     Pre-conditions to start the project.

1.    VS code installed in the local machine.

2.    Node JS installed in the local machine.

3.    Installed MongoDB with MongoDB Compass into the local machine.

4.    An instance of MongoDB running in the local machine.

5.    Have a cluster created in the MongoDB Atlas with the current IP address active in it [Refer the previous post to set the current IP address in the network access in cluster.

 👉     Now lets get started.


🔑    Step 01 - Setting up the folder.

*    Create a folder in the desktop or any subdirectories.
*    Open the folder in VS code.
*    Create another folder called 'backend' below the main folder using command 'mkdir backend'.

🔑    Step 02 - Installation of necessary packages and libraries.

*    Open a new terminal.
*    Run the command 'npm init' and create a init project.
*    Then run the command 'npm install express mongoose'. This command installs express and mongoose into the init project.
*    Then run the command 'npm install nodemon'. This command install nodemon which is useful to auto-save the changes to the server without stopping and restarting it.
*    After installing the nodemon navigate to 'package.json' and edit the 'scripts' tag as following.

"scripts": {
    "start""node app.js",
    "devStart""nodemon app.js"
  }

*    Finally install cors in order to handle JSON inputs and outputs by issuing the command 'npm install cors'.
*    After installing all these packages and libraries the 'package.json' file looks as shown below.

{
  "name""server",
  "version""1.0.0",
  "description""",
  "main""index.js",
  "scripts": {
    "start""node app.js",
    "devStart""nodemon app.js"
  },
  "author""",
  "license""ISC",
  "dependencies": {
    "cors""^2.8.5",
    "express""^4.17.1",
    "mongoose""^5.12.7",
    "nodemon""^2.0.7"
  }
}

🔑    Step 03 - Creating the schema.

*    Now lets create a schema (table) in MongoDB named as 'Student'.
*    First, create a folder inside the main folder named 'model'.
*    Create a file named 'Student.js' inside the model folder.
*    Type the following code inside the Student.js file.

const mongoose = require('mongoose');

const StudentModel = mongoose.Schema({
    studentName: {
        type: String,
        required: true,
    },
    studentFaculty: {
        type: String,
        required: true,
    },
});

const Student = mongoose.model("Student",StudentModel);
module.exports = Student;

*    This code creates a schema names 'Student' inside the DB created inside the cluster.
*    The Student schema has 'studentName' and 'studentFaculty' as the columns.

🔑    Step 04 - Connecting to MongoDB cluster.

*    Now create a file named 'index,js' inside the 'backend' folder.
*    Type the following command to connect to the database.

onst express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();

app.use(express.json());
app.use(cors());

/**The Database connection */
mongoose.connect("<Link of MongoDB cluster for connecting with the application>", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: true,
});

app.listen(3001,() => {
    console.log("Server is started and running on 3001");
});
*

*    These are the main steps involved in setting up the backend. Now lets perform HTTP requests (GET, POST, PUT and DELETE).

🔑    Step 04 - POST Method - Adding a student.

*    The following code shows the import the schema to 'index.js' file.

/**Importing the model */
const StudentModel = require('./model/Student');

*    Type the following code to perform the POST functionality.

/**Post - Add Student  */
app.post("/insertStudent"async (reqres=> {
    /**Provide same name as per the column name */
    const name = req.body.studentName;
    const faculty = req.body.studentFaculty;

    const std = new StudentModel({
        studentName: name,
        studentFaculty: faculty,
    });

    try{
        await std.save();
        res.send("Inserted Data!!");
        console.log("Data inserted successfully!!!");
    }catch(err){
        console.log(err);
    }
});

🔑    Step 05 -GET Method - Getting student details.

*    The following code can be used to get the student details.

app.get("/readAllStudents"async (reqres=> {
    StudentModel.find({}, (error,result=> {
        if(error){
            res.send(error);
        }

        res.send(result)
    })
});

🔑    Step 05 -GET Method - Getting particular student detail by id.

//Read student details by ID
app.get("/readStudentById/:id"async (reqres=> {
    const id = req.params.id;

    StudentModel.find({_id:id}, (error,result=> {
        if(error){
            res.send(error);
        }

        res.send(result)
    })
});

🔑    Step 06 - PUT Method - Update particular student detail by id.

//Update the workshop details
app.put("/updateStudent"async (reqres=> {
    const studentNamereq.body.studentName;
    const studentFacultyreq.body.studentFaculty;
    const id = req.body.id;

    try{
        await StudentModel.findById(id, (errupdatedStudentObject=> {
            updatedStudentObject.studentNamestudentName;
            updatedStudentObject.studentFacultystudentFaculty;

            updatedStudentObject.save();
            res.send("Student detail Updated Successfully");
        });
    }catch(err){
        console.log(err);
    }
});


🔑    Step 06 - DELETE Method - delete particular student detail by id.

/**Delete the student details from  the student Schema for given student ID */
app.delete("/deleteStudent/:id"async (reqres=> {
    const id = req.params.id;

    await StudentModel.findByIdAndRemove(id).exec();
    res.send("Deleted Successfully!!!");
    console.log("Deleted Successfully!!!!");
});

*    After writing all the above code run the command 'npm run devStart' in the terminal.
*    You should see the following output in the terminal.

> server@1.0.0 devStart C:\Users\HP\Desktop\Assignments\DS Assignments\Services\Product Service\server
> nodemon app.js

[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json  
[nodemon] starting `node app.js index.js`   
Server is started and running on 3001

🔑    Step 07 - run in postman.

*    POST Method.

HTTP Method : POST
URL : https://localhost:3001/insertStudent
Body : 
{
    "studentName": "Raj",
    "studentFaculty": "IT"
}

*    GET Method

HTTP Method : POST
URL : https://localhost:3001/readAllStudents

*    GET Method - By id

HTTP Method : GET
URL : https://localhost:3001/readStudentById/<id>

*    PUT Method.

HTTP Method : PUT
URL : https://localhost:3001/updateStudent
Body : 
{
    "studentFaculty": "SE"
}

  DELETE Method - By id

HTTP Method : DELETE
URL : https://localhost:3001/deleteStudent/<id>

😊    These are the main steps involved in creating the backend with REST API and MongoDB. Hope this blog helped you to get an overview about setting up the backend in a MERN project.

[Stay tuned with the blog posts for further updates in the MERN project]. 

Sunday, May 2, 2021

Introduction to Cloud Computing

👉    The Evolution of cloud computing. 

1.    Standalone Systems 

       - The systems which are installed in the local machine to acquire the service.
       - This is in contrast with grid-computing and work remotely.
       - This software applications are not connected to the network and has the ability to operate when offline.


2.    Client - Server.

       - The software application is installed in the server.
       - Clients access the server remotely.
       - In early stage thin clients were used but with time thick clients were introduced.


3.    Web-applications.

       - Access the applications through internet to acquire the service.
       - Web clients can be thin or either thick.
       - The applications are accessed through web browser which requires an active network connection.

4.    Super Computing.

       - This is a computing process which refers to processing of massively complex or data-laden problems using the concentrated computer resources of multiple computer systems working in parallel.
       - This type is mainly used in computation heavy tasks.
    
5.    Cluster Computing.

       - This is a process where many servers are grouped to obtain one fully functional server.
       - This helps to obtain high performance and availability.
       - The servers in the cluster are linked through a LAN network.




6.    Grid Computing.

       - Combining different cluster in different locations to obtain a grid structure.
       - This is used widely used in scientific processes.
       - High performance and availability is ensured by grid computing


👉    Cloud computing. 

*    Cloud computing is an internet process which is the illusion of infinite computer resources, elimination of up-front commitment by cloud users and pay for the use as needed. [Berkeley RADLabs]

*    Cloud computing is a utility service which gives access to tech resources by an expert/experts which is available on demand.




👉    Key ideas of Cloud computing.

*    Pay as you go on. - Pay for the amount used in the cloud services.

*    Self-service interface - The interface for cloud users for monitoring and metering of cloud connection.

*    Auto-scaling    -    Allocating the resource dynamically without under-provisioning or over-provisioning.


👉    Enabling technologies of Cloud computing.

*    Virtual machines.
*    Virtual Storage.
*    Load balancing.
*    Auto-scaling.
*    Monitoring & metering.

👉    Examples of Cloud computing.

*    Amazon EC2.
*    Microsoft Azure.

👉    Virtualization in Cloud computing.

*    Virtual Machine. - Run multiple machines in one machine.

*    Virtual Storage  -  logical space to store data.

*    Virtual Network    -    SDN (Software Defined Network).


👉    Auto-scaling in Cloud computing.

*    Allocating of resources according to the need of the cloud user to avoid under-provisioning and over-provisioning.


👉    Types of cloud - based on structure.

*    Public clouds    -    Open to general public, multi-tenant, no operational or capital cost.

*    Private clouds    -    Restricted only to the owner, solo-tenant, high operational and capital cost.

*    Hybrid clouds    -    This is kind of private cloud but in case need more resource move to public cloud to store or process some insensitive data.

*    Community clouds    -    This is restricted for a specific community like the university students.



👉    Types of cloud - based on service.

*    Iaas (Infra-structure as a service)    -    Create a cloud infrastructure like the virtual machine, disk space, memory , etc.

*    Paas (Platform as a service)    -    Installing a service or application in the cloud platform and obtaining the service. Eg. Tomcat Server

*    Saas (Software as a Service)    -    Using the software in cloud to obtain the service. 
Eg. Microsoft 365.    



Sunday, April 25, 2021

Connecting the MERN project to mongoose - Backend Connection Establishment

 👍    MongoDB     <>   VS code project  ---  Connection Establishment

👉    In this blog lets go across the steps involved in connecting the VS code project to the mongo DB.

👉    Before starting the process make sure you have:

           1. VS Code.

           2. Mongo DB instance running in the local machine.

           3. Signed into the MongoDB Atlas

               [If you haven't signed in use the following link and sign in for free]

               https://www.mongodb.com/cloud/atlas/efficiency?utm_source=google&utm_campaign=gs_apac_sri_lanka_search_core_brand_atlas_desktop&utm_term=mongodb%20atlas&utm_medium=cpc_paid_search&utm_ad=e&utm_ad_campaign_id=12212624368&gclid=Cj0KCQjwppSEBhCGARIsANIs4p7CpEcFIqqnN-57Y3VyLCzNZa5rOsCISYZPPA0GDO1ha-MPTxPgTmYaAsbuEALw_wcB  

        4.  Have node JS installed in the machine.

        5.  MongoDB Compass.

🔆    Make sure you have these materials installed and signed up before you start the project. In case we need to install any other package or sign into other application, stay focused throughout the process to grab them

 👉     STEP 01 - Setting VS Code environment.

*    Create a folder in the project directory and name it as 'server'.

*    Then navigate to the server folder in the VS code terminal and type 'npm init'.

*    A init project will be created in the server folder.

*    Then install ExpressJS and mongoose by issuing the command: 'npm install express mongoose'

*    Then create the 'index.js' file in the server folder.

*    Type the following code in the index.js file

The VS Code image

*    Here the highlighted link is the step 2 : So How to get that link? lets discuss that in step 2 elaborately.

 👉     STEP 02 - Setting up things in MongoDB Atlas.

*    As the initial step sign in using the email and the password.

*    Then create a new project by giving a name for the project.


*    Then after creating the project you have to create a cluster in the project.


*    Then select the free cluster and then choose the cloud storage along with county (Make sure you choose a county with free storage fee) and provide a cluster name. Finally the cluster is created.

*    The below images shows the windows needed for a successful cluster creation.








*    After creating the cluster you have to select the users who have database access.

*    Create a database user with a username and password as follows.



*    After creating the user for database access then select the network access from the project homepage. Here you have to specify from which device/location you are accessing he database.




*    Now we have created a user for the database and the access location/device of the database.

*    Now go back again to the project-cluster homepage.


*    Click on the connect tab.
        
    1.    We should connect the database with the application.
        
    2.    We connect the database with the MongoDB compass for monitoring the database.

👉     STEP 03 - Connect the database with MongoDB Compass.


*    Click on the connect tab as mentioned in the above image.

*    You are launched at the following window.


*    Select connect with 'MongoDB Compass'.

*    Since you have MongoDB Compass installed in the machine copy the link to connect the database.

*    Paste the link in the MongoDB Compass (New Connection). 

*    You can see a part in the link mentioned <password>.

*    Erase that and type the password you have been given for the database user (One you created before with a Username and Password).

*    That's it, now you have successfully connected your database to MongoDB Compass where you can monitor easily.


👉     STEP 04 - Connect the database with VS Code project.

*    Now select the 'connect with application'

*    You get a link, copy that as before.

*    Paste that link in the highlighted part in the below code. 

*    Make sure you give the password of the database user as before.


*     That's all now you have the database connected to your MERN project.

*     Note that the main steps are mentioned for the demo purpose but you can explore more.

*    Stay tuned for the next blog on creating schema on MongoDB from vs code project.  





Sunday, April 18, 2021

MERN stack and Introduction

👉    What is MERN stack ? 

*    MERN stands for MongoDB, ExpressJS, ReactJS, NodeJs.

*    This is a combination simply a stack of all 4 technologies listed above.

*    A MERN Project structure is simple represented as:

    The Database of the project is         :        Mongo Database [Document Database].

    The front-end/ UI                            :        React JS.

    The web framework                        :        Express JS.

    The web server                                :        Node JS

*    MERN is considered as the ideal way to work with JS and JSON in the industry.

👉    What is the MERN stack architecture ?

*    The MERN stack project can be either implemented in client-server (CS) architecture or the Model-View-Controller (MVC) architecture. 

*    Either way the basic architecture of a MERN stack project consists of  Front-end, back-end and database.

*    This can be represented as follows:

👉    React JS front-end.

*    The front-end of a MERN stack project is developed through the React JS.

*    React JS is a popular JS (JavaScript) framework which is used to create UI which renders the UI as the HTML page but actually codes in JS.

*    React JS is used in the front-end due to data driven interfaces with minimal code and the feature of handling the events.

*    React JS also provides features like hooks. webPack, Parcel, state and props to increase the supportability and usability.

👉    ExpressJS and NodeJS.

*    Express JS is a server-side framework running on NodeJS server.

*    Express JS is meant to be popular due to URL routing and handling HTTP request and response.

*    React JS interface interacts with the Express JS through HTTP requests like GET, POST, etc to update or modify data in MongoDB.

*    The functions implemented using Express JS calls the MongoDB driver through callbacks or promises to access the data in database.

👉    Mongo Database.

*    Mongo DB is used to store all the data that flows through the front-end.

*    The Reacts JS , Express JS and Node JS make use of MongoDB for data handling.

*    JSON comes into action when Mongo DB is used in a MERN stack project.

*    All data that are transmitted from the front-end are stored as JSON objects in Mongo DB.

*    Storing data as JSON facilitates in easy access and organized data storage.

 

.

👉    Pre-requirements to start a MERN stack project.

*    In this blog lets list down all the pre-conditions needed to start MERN project which means the packages that facilitates the MERN stack.

*    In coming blogs lets look into each important steps involved in a simple MERN project.

*    We will be using a Client Server architecture for the MERN project.

*    All the packages that are needed by client and server are listed below:


*    The above table shows all the required packages needed to create a MERN stack project.

*    HINT    :     You can use koaJS instead of ExpressJS according to your requirement in the project.







Monday, April 5, 2021

REST API and its 6 constrains

👉    What is REST API?

*    REST stands for Representational State Transfer.

*    It is often used in development in web services. (designing loosely coupled applications over HTTP).

*    It specifies highlevel design guidelines and leaves the implementation to be constructed by the developer.

*    REST is an architectural design, its not either a protocol or a standard.

*    When a client request through the REST API it transfers the resource state to the endpoint or requester.

*    The response from the REST API may be a JSON, HTML, XML, PHP or even a plain text.

*    The RESTful API HTTP request contain headers and parameters which consists of important identifier information such as request metadata, authorization, Uniform Resource Identifier (URI), cookies and more.


👉    What are the architectural constraints of REST API?

*    The above diagram shows the 6 main architectural constrains of the REST API. Although it states 6 the main constraints are only 5. The last constraint (Code-on-demand) is not that visible in the industry and either used in many instances.

1.    Uniform Interface.

*    Uniform interface gives us a simple idea of having a global language which can be understood by everyone.
*    This is based on the HTTP specification.
*    The person should be able to conclude that this is a RESTful API request by looking at the URI, HTTP verb.
*    An example for a URI is https://sliit.lk/api
*    Now lets look into some examples in handling a resource in REST API.

    💥    Create a resource.    POST v1/faculties
    💥    Fetch a resource.    GET v1/faculties
    💥    Update the entire  resource.    PUT v1/faculties/{faculty.id}
    💥    Delete a resource.    DELETE v1/faculties

*    Now lets create a department inside a faculty.
    
    💥    POST v1/faculties/{faculty.id}/departments

*    From the above example what is this faculty.id?
        This is known as the route parameter. There can be multiple route parameters only if it makes a meaning. In many cases the route parameters are not numerous in an URI.

*    What are query parameters?
        These are used in sorting, pagination. These are optional in an URI. There can be multiple query parameters in an URI depending on the logic.

     💥    POST v1/faculties?order-by=name

2.    Stateless.

*    This constrain states that the request has to self-contained.
*    The server does not maintain the state of the client.
*    This in turn can said as, in client-server communication the client information is not stored between the get request and each request is separated and unconnected.

3.    Client-server.

*    The REST API request and response follows a client server architecture.
*    This has a client, server and a HTTP request and response.
*    The HTTP stack is the communication platform between the server and the client.

4.    Cacheable.

*    The REST services should be cacheable.
*    In general the REST services are cacheable, but according to the business requirement the caching nature changes.
*    Consider a banking system, where it is needed to store the account transaction details. But this system is non-cacheable due to security reasons.

5.    Layered System.

*    This constrain mainly hides the inner complexities from the client.
*    In simple words, the inner business logic is not shown to the end user.
*    The client only request and receive the response, in which he/she can see only the URL, not how the resource is handled according to the request.

6.    Code-on-demand [OPTIONAL].

*    This states that the server is able to transfer some code logics to the client, to execute them in the client side.
*    This is not mainly done in the industry for security and performance issues.
*    On the other hand if the code is transferred to the client, it becomes an over-head from the client and results in non-reliable nature.

    


Monolithic vs Microservices

👉     Monolithic Architecture. *     Monolithic architecture was there before Microservices came into action.  *     All functionalities or...