All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yx.rpc.client.route.Routes Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/**
 * Copyright (C) 2016 - 2030 youtongluan.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.yx.rpc.client.route;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.yx.common.Host;

public class Routes {
	private final Map routes;
	private final Map zkDatas;

	private Routes(Map zkDatas, Map routes) {
		super();
		this.zkDatas = zkDatas;
		this.routes = routes;
	}

	private static volatile Routes ROUTE = new Routes(Collections.emptyMap(), Collections.emptyMap());

	public static RpcRoute getRoute(String api) {
		return ROUTE.routes.get(api);
	}

	private static void _refresh(Map data, Map route) {
		Routes r = new Routes(data, route);
		Routes.ROUTE = r;
	}

	static void handle(RouteEvent event) {
		Map data = new HashMap<>(ROUTE.zkDatas);
		switch (event.getType()) {
		case CREATE:
		case MODIFY:
			data.put(event.getUrl(), event.getZkData());
			break;
		case DELETE:

			if (data.remove(event.getUrl()) == null) {
				return;
			}
			break;
		default:
		}
		refresh(data);
	}

	public static synchronized void refresh(Map datas) {
		Map> map = new HashMap<>();
		datas.forEach((url, data) -> {
			Map ms = createServerMachine(url, data);
			ms.forEach((m, serverMachine) -> {
				Set server = map.get(m);
				if (server == null) {
					server = new HashSet<>();
					map.put(m, server);
				}
				server.add(serverMachine);
			});
		});
		Map routes = new HashMap<>();
		map.forEach((method, servers) -> {
			if (servers == null || servers.isEmpty()) {
				return;
			}
			routes.put(method, new RpcRoute(servers));
		});
		_refresh(datas, routes);
	}

	private static Map createServerMachine(Host url, ZkData data) {
		Map servers = new HashMap<>();
		int weight = data.weight > 0 ? data.weight : 100;
		data.getIntfs().forEach(intf -> {
			ServerMachine server = new ServerMachine(url, weight);
			servers.put(intf.getName(), server);
		});
		return servers;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy