SpringBoot集成Nebula
建议模块
在这里给大家推荐一种方式, 每引入一种新的技术,建议新建一个模块, 来适配这种技术,对外提供接口,在调用的地方应用就可以, 不用搞的到处都是, 防止如果后续替换这种技术, 还要到处修改, 这样的话, 只需要增加一个模块, 对外提供一样的接口, 并替换依赖就可以
添加Maven依赖
<dependency>
<groupId>com.vesoft</groupId>
<artifactId>client</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
配合FastJson一起用比较方便, 为了解决fastjson漏洞问题, 使用1.2.83及其以上版本
增加yml配置
nebula:
address[0]:
host: 192.168.247.130
port: 9669
username: root
password: 123456
reconnect: false
space: knowledge_extraction_platform
address[0]: 因为是开发, 所以配置为单机的, 等部署生产的话, 可以增加address[1-..]来完成集群的配置
space: 图空间
新增配置类
主机和端口
package com.jd.knowledgeextractionplatform.nebulagraph.config;
import lombok.Data;
@Data
public class NebulaAddress {
private String host;
private Integer port;
}
解析配置文件的模型
package com.jd.knowledgeextractionplatform.nebulagraph.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.List;
@Data
@Configuration
@ConfigurationProperties(prefix = "nebula")
public class NebulaProperties {
private List<NebulaAddress> address;
private String username;
private String password;
private boolean reconnect;
private String space;
}
基于配置初始化会话和连接池
package com.jd.knowledgeextractionplatform.nebulagraph.config;
import com.jd.knowledgeextractionplatform.nebulagraph.constant.NebulaConstant;
import com.sun.javafx.binding.StringFormatter;
import com.vesoft.nebula.client.graph.NebulaPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import com.vesoft.nebula.client.graph.net.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import java.util.stream.Collectors;
@Slf4j
@Configuration
public class NebulaConfig {
@Bean
public NebulaPool nebulaPool(NebulaProperties nebulaProperties) throws Exception {
NebulaPool pool = new NebulaPool();
NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
nebulaPoolConfig.setMaxConnSize(1000);
boolean init = pool.init(nebulaProperties.getAddress().stream().map(d -> new HostAddress(d.getHost(), d.getPort())).collect(Collectors.toList()), nebulaPoolConfig);
if (!init){
throw new RuntimeException("NebulaGraph init err !");
}else {
log.info("NebulaGraph init Success !");
}
return pool;
}
@Bean
@Scope(scopeName = "prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session session(NebulaPool nebulaPool, NebulaProperties nebulaProperties) {
try {
Session session = nebulaPool.getSession(nebulaProperties.getUsername(), nebulaProperties.getPassword(), nebulaProperties.isReconnect());
session.execute(StringFormatter.concat(NebulaConstant.USE, nebulaProperties.getSpace(), NebulaConstant.SEMICOLON).getValue());
return session;
} catch (Exception e) {
log.error("get nebula session err , {} ", e.toString());
}
return null;
}
}