class Zoo
Public: Create a Zk instence and connect, the following relaying on it, it will block until connected successfully.
options - The import hash include zk_address
Examples
Zoo.new({ :zk_address => "192.168.1.172:2181" }) Zoo.new({ zk_address: "192.168.1.172:2181" }) # => nil
Returns a Zoo
instance.
Constants
- SERVER_FOUND_BASE_PATH
- SERVER_TYPE_HTTP
- SERVER_TYPE_THRIFT
Public Class Methods
# File lib/reminder-client/configcenter.rb, line 26 def initialize(options) p "start connect to #{options[:zk_address]}" @zk = ZK.new(options[:zk_address]) # @config_center_backend = options[:config_center_backend] @parser = Yajl::Parser @encoder = Yajl::Encoder puts "zk successfully connected to #{options[:zk_address]}" end
Public Instance Methods
Public: The serverConfig client part
config_path - The config_path of zk tobe watched. group - The group of the service block - The callback to be call when the config_path to be watched changed.
under development
Returns nil.
# File lib/reminder-client/configcenter.rb, line 106 def server_config_client(config_path, group, &block) puts "zk server_config start watching on #{config_path}" # get server config for the first time version = @zk.get(config_path, :watch => true)[0] version = -1 if version.empty? version = version.to_i # call hessian to get the config data configdata = REMINDER_BACKEND_SERVICE.getMapdata(version, config_path, group) puts "get version for the first time configdata is #{configdata}" block.call(configdata) # register the config path @zk.register(config_path) do |event| puts "serve config event is #{event}" version = @zk.get(config_path, :watch => true)[0] if event.node_changed? version = -1 if version.empty? version = version.to_i puts "server config version is #{version}" # get the config data by version and service through hessian configdata = REMINDER_BACKEND_SERVICE.getMapdata(version, config_path, group) block.call(configdata) end end end
Public: server found client part.
service_type - group_path - The String (zk’s path) to be watched. block - The callback to be call when the server_path to be watched changed.
# File lib/reminder-client/configcenter.rb, line 67 def server_found_client(service_type, group_path, &block) watch_path = get_combined_path([SERVER_FOUND_BASE_PATH, service_type, group_path]) puts "zk server_found start watching on #{watch_path}" relist = @zk.children(watch_path, :watch => true) # relist = @zk.children(server_path) load_balance = @zk.get(watch_path, :watch => true)[0] puts "get children #{relist} and get load_balance #{load_balance} for the first time" #context[:serverlist => relist] #block.call(get_ip_from_server(server_path, relist), load_balance) run_block(service_type, group_path, relist, load_balance, &block) node_subscription = @zk.register(watch_path) do |event| puts "server found event is #{event}" if event.node_child? || event.node_changed? # get children and re-set watch relist = @zk.children(watch_path, :watch => true) # relist = @zk.children(server_path) load_balance = @zk.get(watch_path, :watch => true)[0] #puts relist # check if server_type == thrift or http # block.call() # block.call(get_ip_from_server(server_path, relist), load_balance) run_block(service_type, group_path, relist, load_balance, &block) end end end
Public: zk server found server part, and it will create a ephemeral path with current server’s service ip, address, or so on.
option - The hash :ip_port -> service address .eg “192.168.3.1:2222”. child(option) - The sub-path name.
# File lib/reminder-client/configcenter.rb, line 42 def server_found_server(option, member_path = 'default') # todo reise problem if some key params is nil #raise "error" if server_info = ServerInfo.new(option) p "input params is #{option}" p server_info server_json = @encoder.encode(server_info.getHash) create_path = get_combined_path([SERVER_FOUND_BASE_PATH, server_info.serviceType, server_info.groupPath, server_info.memberName]) # create parent path if not exists create_parentPath([SERVER_FOUND_BASE_PATH, server_info.serviceType, server_info.groupPath]) puts server_json re = @zk.create(create_path, server_json.to_s, :ephemeral => true, :sequence => true) # re = @zk.create(option[:server_found_base_path] + "/" + child, option[:ip_port], :ephemeral => true, :sequence => true) puts "create ephemeral path #{re} with data #{server_json} succ !!!" #end end
Private Instance Methods
# File lib/reminder-client/configcenter.rb, line 183 def create_parentPath(paths) create_path = "" paths.each do |path| create_path << "/" create_path << path # create path if not exists re = @zk.create(create_path, '', :ephemeral => false, :sequence => false) if !@zk.exists?(create_path) puts "create parent path #{re}" if !re.nil? end end
# File lib/reminder-client/configcenter.rb, line 194 def get_combined_path(paths) repath = "" paths.each do |path| repath << "/" if !path.start_with?("/") repath << path end repath end
# File lib/reminder-client/configcenter.rb, line 154 def http_models_generator(jsons) relist = [] jsons.each do |json| hash = @parser.parse(json) relist << server_info_hash_to_http_model(hash) end relist end
# File lib/reminder-client/configcenter.rb, line 137 def run_block(server_type, group_path, children, load_balance, &block) parent_path = get_combined_path([SERVER_FOUND_BASE_PATH, server_type, group_path]) list = [] children.each { |child| #puts "get child #{child}" json_data = @zk.get(get_combined_path([parent_path, child]))[0] puts json_data list << json_data if json_data and !json_data.empty? #p "lalalala -> #{@zk.get(server_path + "/" + child)}" } if server_type == SERVER_TYPE_HTTP block.call(http_models_generator(list.uniq), load_balance) elsif server_type == SERVER_TYPE_THRIFT block.call(thrift_models_generator(list.uniq), load_balance) end end
# File lib/reminder-client/configcenter.rb, line 172 def server_info_hash_to_http_model(hash) HttpModel.new({ ip: hash["ip"], port: hash["port"], serviceType: hash["serviceType"], serviceName: hash["serviceName"]}) end
# File lib/reminder-client/configcenter.rb, line 177 def server_info_hash_to_thrift_model(hash) ThriftModel.new({ ip: hash["ip"], port: hash["port"], serviceType: hash["serviceType"], serviceName: hash["serviceName"], workerSize: hash["workerSize"], selectorSize: hash["selectorSize"] }) end
# File lib/reminder-client/configcenter.rb, line 163 def thrift_models_generator(jsons) relist = [] jsons.each do |json| hash = @parser.parse(json) relist << server_info_hash_to_thrift_model(hash) end relist end