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>
<!--
Check out the properties here
https://github.com/apache/hbase/blob/master/hbase-server/src/test/resources/hbase-site.xml
-->
<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();
}
/**
* 处理Regions指标数据
*
* @throws Exception
*/
public void exposeRegionMetrics() throws IOException {
List<ServerName> serverNames = hBaseUtil.listAllServerName();
for (TableName curTableName : tableNames) {
List<RegionInfo> curRegions = hBaseUtil.listRegionsInfo(curTableName);
// table中regions分区数量指标
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);
// 总文件大小指标 MB
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;
/**
* @TODO 基础任务
*/
@Scheduled(cron = "${scheduled.cron.expr}")
public void task() {
try {
HBaseService.exposeRegionMetrics();
} catch (Exception e) {
log.error("==============HBase Exporter======ERROR=================");
e.printStackTrace();
}
}
}
文章来源地址https://www.toymoban.com/news/detail-539873.html
文章来源:https://www.toymoban.com/news/detail-539873.html
到了这里,关于Springboot+Hbase获取regions信息并上报到metrics接口中的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!