gemfire基本使用以及spring-data-gemfire的使用
1.安装程序的使用
- locator
启动locator
gfsh>start locator --name=locator1
指定端口启动
gfsh>start locator --name=locator1 --port=12105
指定端口和绑定ip启动
gfsh>start locator --name=locator1 --port=12105 --bind-address=10.10.10.110
查看locator状态
gfsh>status locator --name=locator1
gfsh>status locator --host=localhost --port=10334
连接locator
gfsh>connect
gfsh>connect --locator=localhost[10335]
关闭locator
gfsh>stop locator --name=locator1
关闭整个集群
gfsh>shutdown --include-locators=true
- server
启动server
gfsh>start server --name=server1
指定locator启动
gfsh>start server --name=server1 --locators=localhost[10334]
指定端口和locator启动
gfsh>start server --name=server1 --server-port=12104 --locators=10.10.10.110[12105]
停止server
gfsh>stop server --name=server1
- region
创建region
gfsh>create region --name=test --type=REPLICATE_PERSISTENT
存储kv值
gfsh>put --region=test --key="a" --value="A"
查询信息
gfsh>query --query="select * from /test"
查看region信息
gfsh>describe region --name=test
销毁region
gfsh>destroy region --name=test
-
部署jar包(deploy)
gemfire中部署jar包分为实体类和计算类两种情况:
1.实体类: 实体类需要部署到gemfire程序的classpath路径下面;
2.计算类: 对于计算类,可以通过deploy命令手动部署;
部署jar包
gfsh>deploy --jars=/data/local/gemfire/apache-geode-1.5.0/lib/geode-demo-1.0-SNAPSHOT.jar
查看已部署jar包
gfsh>list deployed
卸载jar包
gfsh>undeploy --jar=geode-demo-1.0-SNAPSHOT.jar
2. 结合spring-data的使用
maven依赖:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-gemfire</artifactId>
<version>2.0.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.pivotal.gemfire</groupId>
<artifactId>geode-lucene</artifactId>
</exclusion>
</exclusions>
</dependency>
2.1 客户端模式(client)
- 配置文件:
ClientContext.xml
<!-- 定义client-cache-->
<gfe:client-cache id="gemfireCache" pool-name="gemfirePool"/>
<!-- 定义locator连接池-->
<gfe:pool id="gemfirePool" subscription-enabled="true">
<!--<gfe:locator host="localhost" port="10334"/>-->
<gfe:locator host="10.10.10.110" port="12105"/>
</gfe:pool>
<!-- 定义客户端region -->
<gfe:client-region id="messages" cache-ref="gemfireCache" value-constraint="com.cord.demo.data.Message" shortcut="PROXY"/>
- 通过spring-data接口CrudRepository实现OQL查询region:
MessageReposirory.java
@Repository
@DependsOn("gemfireCache")
public interface MessageReposirory extends CrudRepository<Message, String>{
@Query("SELECT * FROM /messages m WHERE m.message = $1")
List<Message> findByMessage(String message);
@Query("SELECT * FROM /messages m WHERE m.message IN SET $1")
List<Message> findByMessages(List<String> messages);
}
ClientTestController.java
...
List<Message> c1 = reposirory.findByMessage("C");
c1.stream().forEach(System.out::println);
...
结论: 客户端模式更多的是对集群中数据的查询,而对集群中region的管理有限;
2.2 服务端嵌入模式(server)
配置文件:
ServerContext.xml
<!-- 定义region-->
<gfe:replicated-region id="messages" value-constraint="com.cord.demo.data.Message"/>
application.properties
#server端口和绑定地址
spring.data.gemfire.cache.server.port=41414
spring.data.gemfire.cache.server.bind-address=localhost
#locator端口和地址
spring.data.gemfire.locator.host=localhost
spring.data.gemfire.locator.port=10335
gemfire.properties
#开启jmx
jmx-manager=true
jmx-manager-start=true
#指定访问locator集群
locators=localhost[10334],localhost[10335]
启动类注解:
ServerApplication.java
@SpringBootApplication
@CacheServerApplication(name = "embedServer")
@EnableLocator
@EnableGemfireRepositories(basePackages = "com.cord.demo.dao")
@ImportResource(locations = {"classpath:ServerContext.xml"})
public class ServerApplication implements CommandLineRunner {
...
}
动态创建region:
ServerTestController.java
...
@Autowired
private Cache gemfireCache; //系统默认的cache名
...
/**获取region工厂*/
RegionFactory<String, String> rf = gemfireCache.createRegionFactory(RegionShortcut.REPLICATE);
/**创建region*/
Region<String, String> region = rf.create("test");
...