LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub
Star

Merge Multiple Services

In this tutorial, we are going to start a server with multiple services. Each service will have its schema.json, and they need to be merge during server startup so that validation and scope verification can be done on all requests based on the merged schema.

Prepare Environment

Before starting, we need to prepare the environment by clone several repositories from networknt and build the light-codegen. Let’s assume that you are using a workspace called networknt under your user’s home directory.

cd ~/networknt
git clone https://github.com/networknt/light-codegen.git
git clone https://github.com/networknt/model-config.git
git clonehttps://github.com/networknt/light-example-4j.git
cd light-codegen
mvn clean install
cd ..

As we are going to regenerate a server and several services in light-example-4j, let’s rename the merge-schema folder so that you can compare them if you want.

cd ~/networknt/light-example-4j/hybrid
mv merge-schema merge-schema.bak

Generate Merge Server

In light-codegen, the light-hybrid-4j framework generator needs a config.json as input to generate a server project. This file can be found in model-config/hybrid/merge-schema/server.

Here is the content of config.json

{
  "rootPackage": "com.networknt.merger",
  "handlerPackage":"com.networknt.merger.handler",
  "modelPackage":"com.networknt.merger.model",
  "artifactId": "merger",
  "groupId": "com.networknt",
  "name": "merger",
  "version": "1.0.0",
  "httpPort": 8080,
  "enableHttp": false,
  "httpsPort": 8443,
  "enableHttps": true,
  "enableHttp2": true,
  "enableRegistry": false,
  "supportOracle": false,
  "supportMysql": false,
  "supportPostgresql": false,
  "supportH2ForTest": false,
  "supportClient": false
}

Here is the command line to generate the server from the networknt folder.

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-hybrid-4j-server -o light-example-4j/hybrid/merge-schema/server -c model-config/hybrid/merge-schema/server/config.json

Build the server

cd ~/networknt/light-example-4j/hybrid/merge-schema/server
mvn clean install

Generate Two Services

Now let’s generate two services. For hybrid service generator, it needs a config.json and also a schema.json to define the interface/contract for the service.

These files can be found in model-config/hybrid/merge-schema/service1 and service2 folder.

Here is the list of command lines to generate hybrid services

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-hybrid-4j-service -o light-example-4j/hybrid/merge-schema/service1 -m model-config/hybrid/merge-schema/service1/schema.json -c model-config/hybrid/merge-schema/service1/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-hybrid-4j-service -o light-example-4j/hybrid/merge-schema/service2 -m model-config/hybrid/merge-schema/service2/schema.json -c model-config/hybrid/merge-schema/service2/config.json

Update Two Services

Now, let’s update the two services to return a JSON object in the handler.

For service1, let’s update the QuerySide.java to

package com.networknt.merger.handler;

import com.networknt.utility.NioUtils;
import com.networknt.rpc.Handler;
import com.networknt.rpc.router.ServiceHandler;
import java.nio.ByteBuffer;
import io.undertow.server.HttpServerExchange;

@ServiceHandler(id="lightapi.net/service1/query/0.1.0")
public class QuerySide implements Handler {
    @Override
    public ByteBuffer handle(HttpServerExchange exchange, Object input)  {
        return NioUtils.toByteBuffer("{\"message\":\"Hello Query!\"}");
    }
}

For service2, let’s update the CommandSide.java to

package com.networknt.merger.handler;

import com.networknt.utility.NioUtils;
import com.networknt.rpc.Handler;
import com.networknt.rpc.router.ServiceHandler;
import java.nio.ByteBuffer;
import io.undertow.server.HttpServerExchange;

@ServiceHandler(id="lightapi.net/service2/command/0.1.0")
public class CommandSide implements Handler {
    @Override
    public ByteBuffer handle(HttpServerExchange exchange, Object input)  {
        return NioUtils.toByteBuffer("{\"message\":\"Hello Command!\"}");
    }
}

Let’s build the service1 and service2

cd ~/networknt/light-example-4j/hybrid/merge-schema/service1
./mvnw clean install
cd ~/networknt/light-example-4j/hybrid/merge-schema/service2
./mvnw clean install

Start the server with services

Now let’s start the server with services in the classpath.

cd ~/networknt/light-example-4j/hybrid/merge-schema/server
java -cp target/merger-1.0.0.jar:../service1/target/merger-1.0.0.jar:../service2/target/merger-1.0.0.jar com.networknt.server.Server

Now the server is up and running with two services.

Test

Access the query service

curl -k -X POST https://localhost:8443/api/json -H 'content-type: application/json' -d '{"host":"lightapi.net","service":"service1","action":"query","version":"0.1.0","data":{"q1":"Hu","q2":"Steve"}}'

The response should be

{"message":"Hello Query!"}

Access the command service

curl -k -X POST https://localhost:8443/api/json -H 'content-type: application/json' -d '{"host":"lightapi.net","service":"service2","action":"command","version":"0.1.0","data":{"c1":"Hu","c2":"Steve"}}'

The response should be

{"message":"Hello Command!"}

Summary

As you can see, both services have their own schema during development. After they are deployed to the server, these schemas are merged together to form a single service. All traffic to the server instance will be routed to the right handler by the light-hybrid-4j framework.

This tutorial doesn’t explain the details but only walk you through the steps. If you want to learn more, please visit hybrid tutorial

  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Merge Multiple Services” was last updated: April 8, 2019: fixes #69 add light-hybrid-4j merge services tutorial (2ddb2ad)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute