LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

What's on this Page

    • Introduction
    • Preparation
    • Clone the specifications
    • Code generation
    • Test generated code

Introduction and Code Generation

The starting point of the discovery tutorials will walk you through code generation for the services that will be used throughout.

Introduction

This is a tutorial to show you how to use service registry and discovery for microservices. The example services are implemented in a RESTful style but can be implemented in GraphQL or Hybrid as well. We are going to use api_a, api_b, api_c, and api_d as our examples, with security disabled for simplicity. There are some details might not be shown in this tutorial, for example, walking through light-codegen config files, etc. It is recommended to go through ms-chain before this tutorial.

Preparation

In order to follow the steps below, please make sure you have your development environment setup.

  • Create a working directory under your user directory called networknt.
mkdir ~/networknt

Clone the specifications

In order to generate the initial projects, we use light-codegen to scaffold these services from configuration outlining the API specs.

cd ~/networknt
git clone https://github.com/networknt/model-config.git

Code generation

In order to generate the four projects, let’s clone and build the light-codegen in the workspace. We are going to use the command line utility instead of the Docker container.

cd ~/networknt
git clone https://github.com/networknt/light-codegen.git
cd light-codegen
mvn clean install

We are going to generate these preject into the light-example-4j repository. Let’s clone it and move the discovery folder to discovery.bak so that you can compare your work with the checked in copy.

cd ~/networknt
git clone https://github.com/networknt/light-example-4j.git
cd light-example-4j
mv discovery dicovery.bak

Now let’s generate the four APIs.

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_a/generated -m model-config/rest/openapi/aa/1.0.0/openapi.yaml -c model-config/rest/openapi/aa/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_b/generated -m model-config/rest/openapi/ab/1.0.0/openapi.yaml -c model-config/rest/openapi/ab/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_c/generated -m model-config/rest/openapi/ac/1.0.0/openapi.yaml -c model-config/rest/openapi/ac/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/discovery/api_d/generated -m model-config/rest/openapi/ad/1.0.0/openapi.yaml -c model-config/rest/openapi/ad/1.0.0/config.json

We have four projects generated in ~/networknt/light-example-4j within the discovery folder.

Test generated code

Now you can test the generated projects to make sure they are working with mock data. We will pick up one project to test it, but you can test them all.

cd ~/networknt/discovery/api_a/generated
mvn clean install exec:exec

From another terminal, access the server with curl command and check the result.

curl -k https://localhost:7441/v1/data
["Message 1","Message 2"]

Based on the config files for each project, the generated service will listen to 7441, 7442, 7443 and 7444 for https connections. You can start other services to test them on their corresponding port with the same path.

This concludes the first step, and we now have four generated APIs in working condition. In the next step, we are going to change it to enable API to API communication.

Next Step: static

  • 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
“Introduction and Code Generation” was last updated: June 20, 2019: fixes #112 rewrite service discovery tutorial and add security and OpenTracing (0ed89f5)
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