Malagu v1.4.1 released-supports serverless-first microservice framework

Introduction to Malagu Framework

Malagu is a Serverless First, componentized, platform-independent progressive application framework based on TypeScript.

background

When we start to develop a new project, how to choose between monolith and microservice architecture? For entrepreneurial teams, they are more inclined to adopt a monolithic architecture for trial and error. After the business model is successfully verified, it gradually evolves to a microservice architecture according to the increase in business scale, thereby solving the bloated and maintainable monolithic architecture. Sex and so on.

Currently, there are many implementations of microservices, and they are quite mature, such as microservice frameworks like Spring Cloud, Dubbo, etc. The microservice solution provided by Malagu is Serverless first and more suitable for Serverless scenarios. Serverless makes the implementation of the microservice framework lighter, and the use of the framework is simpler. When we use the traditional microservice framework to deploy on the Serverless platform, we will find that the traditional microservice framework is too bloated, many in the traditional The useful functions of the server architecture are implemented and transparently removed by the bottom layer of Serverless, so that developers don't need to care. Such as service registration and discovery, health monitoring, logs, operation and maintenance, and so on.

feature

The microservice solution provided by the Malagu framework has the following characteristics:

  • Lightweight: no reliance on third-party microservice modules
  • Simple: Consistent with the development style of ordinary back-end services, out of the box, zero learning cost
  • Smooth switching: seamless switching from a single unit to a microservice
  • Unified: front-end and BFF, BFF and microservices, and microservices and microservices use a unified JSON RPC communication protocol
  • Serverless first

 

Initialize the microservice project

The Malagu framework provides a microservice development template microservice. Based on this template, we can quickly initialize a microservice project. Of course, we can also create a monorep style project to manage multiple microservice projects. You can choose a template multi-component. The command to initialize the microservice project is as follows:

# Initialize a
 microservice project with a project name of user-service malagu init user-service microservice

The directory structure of the microservice project is as follows:

.
 ├── README.md
 ├── malagu.yml # configuration file
 ├── package.json
 ├── src
 │ ├── common
 │ │ └── welcome-protocol.ts # interface definition│
 └── node
 │ ├── module.ts # Module definition│
 └── welcome-server.ts # Interface implementation
 └── tsconfig.json

 

Develop microservices

  1. Define the interface:
export const WelcomeServer = 'WelcomeServer';

export interface WelcomeServer {
    say(): Promise<string>;
}
  1. Implement interface
import { WelcomeServer } from '../common/welcome-protocol';
import { Rpc } from '@malagu/rpc';

@Rpc(WelcomeServer)
export class WelcomeServerImpl implements WelcomeServer {
    say(): Promise<string> {
        return Promise.resolve('Welcome to Malagu');
    }
}

 

Call microservice

Method 1: Use configuration files to define microservice endpoints

import { WelcomeServer } from 'user-service/common/welcome-protocol';
import { Autorpc } from '@malagu/rpc';
import { Component } from '@malagu/core';

@Component()
export class A {
    @Autorpc(WelcomeServer)
    protected welcomeServer: WelcomeServer;
    say(): Promise<String> {
        return this.welcomeServer.say();
    }
}

Define the microservice endpoint:

malagu : 
  rpc : 
    Endpoint :   # Endpoint's key needs to be consistent with the string value @Autorpc parameters 
      WelcomeServer : HTTP : //abc.com/api/WelcomeServer

Second way: in @Autorpc()direct service endpoint definition of micro parameters

import { WelcomeServer } from 'user-service/common/welcome-protocol';
import { Autorpc } from '@malagu/rpc';
import { Component } from '@malagu/core';

@Component()
export class A {
    @Autorpc('http://abc.com/api/WelcomeServer')
    protected welcomeServer: WelcomeServer;
    say(): Promise<String> {
        return this.welcomeServer.say();
    }
}

 

Microservice endpoint rules

The endpoint rules for microservices developed using Malagu are: [ domain name ]/[ route prefix ]/[ RPC prefix] /[ service identifier ]. among them:

  • The default routing prefix (malagu.server.path) is / 
  • The default RPC prefix (malagu.rpc.path) is api 
  • The service identifier is generally defined as the interface name, such as WelcomeServer 

 

Microservice endpoint placeholder

If you use the microservices developed by the Malagu framework, the endpoint format is uniform. To simplify the configuration of the microservice endpoints, the framework provides two placeholders:

  • {rpcPath}The indicated value is: [ route prefix ]/[ RPC prefix]
  • {erviceIdentifier}  The indicated value is [ service ID ]

 

In the first method of microservice invocation, the configuration file can also be configured as follows:

malagu : 
  RPC : 
    Endpoint :   # Endpoint The key must be the same argument string value @Autorpc 
      WelcomeServer : HTTP : //abc.com { rpcPath } / { serviceIdentifier }

Use the default endpoint to further simplify configuration:

malagu : 
  rpc : 
    defaultEndpoint :   HTTP : //abc.com { rpcPath } / { serviceIdentifier }  # As long as your micro-services deployed in the same domain names under

 

Microservice authentication

The Malagu framework provides an interface ClientConfigProcessor, which is defined as follows:

import { AxiosRequestConfig } from 'axios';

export const ClientConfigProcessor = Symbol('ClientConfigProcessor');

export interface ClientConfigProcessor {
    process(config: AxiosRequestConfig): Promise<void>;
}

The framework passes the configuration parameters of the HTTP request to the implementation of this interface. Through the implementation of this interface, we can customize the configuration parameters of the HTTP request to bring our authentication information.

If we are using simple authentication, the framework provides a configuration property malagu.rpc.client.config. The configuration example is as follows:

malagu:
  rpc:
    client:
      config:
        auth:
          username: abc
          password: 123

In addition to the above configuration attributes, AxiosRequestConfigother attributes can be malagu.rpc.client.configconfigured.

Guess you like

Origin www.oschina.net/news/119916/malagu-1-4-1-released