Class: Zoo
- Inherits:
-
Object
- Object
- Zoo
- Defined in:
- lib/reminder-client/configcenter.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- SERVER_FOUND_BASE_PATH =
"services"
- SERVER_TYPE_HTTP =
"http"
- SERVER_TYPE_THRIFT =
"thrift"
Instance Method Summary collapse
-
#initialize(options) ⇒ Zoo
constructor
A new instance of Zoo.
-
#server_config_client(config_path, group, &block) ⇒ Object
Public: The serverConfig client part.
-
#server_found_client(service_type, group_path, &block) ⇒ Object
Public: server found client part.
-
#server_found_server(option, member_path = 'default') ⇒ Object
Public: zk server found server part, and it will create a ephemeral path with current server’s service ip, address, or so on.
Constructor Details
#initialize(options) ⇒ Zoo
Returns a new instance of Zoo.
26 27 28 29 30 31 32 33 |
# File 'lib/reminder-client/configcenter.rb', line 26 def initialize() p "start connect to #{[:zk_address]}" @zk = ZK.new([:zk_address]) # @config_center_backend = options[:config_center_backend] @parser = Yajl::Parser @encoder = Yajl::Encoder puts "zk successfully connected to #{[:zk_address]}" end |
Instance Method Details
#server_config_client(config_path, group, &block) ⇒ Object
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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# 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 |
#server_found_client(service_type, group_path, &block) ⇒ Object
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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# 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 |
#server_found_server(option, member_path = 'default') ⇒ Object
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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 |