@Component
public class RaftStore implements Closeable{
...
private final Properties meta = new Properties();
private static final String META_FILE_NAME= DATA_BASE_DIR+File.separator+"meta.properties";
private static final String CACHE_DIR = DATA_BASE_DIR+File.separator+"data";
public synchronized void write(final Datum datum) throws Exception{
String namespaceId = KeyBuilder.getNamespace(datum.key);
File cacheFile = new File(cacheFileName(namespaceId,datum.key));
if(!cacheFile.exists()&&!cacheFile.getParentFile().mkdirs()&&!cacheFile.createNewFile()){
MetricsMonitor.getdiskException().increment();
throw new IllegalStateException();
}
FileChannel fc = null;
ByteBuffer data;
data = ByteBuffer.wrap(JacksonUtils.toJson(datum).getBytes(StandardCharsets.UTF_8));
try{
fc = new FileOutputStream(cacheFile,false)
fc.write(data,data.position());
fc.force();
}catch(Exception e){
MetricsMonitor.getDiskException().increment();
throw e;
}finally{
if(fc != null){
fc.close();
}
}
}
}
用文件存储Nacos元数据
最新推荐文章于 2024-08-15 07:30:00 发布