Skip to main content

Posts

Showing posts from November, 2019

Microservices - Config management using Spring cloud bus and Rabbit MQ

In this tutorial we will learn how to add Rabbit MQ capabilities to our Spring cloud config server so any changes to configurations can be pushed to all connected applications during runtime. We need such kind of behaviour when we need to refresh the properties without restarting our application. Below is the overall architecture for this complete setup. Spring Cloud Config Server Spring cloud config server is used to setup the distributed configuration using GIT or local file system where we can keep our configuration files and serve as them from Spring cloud config server. Client application just has to connect with config server by providing their application and profile name for specific configuration. Please refer below link where I have explained more about cloud config and how to code it. https://www.thetechnojournals.com/2019/10/spring-cloud-config.html Install Rabbit MQ Please refer below link on how to install rabbit-mq and virtual host verification. https://w

Installing Rabbit MQ

Install Rabbit MQ There are many ways to install rabbit-mq as given below. Using Homebrew Homebrew has made the life easy to install the Rabbit-MQ in mac. Please refer below link to install the rabbit MQ. https://www.rabbitmq.com/install-homebrew.html Once it is installed, below command can be executed to start & stop rabbit MQ. Start Rabbit MQ: /usr/local/opt/rabbitmq/sbin/rabbitmq-server Stop Rabbit MQ: /usr/local/opt/rabbitmq/sbin/rabbitmqctl stop Using Docker With docker it's very easy to install, we just need to pull the image and run it by executing simple command. Assuming you already have dcoker installed in your machine, you can install and run rabbit-mq using below steps. Pull rabbit-mq image without management console. docker pull rabbitmq:latest Pull rabbit-mq image with management console. It is required to enable the UI for management console. docker pull rabbitmq:3-management Execute below command to run the image in container named &quo

Web scraper using JSoup and Spring Boot

What is webscraping Webscraping is a technique to extract or pull the data from a website to gather required information by parsing the HTML source of their websites, such as articles from news or books site, products information from online shopping sites or course information from education sites. There are many organisations who uses web scraper to provide the best experience to their customers, for example extract the price for a smartphone from multiple online websites and show their customers the best and cheap product URL. We will learn here how to code a web scraper by developing a simple new scraper service. News scraper News scraper is used to extract the news articles or other related contents from a news site. Here we are going to create a web scraper application to pull the articles from news site. Below are the operations provided by our news scraper service. List all the authors Search articles by author name Search articles by article title Search articles

Microservices - Setting up PCF Dev for local development environment

In this tutorial we are going to learn how to setup PCF development environment in local desktop/laptop. Why PCF PCF is a multi-cloud commercial platform where customers can run enterprise applications. It provides continues delivery, security and customization of your products in cloud. So customers can focus on actualy application development and deployment. They don't need to bother much about preparing the infrastructure etc. For more details you may refer below link. https://pivotal.io/why-pivotal What is PCF Dev PCF Dev is a distribution provided by Pivotal which allows developers to run the full featured cloud foundry so development and debugging becomes easier for the developer. If you create free developer account in PCF then you will get only 2 GB memory to run your applications there while for a microservice application it may not be enough. When you setup PCF dev on your machine you don't face such challenges and memory limits to your machine only. Install

Executing chain of tasks sequentially using CompletableFuture

Here we are going to see how to create a chain of tasks using CompletableFuture and then execute them in sequence one by one. Our task is to have a number in a loop and then send it two CompletableFuture chain to process each number in two tasks, for example methodA() & methodB() where number is passed to methodA() and then output of it is passed to methodB(). Like this we will create a chain which will execute in the same order it has created. Common methods Below are common method which will be used as execution of the tasks. Integer methA(int seq) { System.out.println("A-"+seq); return seq; } Integer methB(int seq) { System.out.println("B-"+seq); return seq; } There are many ways to solve this problem. We will see two different approaches to solve the problem with CompletableFuture. Using chain of thenApply() In this approach we will create a single CompletableFuture and then we will keep adding furth

SpringBoot - @ConditionalOnProperty example for conditional bean initialization

@ConditionalOnProperty annotation is used to check if specified property available in the environment or it matches some specific value so it can control the execution of some part of code like bean creation. It may be useful in many cases for example enable/disable service if specific property is available. Below are the attributes which can be used for property check. havingValue - Provide the value which need to check against specified property otherwise it will check that value should not be false. matchIfMissing - If true it will match the condition and execute the annotated code when property itself is not available in environment. name - Name of the property to be tested. If you want to test single property then you can directly put the property name as string like "property.name" and if you have multiple properties to test then you can put the names like {"prop.name1","prop.name2"} prefix - It can be use when you want to apply some prefix to

How to handle exceptions in REST services (JAX-RS and SpringBoot)

Exception handling is vital aspect of any application or service. Exceptions should be handled properly and appropriate messages must be generated to end user or service in case of any exception. Same applies to REST services also whether they are used by some front-end application or some other service uses it to perform some operations. It becomes tricky when we deal with REST services as we need to expose all the supported status codes for both success and error response which our service can generate, so client application or service can handle them appropriately.  Below are some samples of the status code. 200 - OK (API processes the request perfectly) 404 - Not found (Request URI or resource is not available) 500 - Internal server error (Request failed  due to some error at runtime at service side) There are multiple ways to handle the exceptions which we will see in below sections. Manual exception handling We can handle the exception manually also like using try-cat