Skip to content
Browse files

Register with router to allow multiple gateways for load balancing

Change-Id: Ib90b47d440bd3ecacc14f0ac404a631118d9f92f
  • Loading branch information...
1 parent 56a9658 commit 77510c6116a71e21c43e782d32aa2b5671f47091 Harshawardhan Gadgil committed Aug 31, 2012
View
1 marketplace/Gemfile
@@ -3,7 +3,6 @@ source :rubygems
gem 'eventmachine', :git => 'git://github.com/cloudfoundry/eventmachine.git', :branch => 'release-0.12.11-cf'
gem "em-http-request"
gem "ruby-hmac"
-gem "dm-core", "= 1.1.0"
gem "sinatra", "~> 1.2.3"
gem "oauth"
gem "json", "~> 1.4.6"
View
1 marketplace/Gemfile.lock
@@ -200,7 +200,6 @@ PLATFORMS
DEPENDENCIES
ci_reporter
- dm-core (= 1.1.0)
em-http-request
eventmachine!
json (~> 1.4.6)
View
4 marketplace/bin/marketplace_gateway
@@ -13,9 +13,6 @@ require 'base/marketplace_async_gateway'
class VCAP::Services::Marketplace::Gateway < VCAP::Services::Base::Gateway
- def additional_options
- end
-
def async_gateway_class
VCAP::Services::Marketplace::MarketplaceAsyncServiceGateway
end
@@ -55,7 +52,6 @@ class VCAP::Services::Marketplace::Gateway < VCAP::Services::Base::Gateway
@config[:host] = VCAP.local_ip(@config[:ip_route])
@config[:port] ||= VCAP.grab_ephemeral_port
- @config[:url] = "http://#{@config[:host]}:#{@config[:port]}"
EM.run do
sg = async_gateway_class.new(@config)
View
1 marketplace/config/marketplace_gateway.yml
@@ -16,4 +16,5 @@ marketplace: appdirect
acls: [ "*@example.com" ]
+external_uri: appdirect-mpgw.vcap.me
refresh_interval: 300
View
47 marketplace/lib/base/marketplace_async_gateway.rb
@@ -1,13 +1,13 @@
# Copyright (c) 2009-2012 VMware, Inc.
require 'fiber'
-require 'dm-types'
+require 'nats/client'
module VCAP
module Services
module Marketplace
class MarketplaceAsyncServiceGateway < VCAP::Services::AsynchronousServiceGateway
- REQ_OPTS = %w(mbus url token cloud_controller_uri).map {|o| o.to_sym}
+ REQ_OPTS = %w(mbus external_uri token cloud_controller_uri).map {|o| o.to_sym}
set :raise_errors, Proc.new {false}
set :show_exceptions, false
@@ -39,6 +39,7 @@ def setup(opts)
@host = opts[:host]
@port = opts[:port]
+ @external_uri = opts[:external_uri]
@node_timeout = opts[:node_timeout]
@logger = opts[:logger] || make_logger()
@token = opts[:token]
@@ -52,6 +53,13 @@ def setup(opts)
@marketplace_client = load_marketplace(opts)
+ @router_register_json = {
+ :host => @host,
+ :port => @port,
+ :uris => [ @external_uri ],
+ :tags => {:components => "#{@marketplace_client.name}MarketplaceGateway" }
+ }.to_json
+
@catalog = {}
token_hdrs = VCAP::Services::Api::GATEWAY_TOKEN_HEADER
@@ -72,11 +80,12 @@ def setup(opts)
refresh_catalog_and_update_cc
end
- ##### Start up
- @ready_to_serve = false
+ f = Fiber.new do
+ start_nats(opts[:mbus])
+ end
+ f.resume
refresh_catalog_and_update_cc
- @ready_to_serve = true
end
error [JsonMessage::ValidationError, JsonMessage::ParseError] do
@@ -116,21 +125,45 @@ def advertise_services(active=true)
end
end
+ def start_nats(uri)
+ f = Fiber.current
+ @nats = NATS.connect(:uri => uri) do
+ on_connect_nats;
+ f.resume
+ end
+ Fiber.yield
+ end
+
+ def on_connect_nats()
+ @logger.info("Register #{@marketplace_client.name} marketplace gateway: #{@router_register_json}")
+ @nats.publish('router.register', @router_register_json)
+ @router_start_channel = @nats.subscribe('router.start') {
+ @nats.publish('router.register', @router_register_json)
+ }
+ end
+
+ def stop_nats()
+ @nats.unsubscribe(@router_start_channel) if @router_start_channel
+ @logger.debug("Unregister #{@marketplace_client.name} marketplace gateway: #{@router_register_json}")
+ @nats.publish("router.unregister", @router_register_json)
+ @nats.close
+ end
+
def on_exit(stop_event_loop=true)
- @ready_to_serve = false
@refresh_timer.cancel
Fiber.new {
# Since the services are not being stored locally
refresh_catalog
advertise_services(false)
+ stop_nats
EM.stop if stop_event_loop
}.resume
end
#################### Handlers ###################
get "/" do
- return {"offerings" => @catalog, "ready_to_serve" => @ready_to_serve}.to_json
+ return {"marketplace" => @marketplace_client.name, "offerings" => @catalog}.to_json
end
# Provision a marketplace service
View
5 marketplace/lib/marketplaces/appdirect/appdirect_marketplace.rb
@@ -1,6 +1,5 @@
# Copyright (c) 2009-2012 VMware, Inc.
require 'fiber'
-require 'dm-types'
require 'service_error'
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
@@ -19,7 +18,7 @@ def initialize(opts)
super(opts)
@logger = opts[:logger]
- @url = opts[:url]
+ @external_uri = "http://#{opts[:external_uri]}"
@node_timeout = opts[:node_timeout]
@acls = opts[:acls]
@helper = AppdirectHelper.new(opts, @logger)
@@ -52,7 +51,7 @@ def generate_cc_advertise_request(name, bsvc, active = true)
req[:acls][:users] = acls
end
- req[:url] = @url
+ req[:url] = @external_uri
if bsvc["plans"] and bsvc["plans"].count > 0
req[:plans] = []

0 comments on commit 77510c6

Please sign in to comment.
Something went wrong with that request. Please try again.