An open API service indexing awesome lists of open source software.

https://github.com/huntlabs/hunt-service

Distributed RPC framework (micro-service) for DLang based on gRPC and neton.
https://github.com/huntlabs/hunt-service

distributed micro-service rpc rpc-client rpc-server rpc-service

Last synced: 3 months ago
JSON representation

Distributed RPC framework (micro-service) for DLang based on gRPC and neton.

Awesome Lists containing this project

README

          

## Getting started

The following code snippet comes from [Hunt-service examples](https://github.com/huntlabs/hunt-service). You may clone the sample project.

```bash
# git clone https://github.com/huntlabs/hunt-service.git
# cd examples
```

### Define rpc service

```D
syntax = "proto3";

package example.api;

service Hello {

rpc sayHello(HelloRequest) returns (HelloResponse) {

}

}

message HelloRequest{
string name = 1;
}

message HelloResponse{
string echo = 1;
}
```

*See [example/proto/example.proto](https://github.com/huntlabs/hunt-service/blob/master/examples/proto/example.proto) on GitHub.*

### Implement service interface for the provider

```D
module HelloService;

import example.api.examplerpc;
import example.api.example;

import grpc;

class HelloService : HelloBase
{
override Status sayHello(HelloRequest req, ref HelloResponse resp)
{
resp.echo = "hello ," ~ req.name;
return Status.OK;
}

}
```

*See [provider/source/HelloService.d](https://github.com/huntlabs/hunt-service/blob/master/examples/provider/source/HelloService.d) on GitHub.*

### Start service provider

```D
NetUtil.startEventLoop();

auto providerFactory = new ServiceProviderFactory("127.0.0.1",7788);

// If you use a registry, use the following options
// RegistryConfig conf = {"127.0.0.1",50051,"example.provider"};
// providerFactory.setRegistry(conf);

providerFactory.addService(new HelloService());
providerFactory.start();
```

*See [provider/source/app.d](https://github.com/huntlabs/hunt-service/blob/master/examples/provider/source/app.d) on GitHub.*

### Build and run the provider

```bash
# cd provide
# dub run
```

### Call remote service in consumer

```D
NetUtil.startEventLoop();

auto invokerFactory = new ServiceInvokerFactory();

// If you use a registry, use the following options
// RegistryConfig conf = {"127.0.0.1",50051,"example.provider"};
// invokerFactory.setRegistry(conf);

invokerFactory.setDirectUrl("tcp://127.0.0.1:7788");
auto client = invokerFactory.createClient!(HelloClient)();
assert(client !is null);

HelloRequest req = new HelloRequest();

foreach(num; 1 .. 10)
{
req.name = to!string(num);

auto res = client.sayHello(req);

writeln("response : ", res.echo);
}
```

### Build and run the consumer

```bash
# cd consumer
# dub run
```

*See [consumer/source/app.d](https://github.com/huntlabs/hunt-service/blob/master/examples/consumer/source/app.d) on GitHub.*