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
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.
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.
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.
# cd provide
# dub run
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);
}
# cd consumer
# dub run
See consumer/source/app.d on GitHub.