tsloughter / grpcbox

Erlang grpc on chatterbox
Apache License 2.0
140 stars 62 forks source link

How to create <service>_client.erl ? #5

Closed minghu6 closed 6 years ago

minghu6 commented 6 years ago

I have read the README about https://github.com/tsloughter/grpcbox#using-a-service-client. However, When I run rebar3 gprc gen on the root of my project, it only creates*_pb.erl and*_bhvr.erl. So, there is any other steps to generate <service>_client.erl ?

tsloughter commented 6 years ago

Ah, sorry, I hadn't merged the plugin client branch to master.

I've pushed to master now so upgrading the plugin should result in the client being generated.

minghu6 commented 6 years ago

en, I edit rebar.config {grpcbox_plugin, {git, "https://github.com/tsloughter/grpcbox.git", {branch, "master"}}}, unlock and recompile, but it seems that no new version to upgrade?

tsloughter commented 6 years ago

It is {grpcbox_plugin, {git, "https://github.com/tsloughter/grpcbox_plugin.git", {branch, "master"}}}

But also plugins aren't locked, you want rebar3 plugins upgrade grpcbox_plugin or just blow away the _build/default/plugins dir.

minghu6 commented 6 years ago

When I run rebar3 plugins upgrade grpcbox_plugin, it prints:

===> Fetching chatterbox ({git,
                                  "https://github.com/tsloughter/chatterbox",
                                  {ref,
                                   "0d8c06bf23ec65a6a500337d25e856b850a7d711"}})

But where is commit 0d8c06bf23ec65a6a500337d25e856b850a7d711?

tsloughter commented 6 years ago

No reason it shouldn't find that, it is definitely there: https://github.com/tsloughter/chatterbox/commit/0d8c06bf23ec65a6a500337d25e856b850a7d711

tsloughter commented 6 years ago

Because I haven't got the chatterbox changes merged to master and pushed to hex this was in a sort of state of flux. Once that is done I'll publish new releases known to work together.

Though from master for both grpcbox and the plugin they should work together.

minghu6 commented 6 years ago

Yes, i only seach the master branch...

After upgrade the plugin, rebar3 grpc gen did generate *_client.erl file!

but another issue is

===> Compiling src/hellostreamingworld_multi_greeter_client.erl failed
src/hellostreamingworld_multi_greeter_client.erl:13: can't find include file "grpcbox.hrl"

when rebar3 compile

(I can found this hrl file in the ./_build/default/lib/grpcbox/include)

minghu6 commented 6 years ago

If i skip the can't find "grpcbox.hrl" file issue (by copy the grpcbox.hrl into the project's include folder)

I run rebar3 shell

1> hellostreamingworld_multi_greeter_client:say_hello(ctx:new(), #{name => "Woc", num_greetings => 3}, #{}).

** exception error: bad argument
     in function  ets:lookup/2
        called as ets:lookup(gproc,{{p,l,{gproc_pool,default_channel}},shared})
     in call from gproc:get_value1/2 (/home/johnzhuang/coding/erl-repos/myapp/_build/default/lib/gproc/src/gproc.erl, line 1602)
     in call from gproc:get_value/2 (/home/johnzhuang/coding/erl-repos/myapp/_build/default/lib/gproc/src/gproc.erl, line 1584)
     in call from gproc_pool:pick_worker/1 (/home/johnzhuang/coding/erl-repos/myapp/_build/default/lib/gproc/src/gproc_pool.erl, line 314)
     in call from grpcbox_channel:pick/2 (/home/johnzhuang/coding/erl-repos/myapp/_build/default/lib/grpcbox/src/grpcbox_channel.erl, line 47)
     in call from grpcbox_client:stream/5 (/home/johnzhuang/coding/erl-repos/myapp/_build/default/lib/grpcbox/src/grpcbox_client.erl, line 121)

hellostreamingworld.proto

syntax = "proto3";

package hellostreamingworld;

// The greeting service definition.
service MultiGreeter {
  // Sends multiple greetings
  rpc sayHello (HelloRequest) returns (stream HelloReply) {}
}

// The request message containing the user's name and how many greetings
// they want.
message HelloRequest {
  string name = 1;
  string num_greetings = 2;
}

// A response message containing a greeting
message HelloReply {
  string message = 1;
}

And there is a sys.config in config folder of project, with

[
  { myapp, [{client, #{channels => [{default_channel, [{http, "localhost", 50051, []}], #{}}]}}]}
].

What's the problem ?

tsloughter commented 6 years ago

Hm, that error (I should make it better and will) is that the channel isn't found.

Are you sure the server is listening?

minghu6 commented 6 years ago

Yes, the server is listening, I am using google grpc python demo server, and I have tested it with grpc_client.

Maybe I have mistake on setting channel? If I set channel manually like below helloworld_greeter_client:say_hello(ctx:new(), #{name => "Joe"}, #{channel => [{http, "localhost", 50051, []}]}). That's OK?

tsloughter commented 6 years ago

Na, the channel needs to be started and is references an atom that represents the channel's pool and is used to pick a subchannel:

gproc_pool:pick_worker(Name)

Though first i'd verify that in the shell application:get_env(grpcbox, client) returns what you'd expect based on your config.

Maybe your config isn't being used? Or grpcbox isn't being started when you run shell? You can do rebar3 shell --config config/sys.config or set that in your rebar.config under the {shell, [...]}

minghu6 commented 6 years ago

Very interesting, when I first application:get_env(grpcbox, client), it returns undefined. Then I add grpcbox onto the apps, now my

rebar.config

{erl_opts, [debug_info]}.
{deps, [{grpcbox, {git, "https://github.com/tsloughter/grpcbox.git", {branch, "master"}}}]}.

{grpc, [{protos, "priv/protos"},
        {gpb_opts, [{module_name_suffix, "_pb"}]}]}.

{plugins, [{grpcbox_plugin, {git, "https://github.com/tsloughter/grpcbox_plugin.git", {branch, "master"}}}]}.
{shell, [
    {config, "config/sys.config"},
    {apps, [myapp, grpcbox]}
]}.

But when i re-run application:get_env(grpcbox, client) (in rebar3 shell --config config/sys.config), It returns {ok,#{channels => []}}

config/sys.cionfig

[
  { myapp, 
    [
      {client, #{channels => [{default_channel, [{http, "localhost", 50051, []}], #{}}]}},
      {pool_opts, #{size => 10}},
      {grpc_opts, #{service_protos => [helloworld_pb, hellostreamingworld_pb]}}
    ]
  }
].

It seems like not load client config, and why

tsloughter commented 6 years ago

Oooh, I should have noticed this the first time you posted your sys config.

You need to put the options for grpc under grpcbox, so:

[
  { grpcbox, 
    [
      {client, #{channels => [{default_channel, [{http, "localhost", 50051, []}], #{}}]}},
      {pool_opts, #{size => 10}},
      {grpc_opts, #{service_protos => [helloworld_pb, hellostreamingworld_pb]}}
    ]
  }
].
minghu6 commented 6 years ago

That's it! I mistakenly think the options is for myapp... After recorrected it, I can get channel config, and get response from grpc server. Thank you very much!

tsloughter commented 6 years ago

Great!