DubboDubbo 源码学习

Dubbo SPI 源码学习 & admin安装(二)

2018-04-24  本文已影响32人  jwfy

笔记简述
本学习笔记主要是介绍了SPI的使用以及原理,dubbo是如何实现自身的SPI,dubbo如何使用的可以看Dubbo 简要介绍和使用 学习(一)
更多内容可看[目录]Dubbo 源码学习

目录

Dubbo SPI 源码学习 & admin安装(二)
1、SPI
1.1、Java SPI DEMO
1.2、Java SPI 源码实现
2、Dubbo SPI 配置
3、Dubbo SPI 使用 & 源码学习
3.1、 Protocol 获取
3.2、SPI配置文件解析
4、dubbo-admin 安装使用

1、SPI

在dubbo中包含了很多组件,各种组件又有不同的协议使得存在多种实现,而且在开发中都是推荐针对接口编程,那就存在一个问题了,如何选择合适的实现类呢?解决方案就是SPI

SPI的全名为Service Provider Interface,提供类似于插拔式的实现类选择能力。在java中使用的类是java.util.ServiceLoader,然后在META-INF/services/创建的具体文件中写上具体的实现类的类名称,就可以实现具体的类,在早期的JDBC中,必须得通过Class.forName("xxx")这种硬编码的方式去生成对应类的实例,但是现在可以直接通过SPI达到同样的目的

1.1、Java SPI DEMO

public interface Search {
    void print();
}
public class FileSearch implements Search {

    @Override
    public void print() {
        System.out.println("==== A ====");
    }
}
public class SpiTest {

    public static void main(String[] args){
        ServiceLoader<Search> serviceLoader = ServiceLoader.load(Search.class);
        Iterator<Search> it = serviceLoader.iterator();
        while (it.hasNext()){
            Search printImpl = it.next();
            printImpl.print();
        }
    }
}

spi.Search

spi.FileSearch
spi.WebSearch
image

如上图,需要在资源文件的根目录创建一个META-INF.services的文件夹,新建一个Search接口完整路径的文件,本例子是spi.Search,里面填入实现类的类全程。如下图,确实调用了两个具体的实例,迭代分别执行。

image

1.2、Java SPI 源码实现

如下图,首先获取到fullName数据,然后调用getResource获取该文件内的资源


image
    private Iterator<String> parse(Class<?> service, URL u)
        throws ServiceConfigurationError
    {
        InputStream in = null;
        BufferedReader r = null;
        ArrayList<String> names = new ArrayList<>();
        try {
            in = u.openStream();
            r = new BufferedReader(new InputStreamReader(in, "utf-8"));
            int lc = 1;
            while ((lc = parseLine(service, u, r, lc, names)) >= 0);
        } catch (IOException x) {
            fail(service, "Error reading configuration file", x);
        } finally {
            try {
                if (r != null) r.close();
                if (in != null) in.close();
            } catch (IOException y) {
                fail(service, "Error closing configuration file", y);
            }
        }
        return names.iterator();
    }

然后解析文件内的内容,存入到一个ArrayList中,并返回其迭代器(后面肯定还差一步就是实例化)

image

很明显了,在获取到类信息,直接调用newInstance方法完成实例化操作

同时根据newInstance也可以知道对外提供的spi的实现类不能有自定义的构造函数

2、Dubbo SPI 配置

dubbo的spi和java自带的spi稍有区别,如上述的demo中,是在文件中写入什么实现类,就会去实现,如果需要获取其中的一个,则需要循环迭代获取处理,而在dubbo中则采用了类似kv对的样式,在具体使用的时候则通过相关想法即可获取,而且获取的文件路径也不一致

ExtensionLoader 类文件

private static final String SERVICES_DIRECTORY = "META-INF/services/";

private static final String DUBBO_DIRECTORY = "META-INF/dubbo/";
    
private static final String DUBBO_INTERNAL_DIRECTORY = DUBBO_DIRECTORY + "internal/";

如上述代码片段可知,dubbo是支持从META-INF/dubbo/,META-INF/dubbo/internal/以及META-INF/services/三个文件夹的路径去获取spi配置

image

例如com.alibaba.dubbo.rpc.Protocol 文件内容

registry=com.alibaba.dubbo.registry.integration.RegistryProtocol
filter=com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper
listener=com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper
mock=com.alibaba.dubbo.rpc.support.MockProtocol
injvm=com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol
dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol
rmi=com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol
hessian=com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol
com.alibaba.dubbo.rpc.protocol.http.HttpProtocol
com.alibaba.dubbo.rpc.protocol.webservice.WebServiceProtocol
thrift=com.alibaba.dubbo.rpc.protocol.thrift.ThriftProtocol
memcached=memcom.alibaba.dubbo.rpc.protocol.memcached.MemcachedProtocol
redis=com.alibaba.dubbo.rpc.protocol.redis.RedisProtocol

