griddb集群可以使用多种方式部署
- 多播集群
- 静态配置(固定列表)
- url 地址provider
以下是一个简单的基于固定node 节点列表的模式部署
环境准备
为了方便使用docker-compose 部署,同时使用估计ip 分配模式
- docker-compose 文件
version: "3"
services:
pgspider-griddb:
image: dalongrong/pgspider:griddb-4.2
environment:
- "POSTGRES_PASSWORD=dalong"
ports:
- "5432:5432"
networks:
service1_net:
ipv4_address: 192.168.1.14
griddb-java:
build:
context: ./
dockerfile: ./Dockerfile-java
environment:
- "MEMBER_ADDR=griddb-1:10001"
networks:
service1_net:
ipv4_address: 192.168.1.13
griddb-1:
build: ./
networks:
service1_net:
ipv4_address: 192.168.1.10
griddb-2:
build: ./
networks:
service1_net:
ipv4_address: 192.168.1.11
griddb-3:
build: ./
networks:
service1_net:
ipv4_address: 192.168.1.12
networks:
service1_net:
ipam:
driver: default
config:
- subnet: 192.168.1.0/16
简单说明,包含了三类服务,griddb ,一个简单的java 应用,一个postgres griddb 的fdw
- griddb 配置说明
基于官方docker镜像,修改了部分配置
FROM griddbnet/griddb
COPY start_griddb.sh /start_griddb.sh
start_griddb.sh脚本
#!/bin/bash
chown gsadm.gridstore /var/lib/gridstore/data
IP=`grep $HOSTNAME /etc/hosts | awk ' { print $1 }'`
cat << EOF > /var/lib/gridstore/conf/gs_cluster.json
{
"dataStore":{
"partitionNum":128,
"storeBlockSize":"64KB"
},
"cluster":{
"clusterName":"defaultCluster",
"replicationNum":2,
"notificationInterval":"5s",
"heartbeatInterval":"5s",
"loadbalanceCheckInterval":"180s",
"notificationMember": [
{
"cluster": {"address":"192.168.1.10", "port":10010},
"sync": {"address":"192.168.1.10", "port":10020},
"system": {"address":"192.168.1.10", "port":10080},
"transaction": {"address":"192.168.1.10", "port":10001},
"sql": {"address":"192.168.1.10", "port":20001}
},
{
"cluster": {"address":"192.168.1.11", "port":10010},
"sync": {"address":"192.168.1.11", "port":10020},
"system": {"address":"192.168.1.11", "port":10080},
"transaction": {"address":"192.168.1.11", "port":10001},
"sql": {"address":"192.168.1.11", "port":20001}
},
{
"cluster": {"address":"192.168.1.12", "port":10010},
"sync": {"address":"192.168.1.12", "port":10020},
"system": {"address":"192.168.1.12", "port":10040},
"transaction": {"address":"192.168.1.12", "port":10001},
"sql": {"address":"192.168.1.12", "port":20001}
}
]
},
"sync":{
"timeoutInterval":"30s"
}
}
EOF
cat << EOF > /var/lib/gridstore/conf/gs_node.json
{
"dataStore":{
"dbPath":"data",
"backupPath":"backup",
"syncTempPath":"sync",
"storeMemoryLimit":"1024MB",
"storeWarmStart":false,
"storeCompressionMode":"NO_COMPRESSION",
"concurrency":6,
"logWriteMode":1,
"persistencyMode":"NORMAL",
"affinityGroupSize":4,
"autoExpire":false
},
"checkpoint":{
"checkpointInterval":"60s",
"checkpointMemoryLimit":"1024MB",
"useParallelMode":false
},
"cluster":{
"servicePort":10010
},
"sync":{
"servicePort":10020
},
"system":{
"servicePort":10040,
"eventLogPath":"log"
},
"transaction":{
"servicePort":10001,
"connectionLimit":5000
},
"trace":{
"default":"LEVEL_ERROR",
"dataStore":"LEVEL_ERROR",
"collection":"LEVEL_ERROR",
"timeSeries":"LEVEL_ERROR",
"chunkManager":"LEVEL_ERROR",
"objectManager":"LEVEL_ERROR",
"checkpointFile":"LEVEL_ERROR",
"checkpointService":"LEVEL_INFO",
"logManager":"LEVEL_WARNING",
"clusterService":"LEVEL_ERROR",
"syncService":"LEVEL_ERROR",
"systemService":"LEVEL_INFO",
"transactionManager":"LEVEL_ERROR",
"transactionService":"LEVEL_ERROR",
"transactionTimeout":"LEVEL_WARNING",
"triggerService":"LEVEL_ERROR",
"sessionTimeout":"LEVEL_WARNING",
"replicationTimeout":"LEVEL_WARNING",
"recoveryManager":"LEVEL_INFO",
"eventEngine":"LEVEL_WARNING",
"clusterOperation":"LEVEL_INFO",
"ioMonitor":"LEVEL_WARNING"
}
}
EOF
gs_passwd admin -p admin
gs_startnode
sleep 5
while gs_stat -u admin/admin | grep RECOV > /dev/null; do
echo Waiting for GridDB to be ready.
sleep 5
done
gs_joincluster -n 1 -u admin/admin -n 3
tail -f /var/lib/gridstore/log/gridstore*.log
- java 应用
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>com.dalong</groupId>
<artifactId>griddb-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.griddb</groupId>
<artifactId>gridstore</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>
<!-- Maven Shade Plugin -->
<finalName>griddb-app</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dalong.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Java 代码
package com.dalong;
import java.util.Arrays;
import java.util.Properties;
import com.toshiba.mwcloud.gs.Collection;
import com.toshiba.mwcloud.gs.GSException;
import com.toshiba.mwcloud.gs.GridStore;
import com.toshiba.mwcloud.gs.GridStoreFactory;
import com.toshiba.mwcloud.gs.Query;
import com.toshiba.mwcloud.gs.RowKey;
import com.toshiba.mwcloud.gs.RowSet;
public class Application {
static class Person {
@RowKey String name;
boolean status;
long count;
byte[] lob;
}
public static void main(String[] args) throws GSException {
// Get a GridStore instance
Properties props = new Properties();
props.setProperty("notificationMember", args[0]);
props.setProperty("clusterName", "defaultCluster");
props.setProperty("user", "admin");
props.setProperty("password", "admin");
GridStore store = GridStoreFactory.getInstance().getGridStore(props);
// Create a Collection (Delete if schema setting is NULL)
Collection<String, Person> col = store.putCollection("col01", Person.class);
// Set an index on the Row-key Column
col.createIndex("name");
// Set an index on the Column
col.createIndex("count");
// Set the autocommit mode to OFF
col.setAutoCommit(false);
// Prepare data for a Row
Person person = new Person();
person.name = "name01";
person.status = false;
person.count = 1;
person.lob = new byte[] { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
// Operate a Row on a K-V basis: RowKey = "name01"
boolean update = true;
col.put(person); // Add a Row
person = col.get(person.name, update); // Obtain the Row (acquiring a lock for update)
col.remove(person.name); // Delete the Row
// Operate a Row on a K-V basis: RowKey = "name02"
col.put("name02", person); // Add a Row (specifying RowKey)
// Commit the transaction (Release the lock)
col.commit();
// Search the Collection for a Row
Query<Person> query = col.query("select * where name = 'name02'");
// Fetch and update the searched Row
RowSet<Person> rs = query.fetch(update);
while (rs.hasNext()) {
// Update the searched Row
Person person1 = rs.next();
person1.count += 1;
rs.update(person1);
System.out.println("Person: " +
" name=" + person1.name +
" status=" + person1.status +
" count=" + person1.count +
" lob=" + Arrays.toString(person1.lob));
}
// Commit the transaction
col.commit();
// Release the resource
store.close();
}
}
postgres griddb fdw 参考https://github.com/rongfengliang/pgspider-docker
java Dockerfile
FROM openjdk:8u242-jdk-stretch
WORKDIR /app
COPY java-app/target/griddb-app.jar /app/griddb-app.jar
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
java 容器entrypoint
#!/bin/sh
MEMBER=$MEMBER_ADDR
java -jar /app/griddb-app.jar $MEMBER
启动&&测试
- 启动
因为服务有依赖关系,先启动griddb 集群,然后是添加测试数据,最后是使用pg 的fdw 进行数据sql 访问
// 启动griddb 集群
docker-compose up -d griddb-1 griddb-2 griddb-3
// 稍等1分钟左右,或者看看griddb 的日志,查看集群的状态,然后启动java app
docker-compose up -d griddb-java
// 启动pg griddb fdw 扩展:
docker-compose up -d pgspider-griddb
- 使用扩展查询
// 创建扩展
CREATE EXTENSION griddb_fdw;
// 创建外部server
CREATE SERVER griddb_svr FOREIGN DATA WRAPPER griddb_fdw OPTIONS(notification_member 'griddb-1:10001,griddb-2:10001,griddb-3:10001',clustername 'defaultCluster');
// 创建用户映射
CREATE USER MAPPING FOR public SERVER griddb_svr OPTIONS(username 'admin', password 'admin');
// 导入schema
IMPORT FOREIGN SCHEMA griddb_schema FROM SERVER griddb_svr INTO public;
// 数据查询
select * from col01;
- 效果
参考资料
http://www.toshiba-sol.co.jp/en/pro/griddb/docs-en/v4_1/GridDB_TechnicalReference.html
https://github.com/rongfengliang/griddb-cluster-pgspider-fdw
https://github.com/rongfengliang/pgspider-docker
https://github.com/rongfengliang/pgspider-griddb-fdw-docker-learninng
https://griddb.net/en/blog/quickly-deploy-three-node-griddb-cluster/
https://medium.com/griddb