Skip to content

Commit

Permalink
Improve cluster embedded demo
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Jan 16, 2019
1 parent c45d64c commit 0969113
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,19 @@ public class DemoClusterInitFunc implements InitFunc {

@Override
public void init() throws Exception {
// Init token client related data source.
// Register client dynamic rule data source.
initDynamicRuleProperty();

// Register token client related data source.
// Token client common config:
initClientConfigProperty();
// Token client assign config (e.g. target token server) retrieved from assign map:
initClientServerAssignProperty();

// Init token server related data source.
// Register token server related data source.
// Register dynamic rule data source supplier for token server:
registerClusterRuleSupplier();
// Token server transport config extracted from assign map:
initServerTransportConfigProperty();

// Init cluster state property for extracting mode from cluster map data source.
Expand Down Expand Up @@ -101,15 +106,16 @@ private void initServerTransportConfigProperty() {

private void registerClusterRuleSupplier() {
// Register cluster flow rule property supplier which creates data source by namespace.
// Flow rule dataId format: ${namespace}-flow-rules
ClusterFlowRuleManager.setPropertySupplier(namespace -> {
ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(remoteAddress, groupId,
flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
namespace + DemoConstants.FLOW_POSTFIX, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
return ds.getProperty();
});
// Register cluster parameter flow rule property supplier which creates data source by namespace.
ClusterParamFlowRuleManager.setPropertySupplier(namespace -> {
ReadableDataSource<String, List<ParamFlowRule>> ds = new NacosDataSource<>(remoteAddress, groupId,
paramDataId, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
namespace + DemoConstants.PARAM_FLOW_POSTFIX, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
return ds.getProperty();
});
}
Expand Down Expand Up @@ -147,6 +153,8 @@ private int extractMode(List<ClusterGroupEntity> groupList) {
if (groupList.stream().anyMatch(this::machineEqual)) {
return ClusterStateManager.CLUSTER_SERVER;
}
// If current machine belongs to any of the token server group, then it's token client.
// Otherwise it's unassigned, should be set to NOT_STARTED.
boolean canBeClient = groupList.stream()
.flatMap(e -> e.getClientSet().stream())
.anyMatch(e -> e.equals(getCurrentMachineId()));
Expand Down Expand Up @@ -180,8 +188,9 @@ private boolean machineEqual(/*@Valid*/ ClusterGroupEntity group) {
}

private String getCurrentMachineId() {
// Note: this may not work well for container-based env.
return HostNameUtil.getIp() + SEPARATOR + TransportConfig.getRuntimePort();
}

private static final String SEPARATOR = "@";
}
}

0 comments on commit 0969113

Please sign in to comment.