一、 JdbcFilter 过滤器
zebra 定义了 JdbcFilter 过滤器, 它包含的方法覆盖了 zebra 执行生命周期的各个环节(如各层数据源的初始化/刷新、分组连接的获取/关闭、sql的执行 等); 那么就方便在各个环节进行监控上报等操作了
public interface JdbcFilter {
int DEFAULT_ORDER = 0;
int MAX_ORDER = Integer.MAX_VALUE;
int MIN_ORDER = Integer.MIN_VALUE;
/**
* filter ordering <br>
* filter_with_order_3_start filter_with_order_2_start
* filter_with_order_1_start targer_start filter_with_order_1_finish
* filter_with_order_2_finish filter_with_order_3_finish
*
* @return the order of execute
*/
int getOrder();
/**
* init filter
*/
void init();
/** GroupDataSource Filter **/
void initGroupDataSource(GroupDataSource source, JdbcFilter chain);
void refreshGroupDataSource(GroupDataSource source, String propertiesName, JdbcFilter chain);
GroupConnection getGroupConnection(GroupDataSource source, JdbcFilter chain) throws SQLException;
FailOverDataSource.FindMasterDataSourceResult findMasterFailOverDataSource(
FailOverDataSource.MasterDataSourceMonitor source, JdbcFilter chain);
void closeGroupConnection(GroupConnection source, JdbcFilter chain) throws SQLException;
void closeGroupDataSource(GroupDataSource source, JdbcFilter chain) throws SQLException;
void switchFailOverDataSource(FailOverDataSource source, JdbcFilter chain);
/** SingleDataSource Filter **/
DataSource initSingleDataSource(SingleDataSource source, JdbcFilter chain);
SingleConnection getSingleConnection(SingleDataSource source, JdbcFilter chain) throws SQLException;
String processSQL(DataSourceConfig dsConfig, SQLProcessContext ctx, JdbcFilter chain) throws SQLException;
<T> T executeSingleStatement(SingleStatement source, SingleConnection conn, String sql, List<String> batchedSql,
boolean isBatched, boolean autoCommit, Object params, JdbcFilter chain) throws SQLException;
void closeSingleConnection(SingleConnection source, JdbcFilter chain) throws SQLException;
void closeSingleDataSource(SingleDataSource source, JdbcFilter chain) throws SQLException;
void closeSingleResultSet(SingleResultSet source, JdbcFilter chain) throws SQLException;
/** ShardDataSource Filter **/
void initShardDataSource(ShardDataSource source, JdbcFilter chain);
ResultSet executeShardQuery(ShardStatement source, String sql, JdbcFilter chain) throws SQLException;
int executeShardUpdate(ShardStatement source, String sql, int autoGeneratedKeys, int[] columnIndexes,
String[] columnNames, JdbcFilter chain) throws SQLException;
void shardRouting(RouterResult rr, JdbcFilter chain) throws SQLException;
void shardMerge(ShardResultSet rs, JdbcFilter chain) throws SQLException;
void configChanged(PropertyChangeEvent evt, JdbcFilter chain);
}
都是些 void 的无害方法,在zebra数据源、配置及sql代理执行过程的各个点,会执行 JdbcFilter 的对应方法;如在GroupDataSource 初始化的时候,会调用 JdbcFilter#initGroupDataSource 方法
JdbcFilter chain = new DefaultJdbcFilterChain(filters) {
@Override
public void initGroupDataSource(GroupDataSource source, JdbcFilter chain) {
if (index < filters.size()) {
filters.get(index++).initGroupDataSource(source, chain);
} else {
// 开始初始化
source.initInternal();
}
}
};
zebra 里边的 JdbcFilter 由 DefaultFilterManager 类负责管理,只要是实现了 JdbcFilter 接口的类就会被它通过类加载的方式加载到, 多个filter的执行是加入到责任链 DefaultJdbcFilterChain 里执行的
自带的 JdbcFilter有
- CatFilter上报监控信息到cat
- WallFilter 流控
如何扩展 Filter?
-
实现Filter接口 一般继承
DefaultJdbcFilter
即可,Zebra默认提供wallFilter和CatFilter,开发者可以进行参考public class WallFilter extends DefaultJdbcFilter
注册 Filter
Filter的类加载文件放置在 META-INF/zebra-filter.properties
目录下,格式为
- 文件名:zebra.filter.${filterName}=类路径
- 内容: zebra.filter.wall=com.dianping.zebra.filter.wall.WallFilter
二、SPI扩展
zebra 的 SPI 注解
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Spi {
String name() default "";
Scope scope() default Scope.SINGLETON;
}
它针对的是 配置获取方式 ConfigService
自定义的 ConfigService Spi 类加载文件可以放置在 META-INF/services/com.dianping.zebra.config.ConfigService
文件中 进行注册,如
com.dianping.zebra.config.ZookeeperConfigService
com.dianping.zebra.config.PropertyConfigService
如 zk 配置的定义:
@Spi(name = "zookeeper", scope = Scope.SINGLETON)
public class ZookeeperConfigService implements ConfigService
他们会被 ExtensionLoader 加载并创建对象,用于配置拉取