Yrpc

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/yrpc. To experiment with that code, run bin/console for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

Installation

Add this line to your application's Gemfile:

gem 'yrpc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install yrpc

Usage

云深rpc 针对grpc的一次封装开发,类rails 开发模式

项目结构:server 端

app
 --controllers
    --controller..

首先使用 yrpc-cli 生成我们的proto_buff 文件

$ yrpc-cli ./protos lib

例子在 yrpc-test 项目中

生成 app/controllers 目录

$ yrpc-cli -g 

添加第一个ruby controller


class DemoServer < Yrpc::Controllers::Base
  bind ::Demo::Demoer::Service
  before_action [:test_invoke, :error]

  def say_hello
    Demo::HelloReply.new(message: "Hello #{request.message.name}")
  end

  def test_invoke
    request.message.name = "asd"
  end

  def error
    request.("mesasge")
  end

end

首先使用bind 将我们的生成的grpc service 绑定,

 bind ::Demo::Demoer::Service

SayHello.underscore 为我们需要覆写的方法。

say_hello #方法override

controller 带过滤器

before_action

after_action 

controller 已经编写好 接下来创建第一个yrpc server

yrpc-server.rb

require 'yrpc'
require './lib/helloworld_services_pb'

Yrpc.configure do |y|
  y.server_options = {}
  y.controllers_path = 'app/controllers'
end
#
Yrpc::Executor.new.run

默认开启的端口为 9001 可以自己进行配置

-> y.server_ binding_ url:

配置了controller_path 就可以自动load 所有controller了

现在编写client 试试我们的 server 是否生效

yrpc-client.rb

require 'yrpc'
require './lib/helloworld_services_pb'

def main
  begin
    client = ::Yrpc::Client.new(service: Demo::Demoer, options: {hostname: "localhost:9001"})
    response = client.invoke(:SayHello, name: "sad")
    message = response.message.status
    p "Greeting: #{message.code}"
  rescue => e
    p e
  end
end

main

注意! 这里同样需要引用到我们的service类 客户端和服务端需要共同引入

规范

遵循大部分云深response 规范的protocal 文件格式应该为


service demoer {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {
    }
}

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings
message HelloReply {
    Status status = 1;
}

message Status {
    string message = 1;
    string code = 2;
}


Hello Reply 应当带一个nest type

即 返回的时候 带 data.status.code ,data.status.message

即通过code 判断接口是否成功

相关资料

GRPC

Protocal Language

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yrpc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Yrpc project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.