import static org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1.TOKEN_SERIALIZE_RESULT_TO_STRING;
import java.util.HashMap;
import java.util.Map;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.MessageSerializer;
import org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1;
import org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* Gremlin配置
*
*/
@ConditionalOnProperty(value = "gremlin.enabled", havingValue = "true")
@Configuration
public class GremlinConfig {
@Autowired
private Environment env;
@Bean
public Cluster cluster() {
String gremlinServerIp = env.getProperty("gremlinServerIp");
Integer gremlinServerPort = Integer.parseInt(env.getProperty("gremlinServerPort"));
String gremlinServerUserName = env.getProperty("gremlinServerUserName");
String gremlinServerPassword = env.getProperty("gremlinServerPassword");
// 设置正确的 serializer
MessageSerializer<GraphBinaryMapper> serializer = new GraphBinaryMessageSerializerV1();
Map<String, Object> config = new HashMap<>();
config.put(TOKEN_SERIALIZE_RESULT_TO_STRING, true);
serializer.configure(config, null);
Cluster cluster = Cluster.build()
.addContactPoint(gremlinServerIp).port(gremlinServerPort)
.credentials(gremlinServerUserName, gremlinServerPassword)
.serializer(serializer)
.create();
return cluster;
}
@Bean
public Client client(Cluster cluster) {
Client client = cluster.connect();
return client;
}
}
pom依赖配置
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.6.2</version>
</dependency>
版权声明:本文为cg_Amaz1ng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。