nacos源码解读
版本基于2.x
实例注册接口
public class InstanceControllerV2{
//开关类 SwitchManager对该类进行管理
@Autowired
private SwitchDomain switchDomain;
/**
* 实例操作类
* InstanceOperatorClientImpl 2.x版本实现类
* InstanceOperatorServiceImpl 1.x版本实现类
* 都实现接口InstanceOperator
*/
@Autowired
private InstanceOperatorClientImpl instanceServiceV2;
@CanDistro
@PostMapping
@Secured(action = ActionTypes.WRITE)
public String register(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
@RequestParam String serviceName, @RequestParam String ip,
@RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster,
@RequestParam Integer port, @RequestParam(defaultValue = "true") Boolean healthy,
@RequestParam(defaultValue = "1") Double weight, @RequestParam(defaultValue = "true") Boolean enabled,
@RequestParam String metadata, @RequestParam Boolean ephemeral) throws Exception {
NamingUtils.checkServiceNameFormat(serviceName);
checkWeight(weight);
final Instance instance = InstanceBuilder.newBuilder().setServiceName(serviceName).setIp(ip)
.setClusterName(cluster).setPort(port).setHealthy(healthy).setWeight(weight).setEnabled(enabled)
.setMetadata(UtilsAndCommons.parseMetadata(metadata)).setEphemeral(ephemeral).build();
if (ephemeral == null) {
instance.setEphemeral((switchDomain.isDefaultInstanceEphemeral()));
}
//注册实例
instanceServiceV2.registerInstance(namespaceId, serviceName, instance);
return "ok";
}
}
实例操作类(2.x版本使用InstanceOperatorClientImpl)
public class InstanceOperatorClientImpl implements InstanceOperator {
/**
* 针对客户端的业务类
* EphemeralClientOperationServiceImpl 临时节点业务类
* PersistentClientOperationServiceImpl 持久节点业务类
* ClientOperationServiceProxy 临时、持久节点业务代理类
* 都实现了ClientOperationService接口
*/
private final ClientOperationService clientOperationService;
@Override
public void registerInstance(String namespaceId, String serviceName, Instance instance) {
boolean ephemeral = instance.isEphemeral();
String clientId = IpPortBasedClient.getClientId(instance.toInetAddr(), ephemeral);
createIpPortClientIfAbsent(clientId);
Service service = getService(namespaceId, serviceName, ephemeral);
//注册实例
clientOperationService.registerInstance(service, instance, clientId);
}
}
临时节点业务类
public class EphemeralClientOperationServiceImpl implements ClientOperationService {
private final ClientManager clientManager;
@Override
public void registerInstance(Service service, Instance instance, String clientId) {
Service singleton = ServiceManager