Skip to content

Commit

Permalink
[ISSUE #4686] ApolloClientRegisterRepository active offline (#4855)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoyu <[email protected]>
  • Loading branch information
xuziyang and yu199195 authored Jul 17, 2023
1 parent 206c1b0 commit a62a04c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.dto.URIRegisterDTO;
import org.apache.shenyu.register.common.enums.EventType;
import org.apache.shenyu.register.common.path.RegisterPathConstants;
import org.apache.shenyu.spi.Join;

Expand All @@ -36,6 +37,7 @@
import java.util.Properties;
import java.util.Set;


/**
* apollo register center.
*/
Expand Down Expand Up @@ -84,18 +86,43 @@ private void initSubscribe() {
}

// monitor metadata changes
subscribeMetadata();
// monitor uri changes
subscribeUri();
}

private void subscribeMetadata() {
this.config.addChangeListener(changeEvent -> {
for (String changedKey : changeEvent.changedKeys()) {
// apollo has a bug and may push events that are not monitored, so there is this judgment.
if (!changedKey.startsWith(RegisterPathConstants.REGISTER_METADATA_INSTANCE_ROOT_PATH)) {
continue;
}
ConfigChange configChange = changeEvent.getChange(changedKey);
this.publishMetadata(configChange.getNewValue());
}
}, null, Collections.singleton(RegisterPathConstants.REGISTER_METADATA_INSTANCE_ROOT_PATH));
}

// monitor uri changes
private void subscribeUri() {
this.config.addChangeListener(changeEvent -> {
for (String changedKey : changeEvent.changedKeys()) {
// apollo has a bug and may push events that are not monitored, so there is this judgment.
if (!changedKey.startsWith(RegisterPathConstants.REGISTER_URI_INSTANCE_ROOT_PATH)) {
continue;
}
ConfigChange configChange = changeEvent.getChange(changedKey);
this.publishRegisterURI(configChange.getNewValue());
switch (configChange.getChangeType()) {
case ADDED:
case MODIFIED:
this.publishRegisterURI(configChange.getNewValue());
break;
case DELETED:
this.publishUnRegisterURI(configChange.getOldValue());
break;
default:
break;
}
}
}, null, Collections.singleton(RegisterPathConstants.REGISTER_URI_INSTANCE_ROOT_PATH));
}
Expand All @@ -105,8 +132,13 @@ private void publishMetadata(final String metadata) {
}

private void publishRegisterURI(final String uriMetadata) {
URIRegisterDTO uriRegisterDTO = GsonUtils.getInstance().fromJson(uriMetadata, URIRegisterDTO.class);
publisher.publish(Lists.newArrayList(uriRegisterDTO));
publisher.publish(Lists.newArrayList(GsonUtils.getInstance().fromJson(uriMetadata, URIRegisterDTO.class)));
}

private void publishUnRegisterURI(final String uriMetadata) {
URIRegisterDTO uriOffline = GsonUtils.getInstance().fromJson(uriMetadata, URIRegisterDTO.class);
uriOffline.setEventType(EventType.OFFLINE);
publisher.publish(Lists.newArrayList(uriOffline));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ public void createOrUpdateItem(final String key, final String value, final Strin
);
}

/**
* remove item from namespace.
* @param key item key
*/
public void removeItem(final String key) {
this.apolloOpenApiClient.removeItem(
apolloConfig.getAppId(),
apolloConfig.getEnv(),
apolloConfig.getClusterName(),
apolloConfig.getNamespace(),
key,
apolloConfig.getOperator()
);
}

/**
* publish item list in namespace.
* @param releaseTitle publish release title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void init(final ShenyuRegisterCenterConfig config) {
String clusterName = properties.getProperty("clusterName", ConfigConsts.CLUSTER_NAME_DEFAULT);
String namespace = properties.getProperty("namespace", ConfigConsts.NAMESPACE_APPLICATION);
String portalUrl = properties.getProperty("portalUrl");
String operator = properties.getProperty("operator", "apollo");

ApolloConfig apolloConfig = new ApolloConfig();
apolloConfig.setAppId(appId);
Expand All @@ -62,6 +63,7 @@ public void init(final ShenyuRegisterCenterConfig config) {
apolloConfig.setEnv(env);
apolloConfig.setClusterName(clusterName);
apolloConfig.setNamespace(namespace);
apolloConfig.setOperator(operator);

this.apolloClient = new ApolloClient(apolloConfig);
}
Expand All @@ -80,6 +82,14 @@ public void persistURI(final URIRegisterDTO registerDTO) {
LogUtils.info(LOGGER, "{} apollo client register uri success: {}", rpcType, registerDTO);
}

@Override
public void offline(final URIRegisterDTO offlineDTO) {
String rpcType = offlineDTO.getRpcType();
String contextPath = ContextPathUtils.buildRealNode(offlineDTO.getContextPath(), offlineDTO.getAppName());
unRegister(rpcType, contextPath, offlineDTO);
LogUtils.info(LOGGER, "{} apollo client unRegister uri success: {}", rpcType, offlineDTO);
}

private void registerURI(final String rpcType,
final String contextPath,
final URIRegisterDTO registerDTO) {
Expand All @@ -91,6 +101,17 @@ private void registerURI(final String rpcType,
LOGGER.info("register uri data success: {}", realNode);
}

private void unRegister(final String rpcType,
final String contextPath,
final URIRegisterDTO offlineDTO) {
String uriNodeName = buildURINodeName(offlineDTO);
String uriPath = RegisterPathConstants.buildURIParentPath(rpcType, contextPath);
String realNode = RegisterPathConstants.buildRealNode(uriPath, uriNodeName);
apolloClient.createOrUpdateItem(realNode, GsonUtils.getInstance().toJson(offlineDTO), "offline uri");
apolloClient.publishNamespace("publish config", "");
LOGGER.info("unRegister uri data success: {}", realNode);
}

private String buildURINodeName(final URIRegisterDTO registerDTO) {
String host = registerDTO.getHost();
int port = registerDTO.getPort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class ApolloConfig {
*/
private String token;

/**
* operator.
*/
private String operator;

/**
* get appId.
* @return appId
Expand Down Expand Up @@ -146,4 +151,20 @@ public String getToken() {
public void setToken(final String token) {
this.token = token;
}

/**
* get operator.
* @return operator
*/
public String getOperator() {
return operator;
}

/**
* set operator.
* @param operator operator
*/
public void setOperator(final String operator) {
this.operator = operator;
}
}

0 comments on commit a62a04c

Please sign in to comment.