Update token client interface in sentinel-core

- Add `start` and `stop` method for automatic control
- Update TokenClientProvider using SpiLoader

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2018-12-09 21:42:29 +08:00
parent b973aca1f5
commit 9f2678eb6c
2 changed files with 27 additions and 18 deletions

View File

@ -13,7 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.cluster;
package com.alibaba.csp.sentinel.cluster.client;
import com.alibaba.csp.sentinel.cluster.TokenServerDescriptor;
import com.alibaba.csp.sentinel.cluster.TokenService;
/**
* Token client interface for distributed flow control.
@ -29,4 +32,18 @@ public interface ClusterTokenClient extends TokenService {
* @return current token server if connected, otherwise null
*/
TokenServerDescriptor currentServer();
/**
* Start the token client.
*
* @throws Exception some error occurs
*/
void start() throws Exception;
/**
* Stop the token client.
*
* @throws Exception some error occurs
*/
void stop() throws Exception;
}

View File

@ -13,13 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.cluster;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
package com.alibaba.csp.sentinel.cluster.client;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.SpiLoader;
/**
* Provider for a universal {@link ClusterTokenClient} instance.
@ -31,8 +28,6 @@ public final class TokenClientProvider {
private static ClusterTokenClient client = null;
private static final ServiceLoader<ClusterTokenClient> LOADER = ServiceLoader.load(ClusterTokenClient.class);
static {
// Not strictly thread-safe, but it's OK since it will be resolved only once.
resolveTokenClientInstance();
@ -43,17 +38,14 @@ public final class TokenClientProvider {
}
private static void resolveTokenClientInstance() {
List<ClusterTokenClient> clients = new ArrayList<ClusterTokenClient>();
for (ClusterTokenClient client : LOADER) {
clients.add(client);
}
if (!clients.isEmpty()) {
// Get first.
client = clients.get(0);
RecordLog.info("[TokenClientProvider] Token client resolved: " + client.getClass().getCanonicalName());
ClusterTokenClient resolvedClient = SpiLoader.loadFirstInstance(ClusterTokenClient.class);
if (resolvedClient == null) {
RecordLog.info(
"[TokenClientProvider] No existing cluster token client, cluster client mode will not be activated");
} else {
RecordLog.warn("[TokenClientProvider] No existing token client, resolve failed");
client = resolvedClient;
RecordLog.info(
"[TokenClientProvider] Cluster token client resolved: " + client.getClass().getCanonicalName());
}
}