一、配置属性类
@Configuration
@ConfigurationProperties(prefix = "nebula")
@ConditionalOnProperty("nebula.address")
@Data
public class NebulaProperties {
private String address;
private String userName;
private String passworld;
private String spaceName;
private int minConnsSize;
private int maxConnSize;
private int timeout;
private int idleTime;
}
二、初始化数据库连接
@Slf4j
@Configuration
@EnableConfigurationProperties({NebulaProperties.class})
@RequiredArgsConstructor
public class NebulaConfig {
private final NebulaProperties nebulaProperties;
private NebulaPool pool;
private Session session;
private String spaceName;
// 服务一启动会执行该方法
@PostConstruct
public void init() {
pool = new NebulaPool();
session = null;
spaceName = nebulaProperties.getSpaceName();
log.info("执行init方法,spaceName="+spaceName);
}
@PreDestroy
public void destroy() {
if (session != null) {
session.release();
}
pool.close();
}
@Bean
@ConditionalOnProperty("nebula.address")
public Session session() {
try {
NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
nebulaPoolConfig.setMaxConnSize(nebulaProperties.getMaxConnSize());
pool.init(assemblyAddress(nebulaProperties.getAddress()), nebulaPoolConfig);
session = pool.getSession(nebulaProperties.getUserName(), nebulaProperties.getPassworld(), false);
//创建数据库
dbCreate();
} catch (Exception e) {
log.info("address:{},username:{},password:{}",nebulaProperties.getAddress(),nebulaProperties.getUserName(),nebulaProperties.getPassworld());
log.error("session异常:"+e.getMessage());
e.printStackTrace();
}
return session;
}
/**
* 使用SPACE
* @return
*/
public String useSpace(){
return "USE "+spaceName+";";
}
public ResultSet execute(String ngql){
log.info("执行execute方法,ngql="+ngql);
ResultSet resp = null;
try {
resp = this.session.execute(ngql);
if (!resp.isSucceeded()) {
log.error(String.format("Execute: `%s', failed: %s", ngql, resp.getErrorMessage()));
System.out.println(resp.getErrorMessage());
}
}catch (Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
log.error("execute异常");
log.error(e.getMessage());
}
return resp;
}
private List<HostAddress> assemblyAddress(String address) {
String[] split = address.split(",");
List<HostAddress> addresses = new ArrayList<>(split.length);
for (int i=0; i< split.length; i++ ) {
String[] host = split[i].split(":");
addresses.add(new HostAddress(host[0],Integer.valueOf(host[1])));
}
return addresses;
}
/**
* 创建数据库
*/
private void dbCreate(){
if(StringUtils.isEmpty(spaceName)){
log.error("配置文件没有配置SPACE_NAME,退出程序");
System.out.println("配置文件没有配置SPACE_NAME,退出程序");
System.exit(1);
}
String sql = packageCreateSql();
//执行SQL
this.execute(sql);
}
/**
* 组装创建SQL
* @return
*/
private String packageCreateSql(){
StringBuilder builder = new StringBuilder();
String createSpaceSql = MessageFormat.format(RelationConstant.CREATE_SPACE, spaceName);
builder.append(createSpaceSql);
builder.append(useSpace());
String createMemberTagSql = RelationConstant.CREATE_MEMBER_TAG;
builder.append(createMemberTagSql);
return builder.toString();
}
}
网友评论