不过观察上述文件会发现,HttpProtocol是没有对应的k值,那就是说无法通过kv对获取到其协议实现类

后面通过源码可以发现,如果没有对应的name的时候,dubbo会通过findAnnotationName方法获取一个可用的name

3、Dubbo SPI 使用 & 源码学习

通过获取协议的代码来分析下具体的操作过程

3.1、 Protocol 获取

Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();

public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
    // 静态方法,意味着可以直接通过类调用
    if (type == null)
        throw new IllegalArgumentException("Extension type == null");
    if(!type.isInterface()) {
        throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
    }
    if(!withExtensionAnnotation(type)) {
        throw new IllegalArgumentException("Extension type(" + type + 
                ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
    }
    
    ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    // 从map中获取该类型的ExtensionLoader数据
    if (loader == null) {
        EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
        // 如果没有则,创建一个新的ExtensionLoader对象,并且以该类型存储
        loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        // 再从map中获取
        // 这里这样做的原因就是为了防止并发的问题,而且map本是也是个ConcurrentHashMap
    }
    return loader;
}
private ExtensionLoader(Class<?> type) {
    this.type = type;
    objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}
// 传入的type是Protocol.class,所以需要获取ExtensionFactory.class最合适的实现类
public T getAdaptiveExtension() {
    Object instance = cachedAdaptiveInstance.get();
    if (instance == null) {
        if(createAdaptiveInstanceError == null) {
            synchronized (cachedAdaptiveInstance) {
                instance = cachedAdaptiveInstance.get();
                if (instance == null) {
                    try {
                        instance = createAdaptiveExtension();
                        // 创建对象,也是需要关注的函数
                        cachedAdaptiveInstance.set(instance);
                    } catch (Throwable t) {
                        createAdaptiveInstanceError = t;
                        throw new IllegalStateException("fail to create adaptive instance: " + t.toString(), t);
                    }
                }
            }
        }
        else {
            throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
        }
    }

    return (T) instance;
}
private T createAdaptiveExtension() {
    try {
        return injectExtension((T) getAdaptiveExtensionClass().newInstance());
        // (T) getAdaptiveExtensionClass().newInstance() 创建一个具体的实例对象
        // getAdaptiveExtensionClass() 生成相关的class
        // injectExtension 往该对象中注入数据
    } catch (Exception e) {
        throw new IllegalStateException("Can not create adaptive extenstion " + type + ", cause: " + e.getMessage(), e);
    }
}
private Class<?> getAdaptiveExtensionClass() {
    getExtensionClasses();
    // 通过加载SPI配置,获取到需要的所有的实现类存储到map中
    // 通过可能会去修改cachedAdaptiveClass数据,具体原因在spi配置文件解析中分析
    if (cachedAdaptiveClass != null) {
        return cachedAdaptiveClass;
    }
    return cachedAdaptiveClass = createAdaptiveExtensionClass();
}
private Class<?> createAdaptiveExtensionClass() {
    String code = createAdaptiveExtensionClassCode();
    // 动态生成需要的代码内容字符串
    ClassLoader classLoader = findClassLoader();
    com.alibaba.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
    return compiler.compile(code, classLoader);
    // 编译,生成相应的类
}

如下代码Protocol$Adpative 整个的类就是通过createAdaptiveExtensionClassCode()方法生成的一个大字符串

public class Protocol$Adpative implements com.alibaba.dubbo.rpc.Protocol {  
    public void destroy() {throw new UnsupportedOperationException("method public abstract void com.alibaba.dubbo.rpc.Protocol.destroy() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");  
    }  
    public int getDefaultPort() {throw new UnsupportedOperationException("method public abstract int com.alibaba.dubbo.rpc.Protocol.getDefaultPort() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");  
    }  
    public com.alibaba.dubbo.rpc.Exporter export(com.alibaba.dubbo.rpc.Invoker arg0) throws com.alibaba.dubbo.rpc.RpcException {  
        // 暴露远程服务,传入的参数是invoke对象
        if (arg0 == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");  
  
        if (arg0.getUrl() == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");  
  
        com.alibaba.dubbo.common.URL url = arg0.getUrl();  
  
        String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );  
        // 如果没有具体协议,则使用dubbo协议
  
        if(extName == null) throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");  
  
        com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);  
  
        return extension.export(arg0);  
    }  
    public com.alibaba.dubbo.rpc.Invoker refer(java.lang.Class arg0, com.alibaba.dubbo.common.URL arg1) throws com.alibaba.dubbo.rpc.RpcException {  
        // 引用远程对象,生成相对应的远程invoke对象
        if (arg1 == null) throw new IllegalArgumentException("url == null");  
  
        com.alibaba.dubbo.common.URL url = arg1;  
  
        String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );  
  
