美文网首页
服务提供者框架模式

服务提供者框架模式

作者: fengjixcuhui | 来源:发表于2018-03-25 16:31 被阅读0次

定义

服务提供者框架:多个服务提供者实现一个服务,系统为服务提供者的客户端提供多个实现,并把他们从多个实现中解耦出来。

构成

组件 说明
服务接口 由服务提供者实现,用来抽象服务提供者提供的服务
服务提供者接口 服务提供者实现,创建其服务实现的实例
提供者注册API 系统用来注册 服务提供者 ,让客户端访问
服务访问API 客户端用来获取服务的实例

备注:服务访问API一般允许但是不要求客户端指定某种选择提供者的条件,如果没有这样的规定,API就会返回默认实现的一个实例
服务访问API是“灵活的静态工厂”,它构成了SPF的基础

示例

Class.forName("com.mysql.jdbc.Driver");  //1 
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/tcc","root","root"); //2
Statement stm = connection.createStatement(); //3

Driver

package com.mysql.jdbc;

import java.sql.DriverManager;
import java.sql.SQLException;

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    public Driver() throws SQLException {
    }

    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}

DriverManager

package java.sql;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.CopyOnWriteArrayList;
import sun.reflect.CallerSensitive;
import sun.reflect.Reflection;

 public static synchronized void registerDriver(java.sql.Driver driver,
            DriverAction da)
        throws SQLException {

        /* Register the driver if it has not already been added to our list */
        if(driver != null) {
            registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
        } else {
            // This is for compatibility with the original DriverManager
            throw new NullPointerException();
        }

        println("registerDriver: " + driver);

    }

//  Worker method called by the public getConnection() methods.
    private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized(DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }

            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }

        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            throw reason;
        }

        println("getConnection: no suitable driver found for "+ url);
        throw new SQLException("No suitable driver found for "+ url, "08001");
    }

组件 映射
服务接口 Connection
服务提供者接口 Driver
提供者注册API DriverManager.registerDriver()
服务访问API DriverManager.getConnection()方法

相关文章

  • 服务提供者框架模式

    Effective Java中提到了服务提供者框架, 书中是这样描述的: 服务提供者框架中有三个重要的组件:1)服...

  • 服务提供者框架模式

    定义 服务提供者框架:多个服务提供者实现一个服务,系统为服务提供者的客户端提供多个实现,并把他们从多个实现中解耦出...

  • Effective Java 2.0_服务提供者框架_Item

    文章作者:Tyan博客:noahsnail.com 1. 服务提供者框架介绍 1.1 什么是服务提供者框架 服务提...

  • 源码|什么是服务提供者框架?举例?

    服务提供者框架中有四个重要的组件: 服务接口(Service Interface):由服务提供者实现,用来抽象服务...

  • Dubbo负载均衡算法实现

    【前言】 Dubbo 的定位是分布式服务框架,为了避免单点压力过大,服务的提供者通常部署多台,如何从服务提供者集群...

  • (7)负载均衡算法分析

    【前言】 Dubbo 的定位是分布式服务框架,为了避免单点压力过大,服务的提供者通常部署多台,如何从服务提供者集群...

  • 1 Java对象管理

    本节重点说对象的创建和管理,如果还有什么这方面的欠缺,欢迎留言。 目录静态工厂服务提供者框架Builder模式的构...

  • 【框架集成7】 eureka集成

    eureka服务发现中心 pom.xml aplication.yml 配置代码 启动入口 服务提供者 在框架集成...

  • Android 基础

    框架图 Android 四大组件 活动(Activity) 服务(Service) 内容提供者(Content P...

  • Zookeeper用作注册中心的原理

    RPC框架中有3个重要的角色: 注册中心 :保存所有服务的名字,服务提供者的ip列表,服务消费者的IP列表 服务提...

网友评论

      本文标题:服务提供者框架模式

      本文链接:https://www.haomeiwen.com/subject/ftzhcftx.html