问题描述
我需要获取有关 apache geode 客户端缓存应用程序的区域统计信息。
设置: 1个定位器 1台服务器 1 个客户端缓存应用
所有模块都是使用 spring 创建的。
缓存服务器将根据 cache.xml
创建区域缓存.xml:
<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://geode.apache.org/schema/cache"
xsi:schemaLocation="http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd"
version="1.0" lock-lease="120" lock-timeout="60" search-timeout="300"
is-server="true" copy-on-read="false">
<pdx read-serialized="true" persistent="true">
<pdx-serializer>
<class-name>
org.apache.geode.pdx.ReflectionBasedAutoSerializer
</class-name>
</pdx-serializer>
</pdx>
<region name="track" refid="PARTITION_PERSISTENT_OVERFLOW">
<region-attributes statistics-enabled="true">
<compressor>
<class-name>org.apache.geode.compression.SnappyCompressor</class-name>
</compressor>
</region-attributes>
<index name="trackKeyIndex" from-clause="/track" expression="key" key-index="true"/>
<index name="trackTransactionNameIndex" from-clause="/track" expression="transactions[*]"/>
</region>
</cache>
缓存服务器应用
@SpringBootApplication
@org.springframework.data.gemfire.config.annotation.CacheServerApplication(name = "cacheServer",locators = "localhost[10334]")
@EnableClusterAware
@EnableCompression
@EnableStatistics
@EnableGemFireProperties(cacheXmlFile = "cache.xml")
public class CacheServerApplication {
public static void main(String[] args) {
SpringApplication.run(CacheServerApplication.class,args);
}
}
客户端缓存应用
@SpringBootApplication
@ClientCacheApplication
@EnableClusterDefinedRegions //Fetch cluster defined regions for @Resource autowired prop
@EnableStatistics
public class GeodeClientApplication {
public static void main(String[] args) {
SpringApplication.run(GeodeClientApplication.class,args);
}
}
客户端缓存中的组件类,用于获取区域统计信息。
@Component
public class TrackedInsightsCacheService {
private static Logger logger = LoggerFactory.getLogger(TrackedInsightsCacheService.class);
@Autowired
@Resource(name = "track")
private Region trackRegion;
public Object getRegionStatistics(){
RegionAttributes attributes = trackRegion.getAttributes();
if(attributes.getStatisticsEnabled()) {
return trackRegion.getStatistics();
}
return null;
}
public Object get(String key) {
return trackRegion.get(key);
}
public void put(String key,String value){
trackRegion.put(key,value);
}
}
Autowired TrackRegion 是 LocalRegion。每当我进行 get 调用时,它首先检查本地区域,然后检查服务器区域上的密钥。
但是当我执行 getStatistics 调用时,它说该区域的统计数据已禁用。
我在这里错过了什么? 这是获取区域统计信息的正确方法吗。
我可以通过 gfsh 命令行获取集群统计信息,输出是这样的,
gfsh>show metrics
Cluster-wide Metrics
Category | Metric | Value
--------- | --------------------- | -----
cluster | totalHeapSize | 4846
cache | totalRegionEntryCount | 1
| totalRegionCount | 1
| totalMissCount | 81
| totalHitCount | 15
diskstore | totalDiskUsage | 0
| diskReadsRate | 0.0
| diskWritesRate | 0.0
| flushTimeAvgLatency | 0
| totalBackupInProgress | 0
query | activeCQCount | 0
| queryRequestRate | 0.0
我在设置中有多个区域,仅查看集群统计数据是不够的,因此需要获取区域指标数据。
解决方法
getStatistics()
方法返回实际 Region
实例的统计信息。由于您是在客户端执行此方法,因此返回的实际统计信息将针对本地客户端 Region
,这不是您想要的。
gfsh show metrics
命令实际上使用 JMX
检索区域统计信息,您可以检查源代码并根据您的需要进行调整 here。
如果您不想使用 JMX
,另一种选择是编写自定义 Geode Function 并通过使用 {{3} }.
干杯。