        if(extName == null) throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");  
  
        com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);  
  
        return extension.refer(arg0, arg1);  
    }  
}  

到现在可以认为是最上面的获取protocol的方法Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension()返回了一个代码拼接而成然后编译操作的实现类Protocol$Adpative

可是得到具体的实现呢?在com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName)这个代码中,当然这个是有在具体的暴露服务或者引用远程服务才被调用执行的。

public T getExtension(String name) {
    if (name == null || name.length() == 0)
        throw new IllegalArgumentException("Extension name == null");
    if ("true".equals(name)) {
        return getDefaultExtension();
    }
    Holder<Object> holder = cachedInstances.get(name);
    if (holder == null) {
        cachedInstances.putIfAbsent(name, new Holder<Object>());
        holder = cachedInstances.get(name);
    }
    // 无论是否真有数据,在cachedInstances存储的是一个具体的Holder对象
    Object instance = holder.get();
    if (instance == null) {
        synchronized (holder) {
            instance = holder.get();
            if (instance == null) {
                instance = createExtension(name);
                // 创建对象
                holder.set(instance);
            }
        }
    }
    return (T) instance;
}
private T createExtension(String name) {
    Class<?> clazz = getExtensionClasses().get(name);
    // 获取具体的实现类的类
    if (clazz == null) {
        throw findException(name);
    }
    try {
        T instance = (T) EXTENSION_INSTANCES.get(clazz);
        if (instance == null) {
            EXTENSION_INSTANCES.putIfAbsent(clazz, (T) clazz.newInstance());
            // clazz.newInstance 才是真正创建对象的操作
            instance = (T) EXTENSION_INSTANCES.get(clazz);
        }
        injectExtension(instance);
        // 往实例中反射注入参数
        Set<Class<?>> wrapperClasses = cachedWrapperClasses;
        if (wrapperClasses != null && wrapperClasses.size() > 0) {
            for (Class<?> wrapperClass : wrapperClasses) {
                instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
            }
        }
        return instance;
    } catch (Throwable t) {
        throw new IllegalStateException("Extension instance(name: " + name + ", class: " +
                type + ")  could not be instantiated: " + t.getMessage(), t);
    }
}

到这一步就可以认为是dubbo的spi加载整个的过程完成了,整个链路有些长,需要好好的梳理一下

3.2、SPI配置文件解析

上文3.1说到getExtensionClasses完成对spi文件的解析

