Module: EnquiryConsul::Breaker
- Defined in:
- lib/enquiry-consul/breaker.rb
Overview
熔断器,主导熔断机制
Class Method Summary collapse
Class Method Details
.reassemble_request(request, services) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/enquiry-consul/breaker.rb', line 20 def reassemble_request(request, services) service_name = services.first.ServiceName service = EnquiryConsul::Balancer.pick_service(service_name, services) uri = URI("http://#{service.ServiceAddress}:#{service.ServicePort}#{request.uri.path}") request.instance_variable_set(:@uri, uri) request['host'] = uri.host request end |
.run(request, services, reconnect: 3) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/enquiry-consul/breaker.rb', line 5 def run(request, services, reconnect: 3) # 发送请求 response = Net::HTTP.start(request.uri.host, request.uri.port, read_timeout: 10) { |http| http.request request } # TODO: (zhangjiayuan) 目前为通用判断,今后为不同情况增加不同熔断机制 raise if response.code != '200' response rescue reconnect -= 1 raise "请求服务失败" if reconnect <= 0 # 重新组装请求 request = reassemble_request(request, services) sleep(1) retry end |