我目前正在通过 Maven 使用 Spring Boot Starter 1.4.2.RELEASE 和 Geode Core 1.0.0-incubating,针对由 Geode Locator 和 2 个缓存节点组成的本地 Docker 配置。
我在这里查阅了文档:
http://geode.apache.org/docs/guide/developing/distributed_regions/locking_in_global_regions.html
我已经配置了一个 cache.xml 文件以与我的应用程序一起使用,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<client-cache
xmlns="http://geode.apache.org/schema/cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd"
version="1.0">
<pool name="serverPool">
<locator host="localhost" port="10334"/>
</pool>
<region name="testRegion" refid="CACHING_PROXY">
<region-attributes pool-name="serverPool"
scope="global"/>
</region>
</client-cache>
在我的 Application.java 中,我通过以下方式将该区域公开为 bean:
@SpringBootApplication
public class Application {
@Bean
ClientCache cache() {
return new ClientCacheFactory()
.create();
}
@Bean
Region<String, Integer> testRegion(final ClientCache cache) {
return cache.<String, Integer>getRegion("testRegion");
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在我的“服务”DistributedCounter.java 中:
@Service
public class DistributedCounter {
@Autowired
private Region<String, Integer> testRegion;
/**
* Using fine grain lock on modifier.
* @param counterKey {@link String} containing the key whose value should be incremented.
*/
public void incrementCounter(String counterKey) {
if(testRegion.getDistributedLock(counterKey).tryLock()) {
try {
Integer old = testRegion.get(counterKey);
if(old == null) {
old = 0;
}
testRegion.put(counterKey, old + 1);
} finally {
testRegion.getDistributedLock(counterKey).unlock();
}
}
}
我已经使用 gfsh 配置了一个名为 /testRegion 的区域 - 但是没有选项表明它的类型应该是“GLOBAL”,只有各种其他选项可用 - 理想情况下,这应该是一个持久的和复制的缓存,尽管如此以下命令:
create region --name=/testRegion --type=REPLICATE_PERSISTENT
使用方法:http: //geode.apache.org/docs/guide/getting_started/15_minute_quickstart_gfsh.html很容易在我的两个节点配置上看到持久性和复制的功能。
但是,上面的 DistributedCounter 中的锁定不会导致任何错误 - 但当两个进程尝试获取同一个“键”上的锁时它只是不起作用 - 第二个进程不会被阻止获取锁。Gemfire 论坛有一个较早的代码示例,它使用 DistributedLockService - 当前文档警告不要将其用于锁定区域条目。
支持原子增量长的“映射”的细粒度锁定用例是否是受支持的用例,如果是,如何适当地配置它?