sequence
In this paper, we look nacos of DelegateConsistencyServiceImpl
ConsistencyService
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/ConsistencyService.java
public interface ConsistencyService {
/**
* Put a data related to a key to Nacos cluster
*
* @param key key of data, this key should be globally unique
* @param value value of data
* @throws NacosException
* @see
*/
void put(String key, Record value) throws NacosException;
/**
* Remove a data from Nacos cluster
*
* @param key key of data
* @throws NacosException
*/
void remove(String key) throws NacosException;
/**
* Get a data from Nacos cluster
*
* @param key key of data
* @return data related to the key
* @throws NacosException
*/
Datum get(String key) throws NacosException;
/**
* Listen for changes of a data
*
* @param key key of data
* @param listener callback of data change
* @throws NacosException
*/
void listen(String key, RecordListener listener) throws NacosException;
/**
* Cancel listening of a data
*
* @param key key of data
* @param listener callback of data change
* @throws NacosException
*/
void unlisten(String key, RecordListener listener) throws NacosException;
/**
* Tell the status of this consistency service
*
* @return true if available
*/
boolean isAvailable();
}
复制代码
- ConsistencyService定义了put、remove、get、listen、unlisten、isAvailable方法
DelegateConsistencyServiceImpl
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/DelegateConsistencyServiceImpl.java
@Service("consistencyDelegate")
public class DelegateConsistencyServiceImpl implements ConsistencyService {
@Autowired
private PersistentConsistencyService persistentConsistencyService;
@Autowired
private EphemeralConsistencyService ephemeralConsistencyService;
@Override
public void put(String key, Record value) throws NacosException {
mapConsistencyService(key).put(key, value);
}
@Override
public void remove(String key) throws NacosException {
mapConsistencyService(key).remove(key);
}
@Override
public Datum get(String key) throws NacosException {
return mapConsistencyService(key).get(key);
}
@Override
public void listen(String key, RecordListener listener) throws NacosException {
// this special key is listened by both:
if (KeyBuilder.SERVICE_META_KEY_PREFIX.equals(key)) {
persistentConsistencyService.listen(key, listener);
ephemeralConsistencyService.listen(key, listener);
return;
}
mapConsistencyService(key).listen(key, listener);
}
@Override
public void unlisten(String key, RecordListener listener) throws NacosException {
mapConsistencyService(key).unlisten(key, listener);
}
@Override
public boolean isAvailable() {
return ephemeralConsistencyService.isAvailable() && persistentConsistencyService.isAvailable();
}
private ConsistencyService mapConsistencyService(String key) {
return KeyBuilder.matchEphemeralKey(key) ? ephemeralConsistencyService : persistentConsistencyService;
}
}
复制代码
- DelegateConsistencyServiceImpl ConsistencyService implements the interface; which put, remove, get, listen, internal methods are used unlisten mapConsistencyService is used to determine further persistentConsistencyService ephemeralConsistencyService; isAvailable method of claim ephemeralConsistencyService which are available and persistentConsistencyService
summary
ConsistencyService defined put, remove, get, listen, unlisten, isAvailable method; DelegateConsistencyServiceImpl achieved ConsistencyService interfaces; the put, remove, get, listen, internal UNLISTEN methods use mapConsistencyService is determined using ephemeralConsistencyService further persistentConsistencyService; which isAvailable method of claim ephemeralConsistencyService and persistentConsistencyService are available