dgraph 单机的已经运行过了,以下是一个简单的集群环境运行,以及试用
环境准备
- docker-compose 文件
version: "3.2"
services:
zero1:
image: dgraph/dgraph:latest
volumes:
- data-volume1:/dgraph
ports:
- 5080:5080
- 6080:6080
command: dgraph zero --my=zero1:5080 --replicas 3 --idx 1
zero2:
image: dgraph/dgraph:latest
volumes:
- data-volume2:/dgraph
ports:
- 5081:5081
- 6081:6081
command: dgraph zero -o 1 --my=zero2:5081 --replicas 3 --peer zero1:5080 --idx 2
zero3:
image: dgraph/dgraph:latest
volumes:
- data-volume3:/dgraph
ports:
- 5082:5082
- 6082:6082
command: dgraph zero -o 2 --my=zero3:5082 --replicas 3 --peer zero1:5080 --idx 3
alpha1:
image: dgraph/dgraph:latest
hostname: "alpha1"
volumes:
- data-volume-app1:/dgraph
ports:
- 8080:8080
- 9080:9080
command: dgraph alpha --my=alpha1:7080 --lru_mb=2048 --zero=zero1:5080,zero2:5080,zero3:5080
alpha2:
image: dgraph/dgraph:latest
hostname: "alpha2"
volumes:
- data-volume-app2:/dgraph
ports:
- 8081:8081
- 9081:9081
command: dgraph alpha --my=alpha2:7081 --lru_mb=2048 --zero=zero1:5080,zero2:5080,zero3:5080 -o 1
alpha3:
image: dgraph/dgraph:latest
hostname: "alpha3"
volumes:
- data-volume-app3:/dgraph
ports:
- 8082:8082
- 9082:9082
command: dgraph alpha --my=alpha3:7082 --lru_mb=2048 --zero=zero1:5080,zero2:5080,zero3:5080 -o 2
alpha4:
image: dgraph/dgraph:latest
hostname: "alpha4"
volumes:
- data-volume-app4:/dgraph
ports:
- 8083:8083
- 9083:9083
command: dgraph alpha --my=alpha4:7083 --lru_mb=2048 --zero=zero1:5080,zero2:5080,zero3:5080 -o 3
alpha5:
image: dgraph/dgraph:latest
hostname: "alpha5"
volumes:
- data-volume-app5:/dgraph
ports:
- 8084:8084
- 9084:9084
command: dgraph alpha --my=alpha5:7084 --lru_mb=2048 --zero=zero1:5080,zero2:5080,zero3:5080 -o 4
alpha6:
image: dgraph/dgraph:latest
hostname: "alpha6"
volumes:
- data-volume-app6:/dgraph
ports:
- 8085:8085
- 9085:9085
command: dgraph alpha --my=alpha6:7085 --lru_mb=2048 --zero=zero1:5080,zero2:5080,zero3:5080 -o 5
ratel:
image: dgraph/dgraph:latest
hostname: "ratel"
ports:
- 8000:8000
command: dgraph-ratel
volumes:
data-volume1:
data-volume2:
data-volume3:
data-volume-app1:
data-volume-app2:
data-volume-app3:
data-volume-app4:
data-volume-app5:
data-volume-app6:
- 启动
docker-compose up -d
- 集群效果
- 说明
dgraph分不同的角色zero,以及alpha,以上是一个ha 的环境搭建,3个zero,6个alpha,以及配置,主要是方便学习,为了方便使用
同时添加了ratel
使用
方法与单机的类似,集群的数据是自动处理的
- schema 定义
schema.graphql
type Product {
productID: ID!
name: String @search(by: [term])
reviews: [Review] @hasInverse(field: about)
}
type Customer {
username: String! @id @search(by: [hash, regexp])
reviews: [Review] @hasInverse(field: by)
}
type Review {
id: ID!
about: Product!
by: Customer!
comment: String @search(by: [fulltext])
rating: Int @search
}
- deploy schema
curl -X POST localhost:8080/admin/schema -d '@schema.graphql'
- mutation
mutation {
addProduct(input: [
{ name: "GraphQL on Dgraph"},
{ name: "Dgraph: The GraphQL Database"}
]) {
product {
productID
name
}
}
addCustomer(input: [{ username: "Michael"}]) {
customer {
username
}
}
}
// 后边执行
mutation {
addReview(input: [{
by: {username: "Michael"},
about: { productID: "0x3"},
comment: "Fantastic, easy to install, worked great. Best GraphQL server available",
rating: 10}])
{
review {
comment
rating
by { username }
about { name }
}
}
}
- 数据查询
query {
queryProduct {
productID
name
reviews {
id
rating
comment
}
}
}
效果
说明
对于graphql 的地址,实际上我们可以基于lb 的模式做负载处理,这样可以更好的利用资源,以及集群的能力
一个参考模式
说明
https://github.com/dgraph-io/dgraph
https://graphql.dgraph.io/docs/quick-start/
https://dgraph.io/docs/design-concepts/
https://github.com/rongfengliang/dgraph-cluster-graphql-learning