完成安装嬴图Go SDK并设置好一个运行的嬴图实例后,可以开始将您的应用连接到嬴图数据库。
代码配置连接
连接到集群
func TestMisc(t *testing.T) {
config := configuration.NewUltipaConfig(&configuration.UltipaConfig{
Hosts: []string{"192.168.1.85:60061", "192.168.1.86:60061", "192.168.1.87:60061"},
Username: "***",
Password: "***",
})
conn, _ := sdk.NewUltipa(config)
testResult, _ := conn.Test()
println(testResult)
}
连接到嬴图Cloud
func GetClient1(hosts []string, graphName string) (*api.UltipaAPI, error) {
var err error
config, _ := configuration.NewUltipaConfig(&configuration.UltipaConfig{
Hosts: []string{"xaznryn5s.us-east-1.cloud.ultipa.com:60010"},
Username: "***",
Password: "***",
DefaultGraph: "Sample_Graphset",
Debug: true,
})
client, err = sdk.NewUltipa(config)
if err != nil {
log.Fatalln(err)
}
return client, err
}
配置项
下面列出了UltipaConfig
的所有配置项:
项目 |
类型 |
默认值 |
描述 |
---|---|---|---|
Hosts |
[]string | 数据库主机地址或URI(不包含https:// 或http:// )。使用逗号分隔多个实例地址。必需项 |
|
Username |
string | 主机验证用户名。必需项 | |
Password |
string | 主机验证密码。必需项 | |
PasswordEncrypt |
string | MD5 | 驱动程序密码加密方式。支持MD5 、LDAP 和 NOTHING 。内容为空时将使用NOTHING |
DefaultGraph |
string | 数据库默认使用图集 | |
Crt |
[]byte | 加密消息的证书文件 | |
MaxRecvSize |
int | 10MB | 接收数据时最大值(兆字节) |
Consistency |
bool | FALSE | 是否使用主节点以确保一致性读取 |
CurrentGraph |
string | default | 当前图集的名称 |
CurrentClusterId |
string | 名称服务器的集群ID | |
Timeout |
int32 | 1000 | 请求超时时间阈值(单位为秒) |
Debug |
bool | FALSE | 是否启用调试模式 |
HeartBeat |
int | 0 | 所有实例的心跳间隔,单位为毫秒;设置为0则关闭心跳机制 |
YAML配置文件
YAML配置文件存储了连接到嬴图数据库所需的服务器信息。
YAML中的变量 | UltipaConfig中的项目 |
---|---|
hosts |
Hosts |
username |
Username |
password |
Password |
default_graph |
DefaultGraph |
crt |
Crt |
max_recv_size |
MaxRecvSize |
consistency |
Consistency |
current_graph |
CurrentGraph |
current_cluster_id |
CurrentClusterId |
timeout |
Timeout |
debug |
Debug |
heart_beat |
HeartBeat |
驱动程序根据YAML文件中的指定配置而创建。YAML文件应放置在当前Go文件的目录下。
示例YAML配置文件testConfig.yml
:
hosts:
- "192.168.1.85:60061"
- "192.168.1.86:60061"
- "192.168.1.87:60061"
username: ***
password: ***
default_graph: amz
timeout:: 300
func TestMisc(t *testing.T) {
config, _ := configuration.LoadConfigFromYAML("./testConfig.yml")
conn, _ := sdk.NewUltipa(config)
testResult, _ := conn.Test()
println(testResult)
}
测试连接
使用Connection
对象的test()
方法可以检查连接。
myTest, err := conn.Test(nil)
if err != nil {
println(err)
}
println("Test succeeds:", myTest.IsSuccess())
Test succeeds: true