1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-11-06 08:29:15 +02:00

Add examples

This commit is contained in:
Asim Aslam
2020-12-26 15:17:20 +00:00
parent 273bab5dd7
commit a34c70de0e
320 changed files with 20803 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'json-rpc-objects'
gem 'json-rpc-objects-json'

View File

@@ -0,0 +1,84 @@
# Ruby Greeter Server
This is an example of how to run a ruby service within the Micro world.
## Simple RPC
examples/greeter/server/ruby/rpc_server.rb runs on localhost:8080 and responds to JSON RPC requests
examples/greeter/client/ruby/rpc_client.rb demonstrates a client request to this service
rpc_server.rb
```ruby
server = WEBrick::HTTPServer.new :Port => 8080
server.mount_proc '/' do |req, res|
request = JsonRpcObjects::Request::parse(req.body)
response = request.class::version.response::create({:msg => "hello " + request.params[0]["name"]})
res.body = response.to_json
end
trap 'INT' do server.shutdown end
server.start
```
rpc_client.rb
```ruby
uri = URI("http://localhost:8080")
method = "Say.Hello"
request = {:name => "John"}
# create request
req = JsonRpcObjects::Request::create(method, [request], :id => 1)
# do request
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.content_type = 'application/json'
request.body = req.to_json
# parse response
puts JsonRpcObjects::Response::parse(http.request(request).body).result["msg"]
```
## Sidecar Registration
By registering with discovery using the sidecar, other services can find and query your service.
An example server can be found at examples/greeter/server/ruby/sidecar_server.rb
An example client can be found at examples/greeter/client/ruby/sidecar_client.rb
### Run the proxy
```shell
$ go get github.com/micro/micro
$ micro proxy
```
### Register
```ruby
# service definition
service = {
"name" => service,
"nodes" => [{
"id" => service + "-" + SecureRandom.uuid,
"address" => host,
"port" => port
}]
}
# registration url
register_uri = URI("http://localhost:8081/registry")
# register service
http = Net::HTTP.new(register_uri.host, register_uri.port)
request = Net::HTTP::Post.new(register_uri.request_uri)
request.content_type = 'application/json'
request.body = service.to_json
http.request(request)
# degister service
request = Net::HTTP::Delete.new(register_uri.request_uri)
request.content_type = 'application/json'
request.body = service.to_json
http.request(request)
```

View File

@@ -0,0 +1,20 @@
require 'webrick'
require 'json-rpc-objects/request'
require 'json-rpc-objects/response'
# JSON RPC Server
#
# An example service ruby.micro.srv.greeter
server = WEBrick::HTTPServer.new :Port => 8080
server.mount_proc '/' do |req, res|
request = JsonRpcObjects::Request::parse(req.body)
response = request.class::version.response::create({:msg => "hello " + request.params[0]["name"]})
res.body = response.to_json
end
trap 'INT' do server.shutdown end
server.start

View File

@@ -0,0 +1,53 @@
require 'webrick'
require 'securerandom'
require 'net/http'
require 'json'
require 'json-rpc-objects/request'
require 'json-rpc-objects/response'
# Sidecar JSON RPC Server
#
# An example service ruby.micro.srv.greeter
# Registers with the sidecar
register_uri = URI("http://localhost:8081/registry")
service = "ruby.micro.srv.greeter"
method = "Say.Hello"
host = "127.0.0.1"
port = 8080
server = WEBrick::HTTPServer.new :Port => port
server.mount_proc '/' do |req, res|
request = JsonRpcObjects::Request::parse(req.body)
response = request.class::version.response::create({:msg => "hello " + request.params[0]["name"]}, nil, :id => request.id)
res.body = response.to_json
end
trap 'INT' do server.shutdown end
req = {
"name" => service,
"nodes" => [{
"id" => service + "-" + SecureRandom.uuid,
"address" => host,
"port" => port
}]
}
# register service
http = Net::HTTP.new(register_uri.host, register_uri.port)
request = Net::HTTP::Post.new(register_uri.request_uri)
request.content_type = 'application/json'
request.body = req.to_json
http.request(request)
server.start
# degister service
request = Net::HTTP::Delete.new(register_uri.request_uri)
request.content_type = 'application/json'
request.body = req.to_json
http.request(request)