Unified gRPC/HTTP Realtime API framework for Java.
![herts](https://private-user-images.githubusercontent.com/9509132/250916608-f1717514-a403-4597-949f-96c5820f9894.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MTE1NDQsIm5iZiI6MTcyNDcxMTI0NCwicGF0aCI6Ii85NTA5MTMyLzI1MDkxNjYwOC1mMTcxNzUxNC1hNDAzLTQ1OTctOTQ5Zi05NmM1ODIwZjk4OTQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjZUMjIyNzI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MTZlYTAwYjVjZjRkZTRlNTUwMGYwZTk1YmM1MzYxNjM2Nzc5MDY5OWEzNGMwOGJiMzJmZDJiNGNlNzgwZWRlYSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.eXiKb4zwwLoZiXbCZNsm_dNeIyxhKo_XcrVPrkB21Os)
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.