Unified gRPC/HTTP Realtime API framework for Java.
This framework is based on gRPC Streaming, which is a fast and binary network transport for HTTP/2.
However, unlike plain gRPC, it treats Java interfaces as a protocol schema, enabling seamless code sharing without .proto .
Loadbalancing is easily performed with functions supported by Herts.
There are multiple OSS to support.
Herts support original bidirectional streaming.
It enables two-way communication with a simple interface. Also, this communication method can be easily load balanced.
- gRPC Unary
- gRPC Client Streaming
- gRPC Server Streaming
- gRPC Bidirectional Streaming
- gRPC Herts Reactive Steaming
- HTTP API
Server-side
- Java 11+
org.hertsstack
,io.grpc
packages
Client-side
- Java 11+
org.hertsstack
,io.grpc
packages
Package | Maven |
---|---|
herts-codegen | |
herts-core | |
herts-rpc | |
herts-rpc-client | |
herts-http | |
herts-http-client | |
herts-broker | |
herts-broker-local | |
herts-broker-redis | |
herts-metrics | |
herts-gateway |
Dependency.
dependencies {
implementation 'org.hertsstack:herts-core:1.1.2'
implementation 'org.hertsstack:herts-rpc:1.1.3'
implementation 'org.hertsstack:herts-rpc-client:1.1.2'
}
Request Payload definition.
Herts request and response Java Objects should have extend HertsMessage
.
public class Payload extends HertsMessage {
private String hoo;
public String getHoo() {
return hoo;
}
public void setHoo(String hoo) {
this.hoo = hoo;
}
}
Define a interface.
It is used by server and client both.
Herts interface require a @HertsRpcService(value = HertsType.XXXX)
and extends HertsService
.
import HertsRpcService;
import HertsService;
@HertsRpcService(value = HertsType.Unary)
public interface UnaryService extends HertsService {
String helloWorld();
Map<String, String> getUser(Payload payload);
}
Implementation a class.
Herts implementation require a extends HertsServiceXXXX<YOUTR INTERFCE> implements YOUTR INTERFCE
.
import HertsServiceUnary;
public class UnaryServiceImpl extends HertsServiceUnary<UnaryService> implements UnaryService {
@Override
public String helloWorld() {
return "hello world";
}
@Overide
public Map<String, String> getUser(Payload payload) {
System.out.println(payload);
return Collections.singletonMap("name", "foo");
}
}
Server side.
public class Main {
public static void main(String[] args) {
UnaryService service = new UnaryServiceImpl();
HertsRpcServerEngine engine = HertsRpcServerEngineBuilder.builder()
.registerHertsRpcService(service) // You can register multi service
.build();
engine.start();
}
}
Client side.
public class Main {
public static void main(String[] args) {
HertsRpcClient client = HertsRpcClientBuilder
.builder("localhost")
.secure(false)
.registerHertsRpcServiceInterface(UnaryService.class) // You can register multi service
.connect();
UnaryService service = client.createHertsRpcService(UnaryService.class);
var res01 = service.helloWorld();
System.out.println(res01);
Payload p = new Payload();
p.setHoo("test");
var res02 = service.getUser(p);
System.out.println(res02);
}
}
Define a server service interface.
It is used by server and client both.
Herts service interface require a @HertsRpcService(value = HertsType.XXXX)
and extends HertsService
.
import org.herts.core.annotation.HertsRpcService;
import org.herts.core.service.HertsReactiveService;
@HertsRpcService(value = HertsType.Reactive)
public interface ReactiveStreamingService extends HertsReactiveService {
String publishToClient(Payload payload);
List<String> getIds();
}
Implementation a class.
Herts implementation require a extends HertsServiceXXXX<YOUTR INTERFCE> implements YOUTR INTERFCE
.
import org.herts.core.service.HertsServiceReactiveStreaming;
public class ReactiveStreamingServiceImpl extends HertsServiceReactiveStreaming<ReactiveStreamingService, ReactiveReceiver> implements ReactiveStreamingService {
@Override
public String publishToClient(Payload payload) {
var clientId = getClientId();
var uniqId = UUID.randomUUID().toString();
broadcast(clientId).onReceivedData(clientId, "Published");
return uniqId;
}
@Override
public List<String> getIds() {
return Collections.singletonList("Hello");
}
}
Define client receiver interface.
It is used by server and client both.
Herts receiver interface require a @HertsRpcReceiver
and extends HertsReceiver
.
import org.herts.core.annotation.HertsRpcReceiver;
import org.herts.core.service.HertsReceiver;
@HertsRpcReceiver
public interface ReactiveReceiver extends HertsReceiver {
void onReceivedData(String fromClient, String data);
}
Implementation class.
public class ReactiveReceiverImpl implements ReactiveReceiver {
@Override
public void onReceivedData(String fromClient, String data) {
System.out.println("Client: " + fromClient + ", Data: " + data);
}
}
Server side.
public class Main {
public static void main(String[] args) {
var service = new ReactiveStreamingServiceImpl();
HertsRpcServerEngine engine = HertsRpcServerEngineBuilder.builder()
.registerHertsReactiveRpcService(service) // You can register multi service
.build();
engine.start();
}
}
Client side.
public class Main {
public static void main(String[] args) {
HertsRpcClient client = HertsRpcClientBuilder
.builder("localhost")
.secure(false)
.registerHertsRpcServiceInterface(ReactiveStreamingService.class) // You can register multi service
.registerHertsRpcReceiver(new ReactiveReceiver()) // You can register multi receiver
.connect();
ReactiveStreamingService service = client.createHertsRpcService(ReactiveStreamingService.java);
Payload p = new Payload();
var res = service.publishToClient(p);
System.out.println(res);
}
}
- Herts document - Getting Started - gRPC Unary
- Herts document - Getting Started - gRPC Server Streaming
- Herts document - Getting Started - gRPC Client Streaming
- Herts document - Getting Started - gRPC Bid Streaming
- Herts document - Getting Started - gRPC Reactive Streaming
- Herts document - Getting Started - Http Service
Please contribute.
- Fork a repository.
- Make changes, commit to your fork repository.
- Send a pull request of your changes.