最近看了一下NebulaGraph图数据库连接的部分资料,发现还可以通过SessionPool的方式连接。下面是一个简单的DEMO记录。
组件项目
<!-- SpringBoot依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- Client依赖包 -->
<dependency>
<groupId>com.vesoft</groupId>
<artifactId>client</artifactId>
<version>3.8.4</version>
</dependency>
@Data
@ConfigurationProperties(prefix = "nebula-graph")
public class NebulaGraphProperties {
private String[] clusterNodes = null;
private String username;
private String password;
private List<CSessionPoolConfig> cSessionPoolConfigs = new ArrayList<>();
@Data
public static class CSessionPoolConfig {
private String spaceName;
private int minSessionSize = 10;
private int maxSessionSize = 100;
private boolean reconnect = true;
private int timeout = 0;
private int waitTime = 0;
private int cleanTime = 3600;
private int intervalTime = 0;
private int healthCheckTime = 600;
private int retryConnectTimes = 1;
private int retryTimes = 3;
}
}
- SessionPoolBeanDefinitionRegistrar类
public class SessionPoolBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private NebulaGraphProperties nebulaGraphProperties;
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
if (ObjectUtil.isNull(nebulaGraphProperties)) {
return;
}
List<NebulaGraphProperties.CSessionPoolConfig> cSessionPoolConfigs = nebulaGraphProperties.getCSessionPoolConfigs();
if (cSessionPoolConfigs.isEmpty()) {
return;
}
List<HostAddress> hostAddresses = new ArrayList<>();
String[] clusterNodes = nebulaGraphProperties.getClusterNodes();
for (int i = 0, len = clusterNodes.length; i < len; i++) {
String clusterNode = clusterNodes[i];
if (!clusterNode.contains(":")) {
continue;
}
String[] ipAndPort = clusterNode.split(":");
if (ipAndPort.length != 2 || !ipAndPort[1].matches("\\d+")) {
throw new RuntimeException("Invalid Nebula Graph Node " + clusterNode);
}
hostAddresses.add(new HostAddress(ipAndPort[0], Integer.parseInt(ipAndPort[1])));
}
String username = nebulaGraphProperties.getUsername();
String password = nebulaGraphProperties.getPassword();
cSessionPoolConfigs.forEach(cSessionPoolConfig -> {
String spaceName = cSessionPoolConfig.getSpaceName();
if (CharSequenceUtil.isBlank(spaceName)) {
return;
}
SessionPoolConfig sessionPoolConfig = new SessionPoolConfig(hostAddresses, spaceName, username, password);
sessionPoolConfig.setMinSessionSize(cSessionPoolConfig.getMinSessionSize());
sessionPoolConfig.setMaxSessionSize(cSessionPoolConfig.getMaxSessionSize());
sessionPoolConfig.setTimeout(cSessionPoolConfig.getTimeout());
sessionPoolConfig.setWaitTime(cSessionPoolConfig.getWaitTime());
sessionPoolConfig.setCleanTime(cSessionPoolConfig.getCleanTime());
sessionPoolConfig.setIntervalTime(cSessionPoolConfig.getIntervalTime());
sessionPoolConfig.setHealthCheckTime(cSessionPoolConfig.getHealthCheckTime());
sessionPoolConfig.setRetryConnectTimes(cSessionPoolConfig.getRetryConnectTimes());
sessionPoolConfig.setRetryTimes(cSessionPoolConfig.getRetryTimes());
sessionPoolConfig.setReconnect(cSessionPoolConfig.isReconnect());
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
rootBeanDefinition.setBeanClass(SessionPool.class);
rootBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(sessionPoolConfig);
beanDefinitionRegistry.registerBeanDefinition(String.format("%sSessionPool", spaceName), rootBeanDefinition);
});
}
@Override
public void setEnvironment(Environment environment) {
BindResult<NebulaGraphProperties> bindResult = Binder.get(environment)
.bind(nebulaGraphPropertiesPrefix(), NebulaGraphProperties.class);
if (ObjectUtil.isNotNull(bindResult) && bindResult.isBound()) {
nebulaGraphProperties = bindResult.get();
}
}
private String nebulaGraphPropertiesPrefix() {
return Objects.requireNonNull(
AnnotationUtils.getAnnotation(NebulaGraphProperties.class, ConfigurationProperties.class)).prefix();
}
}
@EnableConfigurationProperties({ NebulaGraphProperties.class })
@Import({ SessionPoolBeanDefinitionRegistrar.class })
@Configuration
public class NebulaGraphAutoConfiguration {
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.component.nebula.graph.config.NebulaGraphAutoConfiguration
业务项目
<!--ComponentNebulaGraph依赖包-->
<dependency>
<groupId>com.component</groupId>
<artifactId>component-nebula-graph</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
nebula-graph:
cluster-nodes:
- 192.168.0.1:9559
- 192.168.0.1:9669
username: root
password: 123456
c-session-pool-configs:
- space-name: tmp1_space_name
min-session-size: 10
max-session-size: 200
@Slf4j
@Service("nebulaGraphService")
public class NebulaGraphServiceImpl implements NebulaGraphService {
@Autowired
private SessionPool sessionPool;
protected String executeJsonWithParameter(String statement, Map<String, Object> paramsMap)
throws ClientServerIncompatibleException, AuthFailedException, BindSpaceFailedException, IOErrorException {
return sessionPool.executeJsonWithParameter(statement, paramsMap);
}
}
总体来说也是比较简单的。