# RPC Service Integration Method

The system provides dynamic service customization functionality, allowing users to extend and customize the system's service capabilities at runtime without recompiling and redeploying. Dynamic services can provide data queries, data processing, business integration, and other services based on user-defined business logic.

Dynamic services support two calling methods:

  • Based on standard HTTP protocol: Compliant with RESTful specifications, third-party systems can call dynamic services like standard Web APIs.
  • Based on JVM internal calls: Directly call service logic in the runtime JVM, facilitating integration between internal system modules.

Regardless of the calling method used, the definition and management of dynamic services are unified. Users only need to define the service execution logic and make the corresponding configurations to make the new service available externally.

Dynamic services also provide user authentication and access control mechanisms to ensure service security and reliability. At the same time, the platform has built-in support for service discovery and invocation, reducing integration complexity.

# Dynamic Service Provider

Dynamic services refer to services provided externally by the system, currently supporting calls through standard HTTP protocol and within the JVM, to achieve RPC-like calls.

First, you need to define a DynamicServiceProvider object, which can be created under the menu Development Customization -> Dynamic Services -> Service Providers. A DynamicServiceProvider object contains the following fields:

Variable Name Variable Type Description
name String Unique service name
logic tech.muyan.dynamic.DynamicLogic Service execution logic
active Boolean Whether it's active

The logic field is a DynamicLogic object used to define the service execution logic, with the type DYNAMIC_SERVICE_CORE_LOGIC. The following parameters are injected when this logic is executed:

Variable Name Variable Type Description
user tech.muyan.security.User Executing user
params Map Execution parameters
protocol tech.muyan.enums.DynamicServiceProtocol Method calling the current service, currently only supporting HTTP and JVM
log Closure<?> Log closure for printing execution logs

After configuring the dynamic service, you need to create a corresponding user token based on the User. This user token is used for identity authentication when calling dynamic services. User tokens can be created under the menu Development Customization -> Dynamic Services -> User Tokens by executing the Generate User Token Action. The creation of this user token requires specifying the corresponding user.

For already created user tokens, you can set the Is Effective field to enable or disable the user token.

# Platform Internal Dynamic Service Call

If you are developing applications based on this platform, you can use the pre-packaged DynamicServiceConsumerService class to call dynamic services. The usage of this class is as follows:

import tech.muyan.BeanHelper
import tech.muyan.service.DynamicServiceConsumerService

// čŽˇå– DynamicServiceConsumerService įš„ bean
// Get the bean of DynamicServiceConsumerService
DynamicServiceConsumerService dynamicServiceConsumerService = BeanHelper.getBean(DynamicServiceConsumerService)
// 调į”¨ dynamicServiceConsumerService įš„ invoke æ–šæŗ•
// Invoke the invoke method of dynamicServiceConsumerService
def res = dynamicServiceConsumerService.invoke(serviceName, params, userToken)
1
2
3
4
5
6
7
8

The calling process of DynamicServiceConsumerService is divided into two parts: service discovery and service invocation.

# Dynamic Service Discovery

Service discovery refers to finding the corresponding service provider from the system based on the service name. Currently, two protocols are supported: HTTP and JVM. If it's the HTTP protocol, the URL of the service provider is returned. If it's the JVM protocol, it directly attempts to call the service provider's logic through DynamicServiceProvider.

HTTP service discovery is based on the dynamicService.http.hosts in the system configuration DynamicConfig. Its Value is a string separated by ,, used to specify the addresses of HTTP service providers, such as http://localhost:8080/service,https://muyan.cloud/service. The service in the URL is a fixed prefix.

# Dynamic Service Invocation

When calling a dynamic service, you need to specify the service name, call parameters, and user token. The service name needs to conform to the following types:

  • Directly fill in the service name, DynamicServiceConsumerService will automatically search for the corresponding service provider in the service discovery based on the service name. If multiple service providers are found, it will randomly select one for invocation.
  • A uri starting with jvm:// indicates calling a JVM service, such as jvm://some_service, where some_service is the service name.
  • A uri starting with http:// or https:// indicates calling an HTTP service, such as http://host/service/some_service, where service is the server domain name, service is a fixed URL prefix, and some_service is the service name.
    • If you only want to specify using HTTP as the service call method, but don't want to specify which specific service provider to use, you can use the http://ANY_PROVIDER/some_service method, where ANY_PROVIDER indicates that the specific calling party is determined by the service discovery of DynamicServiceConsumerService.

# Third-Party Dynamic Service Call

If a third-party client wants to access the dynamic services of this platform, it needs to implement the service discovery and service invocation logic through the HTTP protocol on its own:

  • The client needs to specify the service server address, such as http://host/service/some_service, using a POST request
  • Include a X-MY-Token HEADER, with its value being the user token
  • Parameters are filled in the body in JSON format

TIP

Note that the service name needs to be URL encoded to avoid issues caused by special characters such as spaces

Last Updated: 9/29/2024, 2:33:14 AM