1. 添加HBase和Micrometer依赖
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter</ artifactId>
</ dependency>
< dependency>
< groupId> org.apache.hbase</ groupId>
< artifactId> hbase-client</ artifactId>
</ dependency>
< dependency>
< groupId> io.micrometer</ groupId>
< artifactId> micrometer-core</ artifactId>
< version> 1.9.0</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-actuator</ artifactId>
< exclusions>
< exclusion>
< artifactId> micrometer-core</ artifactId>
< groupId> io.micrometer</ groupId>
</ exclusion>
</ exclusions>
</ dependency>
< dependency>
< groupId> io.micrometer</ groupId>
< artifactId> micrometer-registry-prometheus</ artifactId>
< version> 1.9.0</ version>
</ dependency>
</ dependencies>
2. 新增HBase配置文件
< configuration>
< property>
< name> hbase.zookeeper.quorum</ name>
< value> manager01,manager02,manager03</ value>
</ property>
< property>
< name> hbase.zookeeper.property.clientPort</ name>
< value> 2181</ value>
</ property>
< property>
< name> base.client.scanner.caching</ name>
< value> 1000</ value>
</ property>
< property>
< name> zookeeper.session.timeout</ name>
< value> 60000</ value>
</ property>
< property>
< name> zookeeper.znode.parent</ name>
< value> /hbase-unsecure</ value>
</ property>
</ configuration>
3. 编写服务获取HBase Admin客户端
@Slf4j
@Data
public class HBaseUtil implements Closeable {
private static HBaseUtil hBaseUtil = null ;
private Configuration configuration = null ;
private Connection connection = null ;
private Admin admin = null ;
@Override
public void close ( ) throws IOException {
connection. close ( ) ;
}
private HBaseUtil ( Configuration configuration) throws IOException , ServiceException {
this . configuration = configuration;
HBaseAdmin . available ( this . configuration) ;
this . connection = ConnectionFactory . createConnection ( configuration) ;
this . admin = connection. getAdmin ( ) ;
}
public static HBaseUtil getInstance ( ) {
if ( hBaseUtil == null ) {
try {
Configuration configuration = HBaseConfiguration . create ( ) ;
String path = HBaseUtil . class
. getClassLoader ( )
. getResource ( "config/hbase-site.xml" )
. getPath ( ) ;
System . out. println ( path) ;
configuration. addResource ( new org. apache. hadoop. fs. Path( path) ) ;
hBaseUtil = new HBaseUtil ( configuration) ;
} catch ( IOException | ServiceException ex) {
ex. printStackTrace ( ) ;
}
}
return hBaseUtil;
}
public List < ServerName > listAllServerName ( ) throws IOException {
List < ServerName > serverNames = new ArrayList < > ( ) ;
serverNames. addAll ( admin. getClusterStatus ( ) . getServers ( ) ) ;
log. info ( "==========serverNames==============" + serverNames. size ( ) ) ;
return serverNames;
}
public List < RegionInfo > listRegionsInfo ( ServerName serverName) throws IOException {
return admin. getRegions ( serverName) ;
}
. . . . . . .
}
4. 获取regions信息并上报
@Service
@Slf4j
public class HBaseService {
private static HBaseUtil hBaseUtil;
@Autowired
private MeterRegistry meterRegistry;
public HBaseService ( ) {
hBaseUtil = HBaseUtil . getInstance ( ) ;
}
public void exposeRegionMetrics ( ) throws IOException {
List < ServerName > serverNames = hBaseUtil. listAllServerName ( ) ;
for ( TableName curTableName : tableNames) {
List < RegionInfo > curRegions = hBaseUtil. listRegionsInfo ( curTableName) ;
HBaseRegionNumsMetrics hBaseRegionNumsMetrics = HBaseRegionNumsMetrics . builder ( )
. nameSpace ( curTableName. getNamespaceAsString ( ) )
. tableName ( curTableName. getNameAsString ( ) )
. nums ( curRegions. size ( ) )
. build ( ) ;
numsMetrics. add ( hBaseRegionNumsMetrics) ;
List < RegionMetrics > regionMetricsList = hBaseUtil. listRegionMetrics ( curTableName, serverNames) ;
regionMetricsList. forEach ( r -> {
HBaseRegionStoreCountMetrics curStoreCountMetrics = HBaseRegionStoreCountMetrics . builder ( )
. regionsName ( r. getNameAsString ( ) )
. storeCount ( ( long ) r. getStoreCount ( ) )
. tableName ( curTableName. getNameAsString ( ) ) . build ( ) ;
storeCountMetrics. add ( curStoreCountMetrics) ;
HBaseRegionStoreSizeMetrics curStoreSizeMetrics = HBaseRegionStoreSizeMetrics . builder ( )
. regionsName ( r. getNameAsString ( ) )
. storeSize ( r. getStoreFileSize ( ) . getLongValue ( ) )
. tableName ( curTableName. getNameAsString ( ) ) . build ( ) ;
storeSizeMetrics. add ( curStoreSizeMetrics) ;
} ) ;
}
for ( HBaseRegionNumsMetrics num : numsMetrics) {
Gauge . builder ( HBaseRegionNumsMetrics . METRICS_NAME , num:: getNums ) . tags ( num. getTags ( ) ) . register ( meterRegistry) ;
}
. . . . . .
}
5. 启动定时任务
@Slf4j
@Component
@EnableScheduling
public class HBaseMetricsJob {
@Autowired
private HBaseService HBaseService ;
@Scheduled ( cron = "${scheduled.cron.expr}" )
public void task ( ) {
try {
HBaseService . exposeRegionMetrics ( ) ;
} catch ( Exception e) {
log. error ( "==============HBase Exporter======ERROR=================" ) ;
e. printStackTrace ( ) ;
}
}
}