private Map<String, Class<?>> loadExtensionClasses() {
    final SPI defaultAnnotation = type.getAnnotation(SPI.class);
    // 查看该类是否存在SPI注解信息
    if(defaultAnnotation != null) {
        String value = defaultAnnotation.value();
        if(value != null && (value = value.trim()).length() > 0) {
            String[] names = NAME_SEPARATOR.split(value);
            if(names.length > 1) {
                throw new IllegalStateException("more than 1 default extension name on extension " + type.getName()
                        + ": " + Arrays.toString(names));
            }
            if(names.length == 1) cachedDefaultName = names[0];
            // 设置默认的名称,如果注解的值经过切割,发现超过1个的数据,则同样会认为错误
        }
    }
    
    Map<String, Class<?>> extensionClasses = new HashMap<String, Class<?>>();
    loadFile(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
    // 加载文件
    loadFile(extensionClasses, DUBBO_DIRECTORY);
    loadFile(extensionClasses, SERVICES_DIRECTORY);
    return extensionClasses;
}
    
private void loadFile(Map<String, Class<?>> extensionClasses, String dir) {
    String fileName = dir + type.getName();
    // type.getName就是类名称,和java的类似
    try {
        Enumeration<java.net.URL> urls;
        ClassLoader classLoader = findClassLoader();
        if (classLoader != null) {
            urls = classLoader.getResources(fileName);
        } else {
            urls = ClassLoader.getSystemResources(fileName);
        }
        if (urls != null) {
            while (urls.hasMoreElements()) {
                java.net.URL url = urls.nextElement();
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
                    try {
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            final int ci = line.indexOf('#');
                            if (ci >= 0) line = line.substring(0, ci);
                            line = line.trim();
                            if (line.length() > 0) {
                                try {
                                    String name = null;
                                    int i = line.indexOf('=');
                                    // 对k=v这样的格式进行分割操作,分别获取
                                    if (i > 0) {
                                        name = line.substring(0, i).trim();
                                        line = line.substring(i + 1).trim();
                                    }
                                    if (line.length() > 0) {
                                        Class<?> clazz = Class.forName(line, true, classLoader);
                                        if (! type.isAssignableFrom(clazz)) {
                                            throw new IllegalStateException("Error when load extension class(interface: " +
                                                    type + ", class line: " + clazz.getName() + "), class " 
                                                    + clazz.getName() + "is not subtype of interface.");
                                        }
                                        if (clazz.isAnnotationPresent(Adaptive.class)) {
                                            // 如果获取的类包含了Adaptive注解
                                            if(cachedAdaptiveClass == null) {
                                                cachedAdaptiveClass = clazz;
                                            } else if (! cachedAdaptiveClass.equals(clazz)) {
                                               // 已经存在了该数据,现在又出现了,则抛出异常
                                                throw new IllegalStateException("More than 1 adaptive class found: "
                                                        + cachedAdaptiveClass.getClass().getName()
                                                        + ", " + clazz.getClass().getName());
                                            }
                                        } else {  // 不包含Adaptive注解信息
                                            try {
                                                clazz.getConstructor(type);
                                                // 查看构造函数是否包含了type的类型参数
                                                // 如果不存在,则抛出NoSuchMethodException异常
                                                Set<Class<?>> wrappers = cachedWrapperClasses;
                                                if (wrappers == null) {
                                                    cachedWrapperClasses = new ConcurrentHashSet<Class<?>>();
                                                    wrappers = cachedWrapperClasses;
                                                }
                                                wrappers.add(clazz);
                                                // 往wrappers中添加该类
                                            } catch (NoSuchMethodException e) {
                                                clazz.getConstructor();
                                                if (name == null || name.length() == 0) {
                                                   // 没用名字的那种,也就是不存在k=v这种样式
                                                   // 例如上面的HttpProtocol
                                                    name = findAnnotationName(clazz);
                                                    // 查看该类是否存在注解
                                                    if (name == null || name.length() == 0) {
                                                        if (clazz.getSimpleName().length() > type.getSimpleName().length()
                                                                && clazz.getSimpleName().endsWith(type.getSimpleName())) {
                                                            name = clazz.getSimpleName().substring(0, clazz.getSimpleName().length() - type.getSimpleName().length()).toLowerCase();
                                                        } else {
                                                            throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + url);
                                                        }
                                                    }
                                                }
                                                String[] names = NAME_SEPARATOR.split(name);
                                                // 可能存在多个,切割开
                                                if (names != null && names.length > 0) {
                                                    Activate activate = clazz.getAnnotation(Activate.class);
                                                    if (activate != null) {
                                                        // 如果类存在Activate的注解信息
                                                        cachedActivates.put(names[0], activate);
                                                    }
                                                    for (String n : names) {
                                                        if (! cachedNames.containsKey(clazz)) {
                                                            cachedNames.put(clazz, n);
                                                        }
                                                        Class<?> c = extensionClasses.get(n);
                                                        if (c == null) {
                                                            extensionClasses.put(n, clazz);
                                                            // 往容器中填充该键值对信息,k和v
                                                        } else if (c != clazz) {
                                                             // 存在多个同名扩展类,则抛出异常信息
                                                            throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + n + " on " + c.getName() + " and " + clazz.getName());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } catch (Throwable t) {
                                    IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: " + type + ", class line: " + line + ") in " + url + ", cause: " + t.getMessage(), t);
                                    exceptions.put(line, e);
                                }
                            }
                        } // end of while read lines
                    } finally {
                        reader.close();
                    }
                } catch (Throwable t) {
                    logger.error("Exception when load extension class(interface: " +
                                        type + ", class file: " + url + ") in " + url, t);
                }
            } // end of while urls
        }
    } catch (Throwable t) {
        logger.error("Exception when load extension class(interface: " +
                type + ", description file: " + fileName + ").", t);
    }
}

这样完成了对spi配置文件的整个的扫描过程了

4、dubbo-admin 安装使用

在上一节中利用dubbo-admin查看了服务提供方和服务调用方,现在就来简要的介绍下如何在本地跑起来admin,先从github拉取代码incubator-dubbo-ops到本地,在maven环境下,依赖maven-tomcat插件启动,并没有打包成war包丢到Tomcat下启动

添加注册组信息

dubbo-admin.xml 文件添加注册组信息

<dubbo:registry client="curator" address="${dubbo.registry.address}" group="${dubbo.register.group}" check="false" file="false"/>

对应的配置文件信息,添加组的具体内容

dubbo.properties

dubbo.registry.address=zookeeper://127.0.0.1:2182
dubbo.register.group=dubbo-demo
dubbo.admin.root.password=root   // root用户的密码是root
dubbo.admin.guest.password=guest   // guest用户的密码是guest

添加Tomcat插件运行

在pom.xml文件内添加如下代码

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <uriEncoding>UTF-8</uriEncoding>
                <port>8112</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

接下来就可以使用mvn tomcat7:run -Dport=8112或者IDEA 的maven工具直接启动

image

在浏览器输入127.0.0.1:8112,输入账户名和秘密(分别都是root),就进入到admin后台页面

image
上一篇 下一篇

猜你喜欢

热点阅读