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:
parent
b973aca1f5
commit
9f2678eb6c
|
|
@ -13,7 +13,10 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.
|
* Token client interface for distributed flow control.
|
||||||
|
|
@ -29,4 +32,18 @@ public interface ClusterTokenClient extends TokenService {
|
||||||
* @return current token server if connected, otherwise null
|
* @return current token server if connected, otherwise null
|
||||||
*/
|
*/
|
||||||
TokenServerDescriptor currentServer();
|
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;
|
||||||
}
|
}
|
||||||
|
|
@ -13,13 +13,10 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.cluster;
|
package com.alibaba.csp.sentinel.cluster.client;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.ServiceLoader;
|
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
|
import com.alibaba.csp.sentinel.util.SpiLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provider for a universal {@link ClusterTokenClient} instance.
|
* Provider for a universal {@link ClusterTokenClient} instance.
|
||||||
|
|
@ -31,8 +28,6 @@ public final class TokenClientProvider {
|
||||||
|
|
||||||
private static ClusterTokenClient client = null;
|
private static ClusterTokenClient client = null;
|
||||||
|
|
||||||
private static final ServiceLoader<ClusterTokenClient> LOADER = ServiceLoader.load(ClusterTokenClient.class);
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Not strictly thread-safe, but it's OK since it will be resolved only once.
|
// Not strictly thread-safe, but it's OK since it will be resolved only once.
|
||||||
resolveTokenClientInstance();
|
resolveTokenClientInstance();
|
||||||
|
|
@ -43,17 +38,14 @@ public final class TokenClientProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void resolveTokenClientInstance() {
|
private static void resolveTokenClientInstance() {
|
||||||
List<ClusterTokenClient> clients = new ArrayList<ClusterTokenClient>();
|
ClusterTokenClient resolvedClient = SpiLoader.loadFirstInstance(ClusterTokenClient.class);
|
||||||
for (ClusterTokenClient client : LOADER) {
|
if (resolvedClient == null) {
|
||||||
clients.add(client);
|
RecordLog.info(
|
||||||
}
|
"[TokenClientProvider] No existing cluster token client, cluster client mode will not be activated");
|
||||||
|
|
||||||
if (!clients.isEmpty()) {
|
|
||||||
// Get first.
|
|
||||||
client = clients.get(0);
|
|
||||||
RecordLog.info("[TokenClientProvider] Token client resolved: " + client.getClass().getCanonicalName());
|
|
||||||
} else {
|
} else {
|
||||||
RecordLog.warn("[TokenClientProvider] No existing token client, resolve failed");
|
client = resolvedClient;
|
||||||
|
RecordLog.info(
|
||||||
|
"[TokenClientProvider] Cluster token client resolved: " + client.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue