ibm-bluemix-service_discovery 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example/example.rb +46 -0
- data/ibm-bluemix-service_discovery.gemspec +36 -0
- data/lib/ibm/bluemix/service_discovery.rb +406 -0
- data/lib/ibm/bluemix/service_discovery/version.rb +7 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7378510461e0e5b3c919ea26668e5775aeb4a7a1
|
4
|
+
data.tar.gz: a0eda8ba0f69b7146ac8fe4de267d4ef8ce414d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51f655d304040a5c050001f2936251a9d6ef3c0bef73956c07e1b16d9bf9d25d7b5db7853d0590b03b447d8646b888227ed599883479704cafefae186632c456
|
7
|
+
data.tar.gz: 443c5c6adfe870f0f2fe428c758fe8522ad7c713e38508c729258ab41c3d0379fc6041f05ac9bb37c9c60fb7bc8150ee33e5c9412216ec8e99d18c57e563b6f8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at krsyoung@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Christopher Young
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# IBM::Bluemix::ServiceDiscovery
|
2
|
+
|
3
|
+
This is a Ruby Gem for interfacing with the IBM Bluemix [Service Discovery service](https://console.ng.bluemix.net/catalog/services/service-discovery/).
|
4
|
+
|
5
|
+
This Gem provides an easy to user interface to access all of the features of
|
6
|
+
Service Discovery including:
|
7
|
+
|
8
|
+
* registration
|
9
|
+
* heartbeat
|
10
|
+
* deletion
|
11
|
+
* listing
|
12
|
+
* discovery
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'ibm-bluemix-service_discovery'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install ibm-bluemix-service_discovery
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Ruby on Rails
|
33
|
+
|
34
|
+
* create an initializer, load AUTH_TOKEN from secrets
|
35
|
+
* make some config file ?
|
36
|
+
* somehow set the heartbeat frequency
|
37
|
+
* set the service name and parameters
|
38
|
+
* where to get the host and port?
|
39
|
+
|
40
|
+
### Sinatra
|
41
|
+
|
42
|
+
TODO: Write usage instructions here
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `AUTH_TOKEN='your service discovery token' rake spec` to run the tests, where the AUTH_TOKEN is a valid, working auth_token for a Service Discovery instance. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
47
|
+
|
48
|
+
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](https://rubygems.org).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ibm-bluemix-service_discovery. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
53
|
+
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ibm/bluemix/service_discovery"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/example/example.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# This is an example program that makes use of each of the endpoints defined
|
2
|
+
# by the Bluemix Service Discovery service.
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
5
|
+
require 'ibm/bluemix/service_discovery'
|
6
|
+
|
7
|
+
# TODO: you need to replace this with a working auth_token from Bluemix
|
8
|
+
AUTH_TOKEN = ENV['AUTH_TOKEN']
|
9
|
+
|
10
|
+
# create a new service discovery instance using our auth_token from the
|
11
|
+
# Bluemix Service Discovery credentials
|
12
|
+
sd = IBM::Bluemix::ServiceDiscovery.new(AUTH_TOKEN)
|
13
|
+
|
14
|
+
# register a new microservice with service discovery
|
15
|
+
# service_name: sample_service
|
16
|
+
# host: host.ibm.com
|
17
|
+
# port : 1234
|
18
|
+
# meta: empty
|
19
|
+
p "REGISTER"
|
20
|
+
reference = sd.register('sample_service', 'host.ibm.com:12345', {ttl: 45}, {})
|
21
|
+
p reference
|
22
|
+
|
23
|
+
# send a heatbeat right away
|
24
|
+
p "RENEW"
|
25
|
+
reference = sd.renew('sample_service')
|
26
|
+
p reference
|
27
|
+
|
28
|
+
# get a list that should include our newly registered service
|
29
|
+
p "LIST"
|
30
|
+
list = sd.list
|
31
|
+
p list
|
32
|
+
|
33
|
+
# discover the service
|
34
|
+
p "DISCOVER"
|
35
|
+
reference = sd.discover('sample_service')
|
36
|
+
p reference
|
37
|
+
|
38
|
+
# delete the service
|
39
|
+
p "DELETE"
|
40
|
+
reference = sd.delete('sample_service')
|
41
|
+
# p reference
|
42
|
+
|
43
|
+
# get a list that should no longer include the 'sample_service'
|
44
|
+
p "LIST"
|
45
|
+
list = sd.list
|
46
|
+
p list
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ibm/bluemix/service_discovery/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ibm-bluemix-service_discovery"
|
8
|
+
spec.version = IBM::Bluemix::ServiceDiscovery::VERSION
|
9
|
+
spec.authors = ["Christopher Young"]
|
10
|
+
spec.email = ["krsyoung@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Library for using the IBM Bluemix Service Discovery service.}
|
13
|
+
spec.description = %q{Library to support microservices interacting with the IBM Bluemix Service Discovery service. Use this library in your Rails or Sinatra based microservices to easily register and discover services in your Bluemix account.}
|
14
|
+
spec.homepage = "https://github.com/krsyoung/ibm-bluemix-service_discovery"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# # delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency 'unirest'
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "byebug"
|
36
|
+
end
|
@@ -0,0 +1,406 @@
|
|
1
|
+
require "ibm/bluemix/service_discovery/version"
|
2
|
+
require "logger"
|
3
|
+
require "unirest"
|
4
|
+
|
5
|
+
module IBM
|
6
|
+
module Bluemix
|
7
|
+
|
8
|
+
# ServiceDiscovery provides a simple interface to the IBM Bluemix Service
|
9
|
+
# Discovery service. The interface provides methods for all operations
|
10
|
+
# provided by Service Discovery as well as some helpers to make working
|
11
|
+
# with the service a little easier.
|
12
|
+
class ServiceDiscovery
|
13
|
+
# Your code goes here...
|
14
|
+
|
15
|
+
# URL for the production Bluemix Service Discovery endpoint
|
16
|
+
SERVICE_DISCOVERY_ENDPOINT = "https://servicediscovery.ng.bluemix.net/api/v1/services"
|
17
|
+
|
18
|
+
# URL for the production Bluemix Service Registry endpoint
|
19
|
+
SERVICE_REGISTRY_ENDPOINT = "https://servicediscovery.ng.bluemix.net/api/v1/instances"
|
20
|
+
|
21
|
+
# cache of registered services
|
22
|
+
@@services = {}
|
23
|
+
|
24
|
+
# cache of threads running heartbeats
|
25
|
+
@@heartbeats = {}
|
26
|
+
|
27
|
+
# dummy logger that can be overridden
|
28
|
+
class << self
|
29
|
+
attr_writer :logger
|
30
|
+
|
31
|
+
def logger
|
32
|
+
@logger ||= Logger.new($stdout).tap do |log|
|
33
|
+
log.progname = self.name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Create a new ServiceDiscovery instance.
|
39
|
+
#
|
40
|
+
# Example:
|
41
|
+
# >> sd = ServiceDiscovery.new('token...')
|
42
|
+
#
|
43
|
+
# ===Arguments
|
44
|
+
# [auth_token] (String) Valid `auth_token` from the IBM Bluemix Service Discovery service
|
45
|
+
def initialize(auth_token)
|
46
|
+
@auth_token = auth_token
|
47
|
+
|
48
|
+
# defaults
|
49
|
+
Unirest.default_header('Authorization', 'Bearer ' + @auth_token)
|
50
|
+
Unirest.default_header('Accept','application/json')
|
51
|
+
Unirest.default_header('Content-Type','application/json')
|
52
|
+
|
53
|
+
Unirest.timeout(5) # 5s timeout
|
54
|
+
end
|
55
|
+
|
56
|
+
# Register a service with Service Discovery
|
57
|
+
#
|
58
|
+
# Example:
|
59
|
+
# >> sd = ServiceDiscovery.new('token...')
|
60
|
+
# => sd.register('sample_service', '192.168.1.100:8080', { ttl: 60, heartbeat: true }, {})
|
61
|
+
#
|
62
|
+
# ===Arguments
|
63
|
+
# [service_name] (String) The name of the microservice
|
64
|
+
# [host] (String) The host and port where the microservice can be reached at
|
65
|
+
# [options] (Hash) Options, see below
|
66
|
+
# [meta] (Hash) Metadata to store with the service registration
|
67
|
+
#
|
68
|
+
# ====Options
|
69
|
+
# [heartbeat] (Boolean) Enable or disable automated heartbeat for the service
|
70
|
+
# [ttl] (Integer) Expire the service after this many seconds if a heartbeat has not been received. Hearbeat is automatically set to 30% of this value
|
71
|
+
#
|
72
|
+
# ===Returns
|
73
|
+
# resulting payload from the call to Service Discovery
|
74
|
+
#
|
75
|
+
# ===Raises
|
76
|
+
#
|
77
|
+
def register(service_name, host, options = {}, meta = {})
|
78
|
+
|
79
|
+
begin
|
80
|
+
response = Unirest.post SERVICE_REGISTRY_ENDPOINT,
|
81
|
+
parameters: {
|
82
|
+
service_name: service_name,
|
83
|
+
endpoint: {
|
84
|
+
type: 'tcp',
|
85
|
+
value: host
|
86
|
+
},
|
87
|
+
status: "UP",
|
88
|
+
ttl: options[:ttl] || 60,
|
89
|
+
metadata: meta
|
90
|
+
}.to_json
|
91
|
+
rescue Exception => e
|
92
|
+
# TODO: raise custom exception
|
93
|
+
ServiceDiscovery.logger.debug "Exception: #{e.class} #{e.message}"
|
94
|
+
return nil
|
95
|
+
end
|
96
|
+
|
97
|
+
# ServiceDiscovery.logger.debug response
|
98
|
+
|
99
|
+
if response.code != 201
|
100
|
+
#
|
101
|
+
ServiceDiscovery.logger.error response.code
|
102
|
+
end
|
103
|
+
|
104
|
+
# TODO: validate the response.body has the right keys
|
105
|
+
|
106
|
+
@@services[service_name] = response.body
|
107
|
+
|
108
|
+
# response.code # Status code
|
109
|
+
# response.headers # Response headers
|
110
|
+
# response.body # Parsed body
|
111
|
+
# response.raw_body # Unparsed body
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
# '{"service_name":"my_service", "endpoint": { "type":"tcp", "value": "host:port" }, "status":"UP", "ttl":25, "metadata":{"key":"value"}}'
|
116
|
+
|
117
|
+
# {
|
118
|
+
# "id":"6ae425dd79f1962e",
|
119
|
+
# "ttl":30,
|
120
|
+
# "links":{
|
121
|
+
# "self": "https://servicediscovery.ng.bluemix.net/api/v1/instances/6ae425dd79f1962e",
|
122
|
+
# "heartbeat": "https://servicediscovery.ng.bluemix.net/api/v1/instances/6ae425dd79f1962e/heartbeat",
|
123
|
+
# }
|
124
|
+
# }
|
125
|
+
|
126
|
+
# check if we should enable a heartbeat
|
127
|
+
if options[:heartbeat] == true
|
128
|
+
heartbeat_ttl = (options[:ttl] * 0.75).round
|
129
|
+
self.heartbeat(service_name, heartbeat_ttl)
|
130
|
+
end
|
131
|
+
|
132
|
+
return @@services[service_name]
|
133
|
+
end
|
134
|
+
|
135
|
+
# Send a heartbeat request to indicate the service is still alive and well
|
136
|
+
#
|
137
|
+
# Example:
|
138
|
+
# >> sd.renew('sample_service')
|
139
|
+
#
|
140
|
+
# ===Arguments
|
141
|
+
# [service_name] (String) The name of the microservice
|
142
|
+
#
|
143
|
+
# ===Returns
|
144
|
+
# (Boolean) `true` if the service was renewed, `false` otherwise
|
145
|
+
#
|
146
|
+
# ===Raises
|
147
|
+
#
|
148
|
+
def renew(service_name)
|
149
|
+
|
150
|
+
# error, we didn't register the service yet
|
151
|
+
return false unless @@services.has_key? service_name
|
152
|
+
|
153
|
+
service = @@services[service_name]
|
154
|
+
|
155
|
+
begin
|
156
|
+
ServiceDiscovery.logger.debug 'calling heartbeat url: ' + service['links']['heartbeat']
|
157
|
+
response = Unirest.put service['links']['heartbeat'],
|
158
|
+
headers: {
|
159
|
+
'Content-Length': 0
|
160
|
+
}
|
161
|
+
rescue Exception => e
|
162
|
+
#
|
163
|
+
end
|
164
|
+
|
165
|
+
if response.code != 200
|
166
|
+
#
|
167
|
+
# Attempting to send a heartbeat for an expired instance will result in HTTP status code 410 (Gone).
|
168
|
+
return false
|
169
|
+
end
|
170
|
+
|
171
|
+
# curl -X PUT -H "Authorization: Bearer 12o191sqk5h***" -H "Content-Length: 0" https://servicediscovery.ng.bluemix.net/api/v1/instances/6ae425dd79f1962e/heartbeat
|
172
|
+
true
|
173
|
+
end
|
174
|
+
|
175
|
+
# Set-up a separate Thread to send continuous heartbeats (renew) calls to
|
176
|
+
# indicate the service is alive.
|
177
|
+
#
|
178
|
+
# Example:
|
179
|
+
# >> sd.heartbeat('sample_service', 45)
|
180
|
+
#
|
181
|
+
# ===Arguments
|
182
|
+
# [service_name] (String) The name of the microservice
|
183
|
+
# [interval] (Integer) The number of seconds between heartbeats
|
184
|
+
#
|
185
|
+
# ===Returns
|
186
|
+
# (Boolean) `true` if the heartbeat thread was started, `false` otherwise
|
187
|
+
#
|
188
|
+
# ===Raises
|
189
|
+
#
|
190
|
+
def heartbeat(service_name, interval=60)
|
191
|
+
|
192
|
+
# kill the existing thread
|
193
|
+
unless @@heartbeats[service_name].nil?
|
194
|
+
ServiceDiscovery.logger.debug 'killing an existing heartbeat thread'
|
195
|
+
Thread.kill @@heartbeats[service_name]
|
196
|
+
end
|
197
|
+
|
198
|
+
# create a new thread that is going to run forever
|
199
|
+
@@heartbeats[service_name] = Thread.new{
|
200
|
+
while true
|
201
|
+
# TODO: how to handle errors in the thread?
|
202
|
+
ServiceDiscovery.logger.debug 'sending heartbeat'
|
203
|
+
self.renew(service_name)
|
204
|
+
sleep interval
|
205
|
+
end
|
206
|
+
}
|
207
|
+
|
208
|
+
# # something happened?
|
209
|
+
# true
|
210
|
+
end
|
211
|
+
|
212
|
+
# Stops a previously established heartbeat Thread
|
213
|
+
#
|
214
|
+
# Example:
|
215
|
+
# >> sd.unheartbeat('sample_service')
|
216
|
+
#
|
217
|
+
# ===Arguments
|
218
|
+
# [service_name] (String) The name of the microservice
|
219
|
+
#
|
220
|
+
# ===Returns
|
221
|
+
# (Boolean) `true` if the heartbeat thread was stopped, `false` otherwise
|
222
|
+
#
|
223
|
+
# ===Raises
|
224
|
+
#
|
225
|
+
def unheartbeat(service_name)
|
226
|
+
Thread.kill @@heartbeats[service_name]
|
227
|
+
end
|
228
|
+
|
229
|
+
# Deletes a service entry from Service Discovery
|
230
|
+
#
|
231
|
+
# Example:
|
232
|
+
# >> sd.delete('sample_service')
|
233
|
+
#
|
234
|
+
# ===Arguments
|
235
|
+
# [service_name] (String) The name of the microservice
|
236
|
+
#
|
237
|
+
# ===Returns
|
238
|
+
# (Boolean) `true` if the service was removed, `false` otherwise
|
239
|
+
#
|
240
|
+
# ===Raises
|
241
|
+
#
|
242
|
+
def delete(service_name)
|
243
|
+
|
244
|
+
# error, we didn't register the service yet
|
245
|
+
return false unless @@services.has_key? service_name
|
246
|
+
|
247
|
+
service = @@services[service_name]
|
248
|
+
ServiceDiscovery.logger.debug 'Deleting: ' + service.to_s
|
249
|
+
|
250
|
+
begin
|
251
|
+
response = Unirest.delete service['links']['self']
|
252
|
+
rescue Exception => e
|
253
|
+
#
|
254
|
+
ServiceDiscovery.logger.debug "Exception: #{e.class} #{e.message}"
|
255
|
+
end
|
256
|
+
|
257
|
+
if response.code != 200
|
258
|
+
#
|
259
|
+
# Attempting to send a heartbeat for an expired instance will result in HTTP status code 410 (Gone).
|
260
|
+
end
|
261
|
+
@@services.delete service_name
|
262
|
+
true
|
263
|
+
end
|
264
|
+
|
265
|
+
# Resets the state of this service. This includes stopping any existing
|
266
|
+
# heartbeat Threads as well as deleting all registered services.
|
267
|
+
#
|
268
|
+
# Example:
|
269
|
+
# >> sd.reset
|
270
|
+
#
|
271
|
+
# ===Arguments
|
272
|
+
# +none+
|
273
|
+
#
|
274
|
+
# ===Returns
|
275
|
+
# (Boolean) `true` if the reset was successful, `false` otherwise
|
276
|
+
#
|
277
|
+
# ===Raises
|
278
|
+
#
|
279
|
+
def reset
|
280
|
+
|
281
|
+
# cancel any threads
|
282
|
+
@@heartbeats.keys.each do |k|
|
283
|
+
self.unheartbeat(k)
|
284
|
+
end
|
285
|
+
|
286
|
+
# delete all of the services
|
287
|
+
@@services.keys.each do |k|
|
288
|
+
self.delete(k)
|
289
|
+
end
|
290
|
+
|
291
|
+
true
|
292
|
+
end
|
293
|
+
|
294
|
+
# Returns information about the current local state of Service Discovery.
|
295
|
+
# This includes information about the services and the heartbeats.
|
296
|
+
#
|
297
|
+
# Example:
|
298
|
+
# >> sd.info
|
299
|
+
#
|
300
|
+
# ===Arguments
|
301
|
+
# +none+
|
302
|
+
#
|
303
|
+
# ===Returns
|
304
|
+
# (Hash) { services: ['s1', 's2'], heartbeats: ['s1'] }
|
305
|
+
#
|
306
|
+
# ===Raises
|
307
|
+
#
|
308
|
+
def info
|
309
|
+
{ services: @@services.keys, heartbeats: heartbeats.keys }
|
310
|
+
end
|
311
|
+
|
312
|
+
# Get the current list of registered services within Service Discovery
|
313
|
+
#
|
314
|
+
# Example:
|
315
|
+
# >> sd.list
|
316
|
+
#
|
317
|
+
# ===Arguments
|
318
|
+
# +none+
|
319
|
+
#
|
320
|
+
# ===Returns
|
321
|
+
# (Hash) { services: ['s1', 's2'] }
|
322
|
+
#
|
323
|
+
# ===Raises
|
324
|
+
#
|
325
|
+
def list
|
326
|
+
|
327
|
+
# curl -X GET -H "Authorization: bearer 12o191sqk5h***"https://servicediscovery.ng.bluemix.net/api/v1/services
|
328
|
+
|
329
|
+
begin
|
330
|
+
response = Unirest.get SERVICE_DISCOVERY_ENDPOINT
|
331
|
+
rescue Exception => e
|
332
|
+
#
|
333
|
+
ServiceDiscovery.logger.debug "Exception: #{e.class} #{e.message}"
|
334
|
+
end
|
335
|
+
|
336
|
+
if response.code != 200
|
337
|
+
#
|
338
|
+
# Attempting to send a heartbeat for an expired instance will result in HTTP status code 410 (Gone).
|
339
|
+
end
|
340
|
+
|
341
|
+
response.body
|
342
|
+
end
|
343
|
+
|
344
|
+
# Discovers the connection information for a given microservice.
|
345
|
+
#
|
346
|
+
# Example:
|
347
|
+
# >> sd.discover('sample_service')
|
348
|
+
#
|
349
|
+
# ===Arguments
|
350
|
+
# [service_name] (String) The name of the microservice
|
351
|
+
#
|
352
|
+
# ===Returns
|
353
|
+
# (Hash)
|
354
|
+
# {
|
355
|
+
# "service_name":"my_service",
|
356
|
+
# "instances":[
|
357
|
+
# {
|
358
|
+
# "endpoint":
|
359
|
+
# {
|
360
|
+
# "type": "tcp",
|
361
|
+
# "value": "192.168.1.32:80",
|
362
|
+
# },
|
363
|
+
# "status": "UP",
|
364
|
+
# "last_heartbeat": "2015-05-01T08:28:06.801064+02:00",
|
365
|
+
# "metadata": {"key":"value"}
|
366
|
+
# }]
|
367
|
+
# }
|
368
|
+
#
|
369
|
+
# ===Raises
|
370
|
+
#
|
371
|
+
def discover(service_name)
|
372
|
+
# curl -X GET -H "Authorization: bearer 12o191sqk5h***" https://servicediscovery.ng.bluemix.net/api/v1/services/my_service
|
373
|
+
|
374
|
+
# {
|
375
|
+
# "service_name":"my_service",
|
376
|
+
# "instances":[
|
377
|
+
# {
|
378
|
+
# "endpoint":
|
379
|
+
# {
|
380
|
+
# "type": "tcp",
|
381
|
+
# "value": "192.168.1.32:80",
|
382
|
+
# },
|
383
|
+
# "status": "UP",
|
384
|
+
# "last_heartbeat": "2015-05-01T08:28:06.801064+02:00",
|
385
|
+
# "metadata": {"key":"value"}
|
386
|
+
# }]
|
387
|
+
# }
|
388
|
+
|
389
|
+
begin
|
390
|
+
response = Unirest.get SERVICE_DISCOVERY_ENDPOINT + '/' + service_name
|
391
|
+
rescue Exception => e
|
392
|
+
#
|
393
|
+
ServiceDiscovery.logger.debug "Exception: #{e.class} #{e.message}"
|
394
|
+
end
|
395
|
+
|
396
|
+
if response.code != 200
|
397
|
+
#
|
398
|
+
# Attempting to send a heartbeat for an expired instance will result in HTTP status code 410 (Gone).
|
399
|
+
end
|
400
|
+
|
401
|
+
response.body
|
402
|
+
end
|
403
|
+
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ibm-bluemix-service_discovery
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christopher Young
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: unirest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Library to support microservices interacting with the IBM Bluemix Service
|
84
|
+
Discovery service. Use this library in your Rails or Sinatra based microservices
|
85
|
+
to easily register and discover services in your Bluemix account.
|
86
|
+
email:
|
87
|
+
- krsyoung@gmail.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- example/example.rb
|
103
|
+
- ibm-bluemix-service_discovery.gemspec
|
104
|
+
- lib/ibm/bluemix/service_discovery.rb
|
105
|
+
- lib/ibm/bluemix/service_discovery/version.rb
|
106
|
+
homepage: https://github.com/krsyoung/ibm-bluemix-service_discovery
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.5.1
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Library for using the IBM Bluemix Service Discovery service.
|
130
|
+
test_files: []
|