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.
- Maven
- Gradle
<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>
plugins { id 'java' }
repositories { mavenLocal(); mavenCentral() }
dependencies {
implementation 'net.hasor:meteor-protocol-jsonrpc:5.0.0-SNAPSHOT'
}
java { toolchain { languageVersion = JavaLanguageVersion.of(17) } }
Publish the service
Define the EchoService interface in your project.
public interface EchoService {
String echo(String text);
}
Create ProviderMain to implement the interface and publish it over JSON-RPC.
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
- Meteor (Java)
- HTTP (curl)
In the same project, create ConsumerMain. Reuse the EchoService interface to make a call at a fixed address.
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!
With the provider still running, send a JSON-RPC request from your terminal.
curl -sS http://127.0.0.1:8080/jsonrpc/default/1.0.0 \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":1,"method":"echo.echo","params":["Meteor"]}'
The response returns Hello, Meteor! in result.
{"jsonrpc":"2.0","id":1,"result":"Hello, Meteor!"}
echo.echo combines the service alias echo with the method name echo. In the URL, default is the default service group and 1.0.0 is the default version. Clients in other languages can send the same HTTP request.
Next steps
- Build an application to integrate Meteor in a plain Java project or with Spring, Hasor, or Solon.
- Publish over multiple protocols to add endpoints to the same service.
- Use service discovery to switch from a fixed address to an address book or registry.