Monday, September 9, 2019

Eureka Discovery Server

Overview:
This article is a continuation of the Multi Module project. Here we are going to define about discovery server (Eureka).
build.gradle
group = 'com.apandiyan.discoveryserver'

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

application.yml
server:
    port: 9000
eureka:
    client:
        register-with-eureka: false
        fetch-registry: false 
        service-url:
            defaultZone: http://localhost:9000/eureka 
spring:
    application:
        name: discovery-server

DiscoveryServerApplication.java
@SpringBootApplication
@EnableEurekaServer //optional
public class DiscoveryServerApplication {

public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}


Saturday, July 27, 2019

Part-1: Spring & Gradle - Multi Module Project

Overview:
In this article, we’ll implement a spring boot multi module application. We’ll use gradle build system. The goal is here to create a microservices application which will be covered in the upcoming articles.
Layout:
Here we are going to use flat layout to keep the application directories as siblings of the root project directory (Pic-1). The advantage of flat layout is we can have the distinct git repository for every application. In your root project’s settings.gradle file, we need to use the includeFlat with the subproject.
Pic-1. Directory Structure
Project: root
This project having only the project configuration files such as build.gradle and settings.gradle.

settings.gradle:
rootProject.name = 'root'
includeFlat 'utilities', 'exception-handler’, 'discovery-server' 
includeFlat 'enquiry', 'api-gateway', 'registration' 
build.gradle:
buildscript {
ext {
springBootVersion = '2.1.6.RELEASE' 
}
repositories {
mavenCentral() 
}
dependencies
classpath("org.springframework.boot:spring-boot-gradle- plugin:${springBootVersion}"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


group = 'com.apandiyan' 
version = '0.0.1'
sourceCompatibility = '1.8' 
repositories
mavenCentral() 
}
ext {
set('springCloudVersion', "Greenwich.SR1"
}
dependencies
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud- dependencies:${springCloudVersion}"
}
}

}


Source: https://github.com/pandiyan90/microservice-blog/tree/master/root

Sunday, June 9, 2019

Node.JS: winston custom logger with file rotation

How much easy to have logs in a separate file based on log level which is rotational?

Below is the simple logger.js file, which I have written the code to rotate the files once it reached the size of 10mb.

var winston = require('winston');
var DailyRotateFile = require('winston-daily-rotate-file');
var fs = require('fs');
var path = require('path');

const env = process.env.NODE_ENV || 'development';
const logDir = 'log';

if(!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const errorLogsFileTransport = new DailyRotateFile({
    filename: `${logDir}/error-logs-%DATE%.log`,
    datePattern: 'YYYY-MM-DD',
    maxSize: '10k',
    level: 'error',
    zippedArchive: true
})

const debugLogsFileTransport = new DailyRotateFile({
    filename: `${logDir}/debug-logs-%DATE%.log`,
    datePattern: 'YYYY-MM-DD',
    maxSize: '10k',
    level: 'debug',
    zippedArchive: true
})

const infoLogsFileTransport = new DailyRotateFile({
    filename: `${logDir}/info-logs-%DATE%.log`,
    datePattern: 'YYYY-MM-DD',
    maxSize: '10k',
    level: 'info',
    zippedArchive: true
})

const logger = winston.createLogger({
    level: env === 'development' ? 'debug' : 'info',
    format: winston.format.combine(
        winston.format.label({ label: path.basename(process.mainModule.filename)}),
        winston.format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        winston.format.json()
    ),
    transports: [
        new winston.transports.Console({
            level: 'info',
            format: winston.format.combine(
                winston.format.printf(
                    info => `${info.timestamp} ${info.level} [${info.label}]: ${info.message}`
                )
            )
        }),
        errorLogsFileTransport,
        debugLogsFileTransport,
        infoLogsFileTransport
    ]
});

module.exports = logger;