You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by "moremind (via GitHub)" <gi...@apache.org> on 2024/07/02 18:24:36 UTC
Re: [PR] [type:feature] ShenYu Admin Cluster [shenyu]
moremind commented on code in PR #5544:
URL: https://github.com/apache/shenyu/pull/5544#discussion_r1662619360
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ClusterZookeeperConfiguration.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.config;
+
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.config.properties.ClusterZookeeperProperties;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterSelectMasterServiceZookeeperImpl;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperClient;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperConfig;
+import org.apache.shenyu.admin.mode.cluster.service.ClusterSelectMasterService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
+
+import java.util.Objects;
+
+/**
+ * The type Cluster zookeeper configuration.
+ */
+@Configuration(proxyBeanMethods = false)
+@EnableConfigurationProperties({ClusterProperties.class, ClusterZookeeperProperties.class})
+@ConditionalOnProperty(value = {"shenyu.cluster.type"}, havingValue = "zookeeper", matchIfMissing = false)
+public class ClusterZookeeperConfiguration {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ClusterZookeeperConfiguration.class);
+
+ private static final String LOCK_PATH = "/shenyu-lock";
Review Comment:
shenyu-cluster-lock
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/websocket/WebsocketCollector.java:
##########
@@ -84,17 +88,50 @@ private static String getClientIp(final Session session) {
*/
@OnMessage
public void onMessage(final String message, final Session session) {
- if (!Objects.equals(message, DataEventTypeEnum.MYSELF.name())) {
+ if (!Objects.equals(message, DataEventTypeEnum.MYSELF.name())
+ && !Objects.equals(message, DataEventTypeEnum.CLUSTER.name())) {
return;
}
- try {
- ThreadLocalUtils.put(SESSION_KEY, session);
- SpringBeanUtils.getInstance().getBean(SyncDataService.class).syncAll(DataEventTypeEnum.MYSELF);
- } finally {
- ThreadLocalUtils.clear();
- }
+ if (Objects.equals(message, DataEventTypeEnum.CLUSTER.name())) {
+ LOG.info("websocket fetching cluster info...");
+ // check if this node is master
+ boolean isMaster = true;
+ String runningMode = "standalone";
+ String masterUrl = "";
+ ClusterProperties clusterProperties = SpringBeanUtils.getInstance().getBean(ClusterProperties.class);
+ if (clusterProperties.isEnabled()) {
+ ClusterSelectMasterService clusterSelectMasterService = SpringBeanUtils.getInstance().getBean(ClusterSelectMasterService.class);
+ masterUrl = clusterSelectMasterService.getMasterUrl();
+ isMaster = clusterSelectMasterService.isMaster();
+ runningMode = "cluster";
+ }
+ Map<String, Object> map = Maps.newHashMap();
+ map.put("eventType", DataEventTypeEnum.CLUSTER.name());
+ map.put("isMaster", isMaster);
+ map.put("runningMode", runningMode);
+ map.put("masterUrl", masterUrl
+ .replace("http", "ws")
+ .replace("https", "ws")
+ .concat("/websocket"));
+ if (isMaster) {
+ ThreadLocalUtils.put(SESSION_KEY, session);
+ }
+// send(JsonUtils.toJson(map), DataEventTypeEnum.MYSELF);
Review Comment:
remove this
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ClusterZookeeperConfiguration.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.config;
+
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.config.properties.ClusterZookeeperProperties;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterSelectMasterServiceZookeeperImpl;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperClient;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperConfig;
+import org.apache.shenyu.admin.mode.cluster.service.ClusterSelectMasterService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
+
+import java.util.Objects;
+
+/**
+ * The type Cluster zookeeper configuration.
+ */
+@Configuration(proxyBeanMethods = false)
+@EnableConfigurationProperties({ClusterProperties.class, ClusterZookeeperProperties.class})
+@ConditionalOnProperty(value = {"shenyu.cluster.type"}, havingValue = "zookeeper", matchIfMissing = false)
+public class ClusterZookeeperConfiguration {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ClusterZookeeperConfiguration.class);
+
+ private static final String LOCK_PATH = "/shenyu-lock";
+
+ /**
+ * Shenyu Admin distributed lock by spring-integration-zookeeper.
+ *
+ * @param clusterZookeeperClient the cluster zookeeper client
+ * @return the shenyu Admin zookeeper lock registry
+ */
+ @Bean
+ public ZookeeperLockRegistry zookeeperLockRegistry(final ClusterZookeeperClient clusterZookeeperClient) {
+ return new ZookeeperLockRegistry(clusterZookeeperClient.getClient(), LOCK_PATH);
+ }
+
+ /**
+ * Shenyu cluster select master service.
+ *
+ * @param clusterProperties the cluster properties
+ * @param zookeeperLockRegistry the zookeeper lock registry
+ * @param clusterZookeeperClient cluster zookeeper client
+ * @return the shenyu cluster select master service
+ */
+ @Bean
+ public ClusterSelectMasterService clusterSelectMasterZookeeperService(final ClusterProperties clusterProperties,
+ final ZookeeperLockRegistry zookeeperLockRegistry,
+ final ClusterZookeeperClient clusterZookeeperClient) {
+ return new ClusterSelectMasterServiceZookeeperImpl(clusterProperties, zookeeperLockRegistry, clusterZookeeperClient);
+ }
+
+ /**
+ * register zkClient in spring ioc.
+ *
+ * @param clusterZookeeperProperties the zookeeper configuration
+ * @return ClusterZookeeperClient {@linkplain ClusterZookeeperClient}
+ */
+ @Bean
+ public ClusterZookeeperClient clusterZookeeperClient(final ClusterZookeeperProperties clusterZookeeperProperties) {
+ int sessionTimeout = Objects.isNull(clusterZookeeperProperties.getSessionTimeout()) ? 3000 : clusterZookeeperProperties.getSessionTimeout();
Review Comment:
not use magic number
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/service/ShenyuClusterService.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.mode.cluster.service;
+
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.mode.ShenyuRunningModeService;
+import org.apache.shenyu.admin.service.impl.UpstreamCheckService;
+import org.apache.shenyu.admin.service.manager.LoadServiceDocEntry;
+import org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
+import org.apache.shenyu.common.exception.ShenyuException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class ShenyuClusterService implements ShenyuRunningModeService {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ShenyuClusterService.class);
+
+ private final ClusterSelectMasterService shenyuClusterSelectMasterService;
+
+ private final UpstreamCheckService upstreamCheckService;
+
+ private final LoadServiceDocEntry loadServiceDocEntry;
+
+ private final ScheduledExecutorService executorService;
+
+ private final ClusterProperties clusterProperties;
+
+ public ShenyuClusterService(final ClusterSelectMasterService shenyuClusterSelectMasterService,
+ final UpstreamCheckService upstreamCheckService,
+ final LoadServiceDocEntry loadServiceDocEntry,
+ final ClusterProperties clusterProperties) {
+ this.shenyuClusterSelectMasterService = shenyuClusterSelectMasterService;
+ this.upstreamCheckService = upstreamCheckService;
+ this.loadServiceDocEntry = loadServiceDocEntry;
+ this.clusterProperties = clusterProperties;
+ this.executorService = new ScheduledThreadPoolExecutor(1,
+ ShenyuThreadFactory.create("master-selector", true));
+ }
+
+ /**
+ * start master select task.
+ *
+ * @param host host
+ * @param port port
+ * @param contextPath contextPath
+ */
+ public void startSelectMasterTask(final String host, final String port, final String contextPath) {
+ LOG.info("starting select master task");
+ // schedule task 15s
+ executorService.scheduleAtFixedRate(() -> doSelectMaster(host, port, contextPath),
+ 0,
+ clusterProperties.getSelectPeriod(),
+ TimeUnit.SECONDS);
+ }
+
+ private void doSelectMaster(final String host, final String port, final String contextPath) {
+ // try getting lock
+ try {
+ boolean selected = shenyuClusterSelectMasterService.selectMaster(host, port, contextPath);
+ if (!selected) {
+ LOG.info("select master fail, wait for next period");
+// TimeUnit.SECONDS.sleep(clusterProperties.getSelectPeriod());
Review Comment:
remove this
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/service/ShenyuClusterService.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.mode.cluster.service;
+
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.mode.ShenyuRunningModeService;
+import org.apache.shenyu.admin.service.impl.UpstreamCheckService;
+import org.apache.shenyu.admin.service.manager.LoadServiceDocEntry;
+import org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
+import org.apache.shenyu.common.exception.ShenyuException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class ShenyuClusterService implements ShenyuRunningModeService {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ShenyuClusterService.class);
+
+ private final ClusterSelectMasterService shenyuClusterSelectMasterService;
+
+ private final UpstreamCheckService upstreamCheckService;
+
+ private final LoadServiceDocEntry loadServiceDocEntry;
+
+ private final ScheduledExecutorService executorService;
+
+ private final ClusterProperties clusterProperties;
+
+ public ShenyuClusterService(final ClusterSelectMasterService shenyuClusterSelectMasterService,
+ final UpstreamCheckService upstreamCheckService,
+ final LoadServiceDocEntry loadServiceDocEntry,
+ final ClusterProperties clusterProperties) {
+ this.shenyuClusterSelectMasterService = shenyuClusterSelectMasterService;
+ this.upstreamCheckService = upstreamCheckService;
+ this.loadServiceDocEntry = loadServiceDocEntry;
+ this.clusterProperties = clusterProperties;
+ this.executorService = new ScheduledThreadPoolExecutor(1,
+ ShenyuThreadFactory.create("master-selector", true));
+ }
+
+ /**
+ * start master select task.
+ *
+ * @param host host
+ * @param port port
+ * @param contextPath contextPath
+ */
+ public void startSelectMasterTask(final String host, final String port, final String contextPath) {
+ LOG.info("starting select master task");
+ // schedule task 15s
+ executorService.scheduleAtFixedRate(() -> doSelectMaster(host, port, contextPath),
+ 0,
+ clusterProperties.getSelectPeriod(),
+ TimeUnit.SECONDS);
+ }
+
+ private void doSelectMaster(final String host, final String port, final String contextPath) {
+ // try getting lock
+ try {
+ boolean selected = shenyuClusterSelectMasterService.selectMaster(host, port, contextPath);
+ if (!selected) {
+ LOG.info("select master fail, wait for next period");
+// TimeUnit.SECONDS.sleep(clusterProperties.getSelectPeriod());
+// selected = shenyuClusterSelectMasterService.selectMaster(host, port, contextPath);
+ return;
+ }
+
+ LOG.info("select master success");
+
+ // start upstream check task
+ upstreamCheckService.setup();
+
+ // load api
+ loadServiceDocEntry.loadApiDocument();
+
+ boolean renewed = shenyuClusterSelectMasterService.checkMasterStatus();
+
+ while (renewed) {
+// try {
+ // sleeps 10s then renew the lock
+ TimeUnit.SECONDS.sleep(clusterProperties.getSelectPeriod());
+
+ renewed = shenyuClusterSelectMasterService.checkMasterStatus();
+ if (renewed) {
+ LOG.info("renew master success");
+ }
+// } catch (Exception e) {
+// // if renew fail, remove local master flag
Review Comment:
remove this
##########
shenyu-sync-data-center/shenyu-sync-data-websocket/src/main/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClient.java:
##########
@@ -129,71 +137,118 @@ public boolean connectBlocking() {
}
return success;
}
-
+
@Override
public void onOpen(final ServerHandshake serverHandshake) {
+ LOG.info("websocket connection server[{}] is opened, sending sync msg", this.getURI().toString());
+ send(DataEventTypeEnum.CLUSTER.name());
if (!alreadySync) {
send(DataEventTypeEnum.MYSELF.name());
alreadySync = true;
}
}
-
+
@Override
public void onMessage(final String result) {
- handleResult(result);
+// LOG.info("onMessage server[{}] result({})", this.getURI().toString(), result);
+ Map<String, Object> jsonToMap = JsonUtils.jsonToMap(result);
+ Object eventType = jsonToMap.get("eventType");
+ if (Objects.equals("CLUSTER", eventType)) {
Review Comment:
not use magic string, you can create constant variable or enum include `CLUSTER`, `STANDALONE`
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java:
##########
@@ -147,22 +147,22 @@ public UpstreamCheckService(final SelectorMapper selectorMapper,
this.scheduledTime = Integer.parseInt(props.getProperty(Constants.SCHEDULED_TIME, Constants.SCHEDULED_TIME_VALUE));
this.registerType = shenyuRegisterCenterConfig.getRegisterType();
zombieRemovalTimes = Integer.parseInt(props.getProperty(Constants.ZOMBIE_REMOVAL_TIMES, Constants.ZOMBIE_REMOVAL_TIMES_VALUE));
- if (REGISTER_TYPE_HTTP.equalsIgnoreCase(registerType)) {
- setup();
- }
}
/**
* Set up.
*/
public void setup() {
- if (checked) {
- this.fetchUpstreamData();
- executor = new ScheduledThreadPoolExecutor(1, ShenyuThreadFactory.create("scheduled-upstream-task", false));
- scheduledFuture = executor.scheduleWithFixedDelay(this::scheduled, 10, scheduledTime, TimeUnit.SECONDS);
-
- ThreadFactory requestFactory = ShenyuThreadFactory.create("upstream-health-check-request", true);
- invokeExecutor = new ScheduledThreadPoolExecutor(this.scheduledThreads, requestFactory);
+ if (REGISTER_TYPE_HTTP.equalsIgnoreCase(registerType)) {
Review Comment:
change the conditon `if (REGISTER_TYPE_HTTP.equalsIgnoreCase(registerType) && checked)) {`
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterSelectMasterServiceZookeeperImpl.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.mode.cluster.impl.zookeeper;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.mode.cluster.service.ClusterSelectMasterService;
+import org.apache.shenyu.admin.model.dto.ClusterMasterDTO;
+import org.apache.shenyu.common.utils.JsonUtils;
+import org.apache.zookeeper.CreateMode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
+
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+
+public class ClusterSelectMasterServiceZookeeperImpl implements ClusterSelectMasterService {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClusterSelectMasterServiceZookeeperImpl.class);
+
+ private static final String MASTER_LOCK_KEY = "cluster/master";
+
+ private static final String MASTER_INFO = "/cluster/master/info";
Review Comment:
LOCK=shenyu_cluster_lock
MASTER_INFO = shenyu_cluster_lock/master/info
MASTER_LOCK_KEY = shenyu_cluster_lock/master
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/DataChangedEventDispatcher.java:
##########
@@ -17,42 +17,67 @@
package org.apache.shenyu.admin.listener;
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.mode.cluster.service.ClusterSelectMasterService;
import org.apache.shenyu.admin.service.manager.LoadServiceDocEntry;
import org.apache.shenyu.common.dto.AppAuthData;
+import org.apache.shenyu.common.dto.DiscoverySyncData;
+import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.common.dto.ProxySelectorData;
import org.apache.shenyu.common.dto.RuleData;
import org.apache.shenyu.common.dto.SelectorData;
-import org.apache.shenyu.common.dto.MetaData;
-import org.apache.shenyu.common.dto.DiscoverySyncData;
-import org.apache.shenyu.common.dto.ProxySelectorData;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
+import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
+import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import java.util.Objects;
/**
* Event forwarders, which forward the changed events to each ConfigEventListener.
*/
@Component
public class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DataChangedEventDispatcher.class);
private final ApplicationContext applicationContext;
private List<DataChangedListener> listeners;
+
+ @Resource
+ private ClusterProperties clusterProperties;
+
+ @Resource
+ @Nullable
+ private ClusterSelectMasterService shenyuClusterSelectMasterService;
public DataChangedEventDispatcher(final ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
@SuppressWarnings("unchecked")
- public void onApplicationEvent(final DataChangedEvent event) {
+ public void onApplicationEvent(@NotNull final DataChangedEvent event) {
for (DataChangedListener listener : listeners) {
+ if (!(listener instanceof AbstractDataChangedListener)) {
+ if (clusterProperties.isEnabled()
Review Comment:
change the condition xx&&yy&&zz
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/jdbc/ClusterSelectMasterServiceJdbcImpl.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.mode.cluster.impl.jdbc;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.mode.cluster.impl.jdbc.mapper.ClusterMasterMapper;
+import org.apache.shenyu.admin.mode.cluster.service.ClusterSelectMasterService;
+import org.apache.shenyu.admin.model.dto.ClusterMasterDTO;
+import org.apache.shenyu.admin.model.entity.ClusterMasterDO;
+import org.apache.shenyu.admin.transfer.ClusterMasterTransfer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.integration.jdbc.lock.JdbcLockRegistry;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.util.Objects;
+import java.util.concurrent.locks.Lock;
+
+public class ClusterSelectMasterServiceJdbcImpl implements ClusterSelectMasterService {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClusterSelectMasterServiceJdbcImpl.class);
+
+ private static final String MASTER_LOCK_KEY = "shenyu:cluster:master";
+
+ private static final String MASTER_ID = "1";
+
+ private final ClusterProperties clusterProperties;
+
+ private final JdbcLockRegistry jdbcLockRegistry;
+
+ private final ClusterMasterMapper clusterMasterMapper;
+
+ private final Lock clusterMasterLock;
+
+ private volatile boolean masterFlag;
+
+ public ClusterSelectMasterServiceJdbcImpl(final ClusterProperties clusterProperties,
+ final JdbcLockRegistry jdbcLockRegistry,
+ final ClusterMasterMapper clusterMasterMapper) {
+ this.clusterProperties = clusterProperties;
+ this.jdbcLockRegistry = jdbcLockRegistry;
+ this.clusterMasterMapper = clusterMasterMapper;
+ this.clusterMasterLock = jdbcLockRegistry.obtain(MASTER_LOCK_KEY);
+ }
+
+ @Override
+ public boolean selectMaster() {
+ masterFlag = clusterMasterLock.tryLock();
+ LOG.info("select master result: {}", masterFlag);
+ return masterFlag;
+ }
+
+ @Override
+ public boolean selectMaster(final String masterHost, final String masterPort, final String contextPath) {
+ masterFlag = clusterMasterLock.tryLock();
+ if (masterFlag) {
+ Timestamp now = Timestamp.valueOf(LocalDateTime.now());
+ ClusterMasterDO masterDO = ClusterMasterDO.builder()
+ .id(MASTER_ID)
+ .masterHost(masterHost)
+ .masterPort(masterPort)
+ .contextPath(contextPath)
+ .dateCreated(now)
+ .dateUpdated(now)
+ .build();
+ try {
+ clusterMasterMapper.insert(masterDO);
+ } catch (Exception e) {
+ clusterMasterMapper.updateSelective(masterDO);
+ }
+ }
+ return masterFlag;
+ }
+
+ @Override
+ public boolean checkMasterStatus() throws IllegalStateException {
+ if (masterFlag) {
+ jdbcLockRegistry.renewLock(MASTER_LOCK_KEY);
+ }
+ return masterFlag;
+ }
+
+ @Override
+ public boolean releaseMaster() {
+ if (masterFlag) {
+ clusterMasterLock.unlock();
+ masterFlag = false;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean isMaster() {
+ if (!clusterProperties.isEnabled()) {
+ return true;
+ }
+ return masterFlag;
+ }
+
+ @Override
+ public ClusterMasterDTO getMaster() {
+ ClusterMasterDO masterDO = clusterMasterMapper.selectById(MASTER_ID);
+ return Objects.isNull(masterDO) ? new ClusterMasterDTO() : ClusterMasterTransfer.INSTANCE.mapToDTO(masterDO);
+ }
+
+ @Override
+ public String getMasterUrl() {
+ ClusterMasterDO master = clusterMasterMapper.selectById(MASTER_ID);
+ String contextPath = master.getContextPath();
+ if (StringUtils.isEmpty(contextPath)) {
+ return "http://" + master.getMasterHost() + ":" + master.getMasterPort();
Review Comment:
consider the http schema
##########
shenyu-sync-data-center/shenyu-sync-data-websocket/src/main/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClient.java:
##########
@@ -129,71 +137,118 @@ public boolean connectBlocking() {
}
return success;
}
-
+
@Override
public void onOpen(final ServerHandshake serverHandshake) {
+ LOG.info("websocket connection server[{}] is opened, sending sync msg", this.getURI().toString());
+ send(DataEventTypeEnum.CLUSTER.name());
if (!alreadySync) {
send(DataEventTypeEnum.MYSELF.name());
alreadySync = true;
}
}
-
+
@Override
public void onMessage(final String result) {
- handleResult(result);
+// LOG.info("onMessage server[{}] result({})", this.getURI().toString(), result);
Review Comment:
open this?
##########
shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ClusterZookeeperConfiguration.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.config;
+
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import org.apache.shenyu.admin.config.properties.ClusterZookeeperProperties;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterSelectMasterServiceZookeeperImpl;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperClient;
+import org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperConfig;
+import org.apache.shenyu.admin.mode.cluster.service.ClusterSelectMasterService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
+
+import java.util.Objects;
+
+/**
+ * The type Cluster zookeeper configuration.
+ */
+@Configuration(proxyBeanMethods = false)
+@EnableConfigurationProperties({ClusterProperties.class, ClusterZookeeperProperties.class})
+@ConditionalOnProperty(value = {"shenyu.cluster.type"}, havingValue = "zookeeper", matchIfMissing = false)
+public class ClusterZookeeperConfiguration {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ClusterZookeeperConfiguration.class);
+
+ private static final String LOCK_PATH = "/shenyu-lock";
+
+ /**
+ * Shenyu Admin distributed lock by spring-integration-zookeeper.
+ *
+ * @param clusterZookeeperClient the cluster zookeeper client
+ * @return the shenyu Admin zookeeper lock registry
+ */
+ @Bean
+ public ZookeeperLockRegistry zookeeperLockRegistry(final ClusterZookeeperClient clusterZookeeperClient) {
+ return new ZookeeperLockRegistry(clusterZookeeperClient.getClient(), LOCK_PATH);
+ }
+
+ /**
+ * Shenyu cluster select master service.
+ *
+ * @param clusterProperties the cluster properties
+ * @param zookeeperLockRegistry the zookeeper lock registry
+ * @param clusterZookeeperClient cluster zookeeper client
+ * @return the shenyu cluster select master service
+ */
+ @Bean
+ public ClusterSelectMasterService clusterSelectMasterZookeeperService(final ClusterProperties clusterProperties,
+ final ZookeeperLockRegistry zookeeperLockRegistry,
+ final ClusterZookeeperClient clusterZookeeperClient) {
+ return new ClusterSelectMasterServiceZookeeperImpl(clusterProperties, zookeeperLockRegistry, clusterZookeeperClient);
+ }
+
+ /**
+ * register zkClient in spring ioc.
+ *
+ * @param clusterZookeeperProperties the zookeeper configuration
+ * @return ClusterZookeeperClient {@linkplain ClusterZookeeperClient}
+ */
+ @Bean
+ public ClusterZookeeperClient clusterZookeeperClient(final ClusterZookeeperProperties clusterZookeeperProperties) {
+ int sessionTimeout = Objects.isNull(clusterZookeeperProperties.getSessionTimeout()) ? 3000 : clusterZookeeperProperties.getSessionTimeout();
Review Comment:
you can init the value
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org