nacos源码3-服务管理-客户端

2019-01-13  本文已影响0人  modou1618

一 入口

1.1 NamingFactory工厂类

public class NamingFactory {

    public static NamingService createNamingService(String serverList) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
            Constructor constructor = driverImplClass.getConstructor(String.class);
            NamingService vendorImpl = (NamingService)constructor.newInstance(serverList);
            return vendorImpl;
        } catch (Throwable e) {
            throw new NacosException(-400, e.getMessage());
        }
    }

    public static NamingService createNamingService(Properties properties) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
            Constructor constructor = driverImplClass.getConstructor(Properties.class);
            NamingService vendorImpl = (NamingService)constructor.newInstance(properties);
            return vendorImpl;
        } catch (Throwable e) {
            throw new NacosException(-400, e.getMessage());
        }
    }
}

1.2 NacosNamingService

NacosNamingService.png

二 NamingProxy

2.1 HttpClient

String encodedContent = encodingParams(paramValues, encoding);
private static String encodingParams(Map<String, String> params, String encoding)
        throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder();
    if (null == params || params.isEmpty()) {
        return null;
    }

    params.put("encoding", encoding);

    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (StringUtils.isEmpty(entry.getValue())) {
            continue;
        }

        sb.append(entry.getKey()).append("=");
        sb.append(URLEncoder.encode(entry.getValue(), encoding));
        sb.append("&");
    }

    return sb.toString();
}
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(CON_TIME_OUT_MILLIS);
conn.setReadTimeout(TIME_OUT_MILLIS);
conn.setRequestMethod(method);
setHeaders(conn, headers, encoding);
private static void setHeaders(HttpURLConnection conn, List<String> headers, String encoding) {
    if (null != headers) {
        for (Iterator<String> iter = headers.iterator(); iter.hasNext(); ) {
            conn.addRequestProperty(iter.next(), iter.next());
        }
    }

    conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="
            + encoding);
    conn.addRequestProperty("Accept-Charset", encoding);
}
return getResult(conn);

private static HttpResult getResult(HttpURLConnection conn) throws IOException {
    int respCode = conn.getResponseCode();

    InputStream inputStream;
    if (HttpURLConnection.HTTP_OK == respCode
            || HttpURLConnection.HTTP_NOT_MODIFIED == respCode) {
        inputStream = conn.getInputStream();
    } else {
        inputStream = conn.getErrorStream();
    }

    Map<String, String> respHeaders = new HashMap<String, String>(conn.getHeaderFields().size());
    for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
        respHeaders.put(entry.getKey(), entry.getValue().get(0));
    }

    String encodingGzip = "gzip";

    if (encodingGzip.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) {
        inputStream = new GZIPInputStream(inputStream);
    }

    return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)), respHeaders);
}

2.2 NamingProxy

2.2.1 服务管理服务端地址管理

2.2.2 接口

三 BeatReactor

public class BeatInfo {
    private int port;
    private String ip;
    private double weight;
    private String dom;
    private String cluster;
    private Map<String, String> metadata;
...
}

四 EventDispatcher

public void serviceChanged(ServiceInfo serviceInfo) {
    if (serviceInfo == null) {
        return;
    }

    changedServices.add(serviceInfo);
}
List<EventListener> listeners = observerMap.get(serviceInfo.getKey());

if (!CollectionUtils.isEmpty(listeners)) {
    for (EventListener listener : listeners) {
        List<Instance> hosts = Collections.unmodifiableList(serviceInfo.getHosts());
        listener.onEvent(new NamingEvent(serviceInfo.getName(), hosts));
    }
}

五 HostReactor

5.1 FailoverReactor

5.2 PushRecver

类型 功能
dom 更新客户端存储的服务实例信息,触发变更监听回调
dump json格式转换hostReactor.serviceInfoMap整体服务实例信息,回传给服务管理服务端。

5.3 服务实例管理

服务实例获取流程.png

5.3.1 服务实例拉模式

六 Balancer

public class Ref<T> {
        private List<Pair<T>> itemsWithWeight = new ArrayList<Pair<T>>();//带权重原始数据
        private List<T> items = new ArrayList<T>();//实例数组
        private double[] weights;//对应索引实例的权重+所有之前索引实例的权重。
}
上一篇下一篇

猜你喜欢

热点阅读