Skip to content

Latest commit

 

History

History
301 lines (235 loc) · 12.8 KB

README.md

File metadata and controls

301 lines (235 loc) · 12.8 KB

Herts

build End 2 end test Release Apache License Doc herts-core

Unified gRPC/HTTP Realtime API framework for Java.

herts

Document

Herts Document

About

Code base Streaming

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 .

Easy to load balancing

Loadbalancing is easily performed with functions supported by Herts.
There are multiple OSS to support.

Original Streaming Interface

Herts support original bidirectional streaming.
It enables two-way communication with a simple interface. Also, this communication method can be easily load balanced.

herts

Support Service Type

  • gRPC Unary
  • gRPC Client Streaming
  • gRPC Server Streaming
  • gRPC Bidirectional Streaming
  • gRPC Herts Reactive Steaming
  • HTTP API

Requirements

Server-side

  • Java 11+
  • org.hertsstack, io.grpc packages

Client-side

  • Java 11+
  • org.hertsstack, io.grpc packages

Maven Central

Package Maven
herts-codegen herts-core
herts-core herts-core
herts-rpc herts-rpc
herts-rpc-client herts-rpc-client
herts-http herts-http
herts-http-client herts-http-client
herts-broker herts-broker
herts-broker-local herts-broker-local
herts-broker-redis herts-broker-redis
herts-metrics herts-metrics
herts-gateway herts-metrics

Getting Started

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;
    }
}

Herts Unary sample

Define Service

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");
    }
}

Start Server/Client

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);
    }
}

Herts Reactive Streaming sample

Define Server Service

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

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);
    }
}

Start Server/Client

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);
    }
}

All Examples

Automatic create HTTP Client Code for Typescript

CodeGen for Typescript

Contributer Guide

Please contribute.

  • Fork a repository.
  • Make changes, commit to your fork repository.
  • Send a pull request of your changes.

License

Apache-2.0