light-4j Documentation

repository·master·Indexed 25 days ago

https://github.com/networknt/light-4j

A fast, lightweight, and cloud-native microservices framework built on Undertow HTTP core. It features an embedded gateway for security, validation, and observability, and includes modules for HTTP client configuration (supporting Undertow and JDK 11), a HikariCP-based data-source provider for various databases (MySQL, Postgres, Oracle, etc.), and an egress-router for managing service discovery, JWT tokens, and TLS certificates.

Tokens
9.3K
Snippets
17
Records
49
Agent score
87%

What's inside light-4j

  1. Overview of the client-config module

    master

    The client-config module is a shared component used by both the light-4j client module and light-aws-lambda. It provides configuration support for two types of HTTP clients:

    1. Undertow-based client
    2. JDK 11 http-client

    This module enables configuration reloading and is designed to be backward-compatible by following the Undertow client syntax in cases of discrepancy.

  2. Overview of Light-4j frameworks

    master

    Light-4j provides multiple specialized frameworks for different microservice architectures:

    • light-rest-4j: A RESTful microservice framework using OpenAPI specifications for code generation, runtime security, and validation.
    • light-graphql-4j: A GraphQL microservice framework supporting schema generation from IDL and plugins.
    • light-hybrid-4j: A hybrid framework combining monolithic and microservice architectures.
    • light-eventuate-4j: A messaging-based framework built on Kafka, supporting event sourcing and CQRS.
  3. Overview of the egress-router module

    master

    The egress-router module provides handlers and utility classes for managing egress traffic (routing). It is designed to offload complex client-side responsibilities from legacy systems or applications developed in different technologies (such as Node.js or .NET) when they need to call light-api services.

    By using router handlers, you can delegate the following client-side features to the router:

    • Service discovery
    • JWT token management
    • Loading TLS certificates
  4. Implement Service Self-Registration via WebSocket

    master

    Services must register themselves with the portal-registry using a dedicated WebSocket channel. This channel handles service registration, liveness monitoring, and bidirectional command communication.

    Endpoint: wss://<controller-host>:<port>/ws/microservice

    Authentication: Authentication is performed within the first JSON-RPC message payload using the service/register method. The JWT must be provided in the params.jwt field without a Bearer prefix. The JWT must use the RS256 algorithm. The controller resolves the signing key via JWKS using the kid.

    Disconnect Semantics: There is no explicit deregister RPC. Closing the registration socket automatically marks the instance as disconnected. Reconnecting will generate a new runtimeInstanceId.

    {
      "jsonrpc": "2.0",
      "id": "register-1",
      "method": "service/register",
      "params": {
        "jwt": "<service-jwt>",
        "serviceId": "com.networknt.user-1.0.0",
        "envTag": "prod",
        "environment": "prod",
        "version": "1.0.0",
        "protocol": "https",
        "port": 8443,
        "tags": {}
      }
    }
  5. Perform Service Discovery via WebSocket

    master

    Consumers can discover services using the Discovery Channel. This channel supports one-shot lookups, live subscriptions, and receiving change notifications.

    Endpoint: wss://<controller-host>:<port>/ws/discovery

    Authentication: Authentication occurs during the WebSocket upgrade request via the Authorization header: Authorization: Bearer <token>

    portal-registry uses controllerDiscoveryToken if configured, otherwise it defaults to portalToken.

  6. Use the Token Limit handler to rate limit OAuth 2.0 token endpoints

    master

    The Token Limit handler is used to rate limit the OAuth 2.0 token endpoint. Its purpose is to ensure clients are caching tokens and to identify 'bad citizens' that request a new token for every request.

    This handler can be deployed on:

    • The oauth-kafka server
    • A dedicated light-gateway for OAuth token and key services.

    Environment-specific behavior:

    • Lower environments: The handler can be configured to return an error message if a token is not cached by the client.
    • Production environments: The handler can be configured to allow the request to complete while emitting a warning in the logs.
  7. Start the Light-4j server

    master

    Depending on your environment, you can start the server using one of the following methods:

    • In IDE: Run the com.networknt.server.Server main class.
    • From Maven: Use the exec:exec goal.
    • Command Line: Run the generated JAR file.
    ### From Maven
    mvn exec:exec
    
    ### Command Line
    java -jar target/demo-0.1.0.jar
  8. Get started with Light-4j

    master

    You can start a new project using one of two methods:

    1. Light-codegen generator: Use the light-codegen tool to generate a working project. It currently supports light-rest-4j, light-graphql-4j, light-hybrid-server-4j, and light-hybrid-service-4j. You can use it via:
      • The codegen-cli command line utility after building the project.
      • The networknt/light-codegen Docker image.
      • The generate.sh script from the model-config repository.
      • The codegen-web API (UI pending).
    2. Example projects: Copy and adapt an existing project from light-example-4j. Detailed descriptions of examples and tutorials are available on the official website.
  9. Stop the Light-4j server gracefully

    master

    While Ctrl+C can be used to kill the server, for production environments you should use the TERM signal to allow the server's shutdown hook to execute. This ensures the server can:

    • Complete in-flight requests.
    • Close database connections.
    • Notify service registries (if using discovery) and wait for clients to refresh their local cache (up to 30 seconds) before shutting down.
    kill -s TERM <pid>
  10. Learn how to use light-proxy

    master

    To implement or use the proxy functionality, refer to the following resources:

    • Source Code: Visit the light-proxy GitHub repository.
    • Core Concepts: Read the Getting Started guide.
    • RESTful Proxy Implementation: Follow the Tutorial for a step-by-step guide.
    • Configuration: Consult the Configuration documentation to adapt the proxy to your specific requirements.
    • Deployment: Use the Artifact guide to select the correct artifact for your deployment environment.