enquiry-consul 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc42da10dd9cb7a91a2440a8de15d06bc6c753f1
4
+ data.tar.gz: 21927fed01a768660ea6ce35a3fa74ec82c02e5d
5
+ SHA512:
6
+ metadata.gz: 418581bc7f36801e5ac0bc7d958129217eeb821ed8a9e90f79f72baf6c1ff773f9f5041af2062dd6f8bb76cf2a190a08fc2b6bb1f24a247329c74105107be848
7
+ data.tar.gz: 25a0fde0b5a853e21dac053a5e42cce46aa3be7e195bf934277927c1d61198e616db621861fc9e0489b965769d465228952049e6bd60e5b01435855c1740ad46
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Jiayuan
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # EnquiryConsul
2
+ EnquiryConsul is a very simple service discovery client, rails edition.
3
+
4
+ I haven't found any Consul client provides services as Hystrix, Load Balancing, etc. in Rails. That's why EnquiryConsul was born. However, it's now in a pretty early phrase, providing basic service discovery, load balancing, breaker functions.
5
+
6
+ ## **You are more than welcome to contribute to this project!**
7
+
8
+ Please notify me if anyone knows rounded Consul clients in Rails. :)
9
+
10
+ Please Note:
11
+ * Use it in Rails.
12
+ * Dependent on Diplomat. It's a rails http wrapper. The only wrapper I've found written in Ruby.
13
+ * Config your Consul cluster IP and port with Diplomat.
14
+ * Visit Diplomat: https://github.com/WeAreFarmGeek/diplomat
15
+ * `require 'enquiry-consul'` if necessary.
16
+
17
+ ## Installation
18
+ Add this to your Gemfile
19
+ ```
20
+ gem 'enquiry-consul'
21
+ ```
22
+
23
+ Execute
24
+ ```
25
+ bundle install
26
+ ```
27
+
28
+ ## Basic Usage
29
+ **Available methods (for now): GET, POST, PUT**
30
+ ```
31
+ EnquiryConsul.get(<YourServiceName>, <Api>, data: <Body>, reconnect: <RetryTimes>)
32
+ ```
33
+ ## **Have Fun!**
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'enquiry-consul'.freeze
3
+ s.version = '0.1.7'.freeze
4
+ s.date = '2019-06-29'.freeze
5
+ s.summary = "Early access edition".freeze
6
+ s.description = 'simple discovery service with consul'.freeze
7
+ s.authors = ["Emilio Chang".freeze]
8
+ s.email = 'zhangjiayuan93@hotmail.com'.freeze
9
+ s.files = `git ls-files`.split("\n")
10
+ s.require_paths = ["lib"]
11
+ s.license = 'MIT'.freeze
12
+ s.homepage = 'https://github.com/em1l1o/enquiry-consul'.freeze
13
+
14
+ s.add_dependency(%q<diplomat>, [">= 0"])
15
+ end
@@ -0,0 +1,35 @@
1
+ # 客户端部分,对外暴露接口,功能目前为发送基本请求
2
+ # 目前封装的方法有 get post put
3
+ # @param service_name [String]
4
+ # @param api [String]
5
+ # @param data [Hash]
6
+ # @param reconnect [Integer]
7
+ # @return [Hash]
8
+ # 用例 EnquiryConsul.post('javaPay', 'checkPayment', data: payment_params, reconnect: 5)
9
+ # TODO: (zhangjiayuan) 目前是 demo,之后会增加更多功能
10
+
11
+ require 'diplomat'
12
+
13
+ module EnquiryConsul
14
+ require 'enquiry-consul/balancer'
15
+ require 'enquiry-consul/breaker'
16
+
17
+ %w[get post put].each do |method|
18
+ define_singleton_method method do |service_name, api, data: {}, reconnect: 3|
19
+ # 从 consul 获取服务
20
+ services = Diplomat::Service.get(service_name, :all)
21
+ raise "获取服务失败" if services.blank?
22
+ # 进行负载均衡
23
+ service = Balancer.pick_service(service_name, services)
24
+ # 组装地址
25
+ uri = URI("http://#{service.ServiceAddress}:#{service.ServicePort}/#{api}")
26
+ # 生成请求
27
+ request = eval("Net::HTTP::#{method.capitalize}").new(uri)
28
+ request.set_form_data(data)
29
+ # 熔断器发送请求
30
+ response = Breaker.run(request, services, reconnect: reconnect)
31
+ # 解析响应
32
+ JSON.parse response.body
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ # 负载均衡模块
2
+ module EnquiryConsul::Balancer
3
+ module_function
4
+
5
+ def pick_service(service_name, services)
6
+ pre_index = Rails.cache.read(service_name)
7
+ next_index = pre_index && pre_index + 1 || 0
8
+ next_index = 0 if next_index >= services.size
9
+ Rails.cache.write(service_name, next_index)
10
+ services[next_index]
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ # 熔断器,主导熔断机制
2
+ module EnquiryConsul::Breaker
3
+ module_function
4
+
5
+ def run(request, services, reconnect: 3)
6
+ # 发送请求
7
+ response = Net::HTTP.start(request.uri.host, request.uri.port, read_timeout: 10) { |http| http.request request }
8
+ # TODO: (zhangjiayuan) 目前为通用判断,今后为不同情况增加不同熔断机制
9
+ raise if response.code != '200'
10
+ response
11
+ rescue
12
+ reconnect -= 1
13
+ raise "请求服务失败" if reconnect <= 0
14
+ # 重新组装请求
15
+ request = reassemble_request(request, services)
16
+ sleep(1)
17
+ retry
18
+ end
19
+
20
+ def reassemble_request(request, services)
21
+ service_name = services.first.ServiceName
22
+ service = EnquiryConsul::Balancer.pick_service(service_name, services)
23
+ uri = URI("http://#{service.ServiceAddress}:#{service.ServicePort}#{request.uri.path}")
24
+ request.instance_variable_set(:@uri, uri)
25
+ request['host'] = uri.host
26
+ request
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enquiry-consul
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ platform: ruby
6
+ authors:
7
+ - Emilio Chang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: diplomat
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
+ description: simple discovery service with consul
28
+ email: zhangjiayuan93@hotmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - LICENSE
35
+ - README.md
36
+ - enquiry-consul.gemspec
37
+ - lib/enquiry-consul.rb
38
+ - lib/enquiry-consul/balancer.rb
39
+ - lib/enquiry-consul/breaker.rb
40
+ homepage: https://github.com/em1l1o/enquiry-consul
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.6.13
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Early access edition
64
+ test_files: []