Skip to content

huntlabs/hunt-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting started

The following code snippet comes from Hunt-service examples. You may clone the sample project.

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

Define rpc service

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 on GitHub.

Implement service interface for the provider

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 on GitHub.

Start service provider

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 on GitHub.

Build and run the provider

# cd provide
# dub run

Call remote service in consumer

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

# cd consumer
# dub run

See consumer/source/app.d on GitHub.