Skip to content

RPC Invoke Extension

Ryu Xin edited this page Oct 17, 2022 · 5 revisions

Sample description

With this sample, you can learn:

  1. How to configure an extension point of type REMOTE
  2. A sample remote extension point invocation
  3. Limitations of Remote Call Extension Points
  4. How to customize remote call implementation

What you’ll need

  1. An Integrated Developer Environment (IDE). Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
  2. A Java™ Development Kit (JDK). We recommend BellSoft Liberica JDK version 8 or version 11.
  3. Nacos installation has been completed and can run normally
  4. Complete the Quickstart Guide reading

Maven dependency

<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-model</artifactId>
      <version>1.0.12</version>
</dependency>
<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-runtime</artifactId>
      <version>1.0.12</version>
</dependency>

Step 1: Define the extension point of the REMOTE type

We define a protocolType that is an extension point of REMOTE, as follows:

public interface RemoteSampleBusinessExt extends IBusinessExt {

    String EXT_REMOTE_HELLO_INVOKE = "EXT_REMOTE_HELLO_INVOKE";

    @Extension(
            name = "EXT_REMOTE_HELLO_INVOKE",
            code = EXT_REMOTE_HELLO_INVOKE,
            protocolType = ProtocolType.REMOTE,
            reduceType = ReduceType.FIRST
    )
    String remoteHelloInvoke(String word);
}

Step 2: Build a business plug-in server

In this sample code, we define a project that runs business custom plugins: lattice-plugin-server. In this project, the Lattice plugin container needs to be import in pom.xml, as follows:

<dependency>
    <groupId>org.hiforce.lattice</groupId>
    <artifactId>lattice-remote-container</artifactId>
    <version>${version.lattice}</version>
</dependency>

Then deploy business A's implementation of this remote extension point under this project. The custom implementation of business A is as follows:

@Business(code = BusinessA.CODE, name = "Business A")
public class BusinessA {
    public static final String CODE = "business.a";
}

@Realization(codes = BusinessA.CODE)
public class BusinessAExt extends BlankRemoteSampleBusinessExt {

    @Override
    public String remoteHelloInvoke(String word) {
        String str = "Hello: " + word;
        System.out.println("=====> BusinessAExt: " + str);
        return str;
    }
}

Then, we define a LatticeCommandRunner to start the Lattice plugin container after the Spring Boot container is successfully started. as follows:

@Component
public class LatticeCommandRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        LatticePluginContainer.getInstance().start();
        System.out.println(">>> Lattice Plugin Container started!");
    }
}

In the example, we use middleware such as dubbo and nacos. You need to modify the parameters such as application.properties and nacos registration address in bootstrap.properties. How to build Nacos is beyond the scope of this article.

Step 3: In the main business service, enable the Lattice remote call function

In this example, lattice-fos-server is our assumed business main service. We need to introduce lattice-remote-runner-dubbo in pom.xml to develop remote extension point calls in dubbo mode.

<dependency>
    <groupId>org.hiforce.lattice</groupId>
    <artifactId>lattice-remote-client</artifactId>
    <version>${version.lattice}</version>
</dependency>
<dependency>
    <groupId>org.hiforce.lattice</groupId>
    <artifactId>lattice-remote-runner-dubbo</artifactId>
    <version>${version.lattice}</version>
</dependency>

For remote invocation, in addition to dubbo, there can also be other protocols such as RESTFUL. We just need to replace lattice-remote-runner-dubbo with packages of other protocols. In the back, we will introduce how to customize and extend.

In the main service, we define TestInvokeController to simulate the use of this remote extension point, as follows:

@RestController
public class TestInvokeController {

    @RequestMapping("/hello")
    public String hello() {
        LatticeRemoteInvokeAbility ability = new LatticeRemoteInvokeAbility(() -> new BizContext() {
           ......
        });
        return ability.remoteHelloInvoke();
    }
}

After we start both the business main service and the business plugin container, open the browser and enter http://localhost:8088/hello , we can see the following prints in the background of the business plugin container:

=====> BusinessAExt: Hello: Jack

Restrictions on remote extension point calls

For extensions whose type is REMOTE, business overlay is not supported in Lattice. Because there are a large number of remote implementations superimposed, it will have a great impact on performance and stability. If the business needs to be superimposed, my suggestion is to further define the business extension point in the remote business plug-in container, realize the business overlay, and then return the overlay result.

How to customize remote call implementation

We can refer to the writing method of lattice-remote-runner-dubbo project, and we can refer to the writing method of DubboExtensionRunnerBuilder to implement the RemoteExtensionRunnerBuilderBean interface, as follows:

@Service
public class DubboExtensionRunnerBuilder implements RemoteExtensionRunnerBuilderBean {
    @Override
    public <R> ExtensionRemoteRunner<R> build(
            IAbility ability, TemplateSpec templateSpec, 
            String extCode, String scenario) {
            ......
    }
}

This Builder needs to be made into a SpringBean. It should be noted that Lattice currently supports the existence of a remote interface BuilderBean.

Sample code URL

URL of this sample code: https://github.com/hiforce/lattice-remote-sample


中文版:https://www.ryu.xin/2022/10/01/lattice-rpc-invoke/