Skip to main content

This page is available in Simplified Chinese.

Quick start

Publish a JSON-RPC echo service, then call it from the Meteor Java client or with curl to get Hello, Meteor!.

Prerequisites​

Use JDK 17 or later and Maven or Gradle. The provider listens on local port 8080. The consumer uses a fixed address and runs without a registry.

This guide uses 5.0.0-SNAPSHOT. If you use a development snapshot, first make sure it is available in your Maven repository. The examples support both the local Maven repository and Maven Central.

Add dependencies​

Create a plain Java project and apply the configuration for your build tool. The JSON-RPC module pulls in Meteor core as a transitive dependency.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>meteor-quick-start</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>net.hasor</groupId>
<artifactId>meteor-protocol-jsonrpc</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

Publish the service​

Define the EchoService interface in your project.

Service interface
public interface EchoService {
String echo(String text);
}

Create ProviderMain to implement the interface and publish it over JSON-RPC.

Provider
import java.util.concurrent.CountDownLatch;
import net.hasor.meteor.MetContext;
import net.hasor.meteor.bootstrap.MetConfiguration;

public class ProviderMain {
public static void main(String[] args) throws Exception {
// Configure the protocol and listening port.
MetConfiguration config = new MetConfiguration();
config.setApplicationName("echo-provider");
config.connector("http").bind("127.0.0.1", 8080)
.protocol("jsonrpc").contextPath("/jsonrpc");

try (MetContext context = config.buildContext()) {
context.start();

// Publish the service with the JSON-RPC alias echo.
context.publisher().rsfService(EchoService.class, text -> "Hello, " + text + "!")
.protocol("jsonrpc").aliasName("jsonrpc", "echo")
.register();
System.out.println("Service listening on port 8080");

// Keep the provider process running.
new CountDownLatch(1).await();
}
}
}

Run ProviderMain.main(). Once the startup message appears, leave the process running.

Call the service​

In the same project, create ConsumerMain. Reuse the EchoService interface to make a call at a fixed address.

Consumer
import java.util.List;
import net.hasor.meteor.MetBindInfo;
import net.hasor.meteor.MetContext;
import net.hasor.meteor.address.InterAddress;
import net.hasor.meteor.bootstrap.MetConfiguration;

public class ConsumerMain {
public static void main(String[] args) throws Exception {
// Configure the protocol for outgoing calls.
MetConfiguration config = new MetConfiguration();
config.setApplicationName("echo-consumer");
config.connector("http").protocol("jsonrpc").contextPath("/jsonrpc");

try (MetContext context = config.buildContext()) {
context.start();

// Subscribe to the service and add the provider’s fixed address.
MetBindInfo<EchoService> binding = context.publisher().rsfService(EchoService.class)
.protocol("jsonrpc").aliasName("jsonrpc", "echo")
.register();
context.getUpdater().appendStaticAddress(binding.getBindID(),
List.of(new InterAddress("jsonrpc", "127.0.0.1", 8080)));

// Get the interface proxy and invoke the method.
EchoService echo = context.getRsfClient().getRemote(binding);
System.out.println(echo.echo("Meteor"));
}
}
}

Run ConsumerMain.main(). It prints the following line.

Hello, Meteor!

Next steps​