-
Notifications
You must be signed in to change notification settings - Fork 59
RPC Invoke Extension
With this sample, you can learn:
- How to configure an extension point of type REMOTE
- A sample remote extension point invocation
- Limitations of Remote Call Extension Points
- How to customize remote call implementation
- An Integrated Developer Environment (IDE). Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
- A Java™ Development Kit (JDK). We recommend BellSoft Liberica JDK version 8 or version 11.
- Nacos installation has been completed and can run normally
- Complete the Quickstart Guide reading
<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>
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);
}
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.
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
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.
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.
URL of this sample code: https://github.com/hiforce/lattice-remote-sample
Thanks..
Getting Started
Key Features
- Business Overlay Product
- Register Business Configuration
- Load Local Configuration
- Reduce Strategy
- UseCase Precipitation and Reuse
- RPC Invoke Extension
- Dynamic Loading
Key Concepts
Stories