瀏覽代碼

Add common module for Sentinel default cluster implementation

- Add a `ClusterTransportClient` for transport abstraction of Sentinel cluster
- Add universal request/response interface and common ClusterRequest/ClusterResponse abstraction
- Add common request/response data entity
- Add basic abstraction of codec (EntityWriter and EntityDecoder)

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 6 年之前
父節點
當前提交
b3ae7f58bf
共有 18 個文件被更改,包括 805 次插入0 次删除
  1. +44
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterConstants.java
  2. +32
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterErrorMessages.java
  3. +37
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterTransportClient.java
  4. +33
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityDecoder.java
  5. +35
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityWriter.java
  6. +27
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityDecoder.java
  7. +29
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityWriter.java
  8. +27
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityDecoder.java
  9. +29
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityWriter.java
  10. +32
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/exception/SentinelClusterException.java
  11. +79
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/ClusterRequest.java
  12. +39
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/Request.java
  13. +63
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/FlowRequestData.java
  14. +65
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/ParamFlowRequestData.java
  15. +87
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/ClusterResponse.java
  16. +46
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/Response.java
  17. +52
    -0
      sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/data/FlowTokenResponseData.java
  18. +49
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java

+ 44
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterConstants.java 查看文件

@@ -0,0 +1,44 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public final class ClusterConstants {

public static final int MSG_TYPE_PING = 0;
public static final int MSG_TYPE_FLOW = 1;
public static final int MSG_TYPE_PARAM_FLOW = 2;

public static final int RESPONSE_STATUS_BAD = -1;
public static final int RESPONSE_STATUS_OK = 0;

public static final int PARAM_TYPE_INTEGER = 0;
public static final int PARAM_TYPE_LONG = 1;
public static final int PARAM_TYPE_BYTE = 2;
public static final int PARAM_TYPE_DOUBLE = 3;
public static final int PARAM_TYPE_FLOAT = 4;
public static final int PARAM_TYPE_SHORT = 5;
public static final int PARAM_TYPE_BOOLEAN = 6;
public static final int PARAM_TYPE_STRING = 7;

public static final int DEFAULT_CLUSTER_SERVER_PORT = 8730;
public static final int DEFAULT_REQUEST_TIMEOUT = 20;

private ClusterConstants() {}
}

+ 32
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterErrorMessages.java 查看文件

@@ -0,0 +1,32 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster;

/**
* @author jialiang.ljl
* @since 1.4.0
*/
public final class ClusterErrorMessages {

public static final String BAD_REQUEST = "bad request";
public static final String UNEXPECTED_STATUS = "unexpected status";
public static final String TOO_MANY_REQUESTS = "too many requests (client side)";
public static final String REQUEST_TIME_OUT = "request time out";
public static final String CLIENT_NOT_READY = "client not ready (not running or initializing)";
public static final String NO_RULES_IN_SERVER = "no rules in token server";

private ClusterErrorMessages() {}
}

+ 37
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterTransportClient.java 查看文件

@@ -0,0 +1,37 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster;

import com.alibaba.csp.sentinel.cluster.request.ClusterRequest;
import com.alibaba.csp.sentinel.cluster.response.ClusterResponse;

/**
* Synchronous transport client for distributed flow control.
*
* @author Eric Zhao
* @since 1.4.0
*/
public interface ClusterTransportClient {

/**
* Send request to remote server and get response.
*
* @param request Sentinel cluster request
* @return response from remote server
* @throws Exception some error occurs
*/
ClusterResponse sendRequest(ClusterRequest request) throws Exception;
}

+ 33
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityDecoder.java 查看文件

@@ -0,0 +1,33 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.codec;

/**
* @param <S> source stream type
* @param <T> target entity type
* @author Eric Zhao
* @since 1.4.0
*/
public interface EntityDecoder<S, T> {

/**
* Decode target object from source stream.
*
* @param source source stream
* @return decoded target object
*/
T decode(S source);
}

+ 35
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityWriter.java 查看文件

@@ -0,0 +1,35 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.codec;

/**
* A universal interface for publishing entities to a target stream.
*
* @param <E> entity type
* @param <T> target stream type
* @author Eric Zhao
* @since 1.4.0
*/
public interface EntityWriter<E, T> {

/**
* Write the provided entity to target stream.
*
* @param entity entity to publish
* @param target the target stream
*/
void writeTo(E entity, T target);
}

+ 27
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityDecoder.java 查看文件

@@ -0,0 +1,27 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.codec.request;

import com.alibaba.csp.sentinel.cluster.codec.EntityDecoder;
import com.alibaba.csp.sentinel.cluster.request.Request;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public interface RequestEntityDecoder<S, T extends Request> extends EntityDecoder<S, T> {

}

+ 29
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityWriter.java 查看文件

@@ -0,0 +1,29 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.codec.request;

import com.alibaba.csp.sentinel.cluster.codec.EntityWriter;
import com.alibaba.csp.sentinel.cluster.request.Request;

/**
* A universal {@link EntityWriter} interface for publishing {@link Request} to a target stream.
*
* @author Eric Zhao
* @since 1.4.0
*/
public interface RequestEntityWriter<E extends Request, T> extends EntityWriter<E, T> {

}

+ 27
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityDecoder.java 查看文件

@@ -0,0 +1,27 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.codec.response;

import com.alibaba.csp.sentinel.cluster.codec.EntityDecoder;
import com.alibaba.csp.sentinel.cluster.response.Response;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public interface ResponseEntityDecoder<S, T extends Response> extends EntityDecoder<S, T> {

}

+ 29
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityWriter.java 查看文件

@@ -0,0 +1,29 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.codec.response;

import com.alibaba.csp.sentinel.cluster.codec.EntityWriter;
import com.alibaba.csp.sentinel.cluster.response.Response;

/**
* A universal {@link EntityWriter} interface for publishing {@link Response} to a target stream.
*
* @author Eric Zhao
* @since 1.4.0
*/
public interface ResponseEntityWriter<E extends Response, T> extends EntityWriter<E, T> {

}

+ 32
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/exception/SentinelClusterException.java 查看文件

@@ -0,0 +1,32 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.exception;

/**
* @author jialiang.ljl
* @since 1.4.0
*/
public class SentinelClusterException extends Exception {

public SentinelClusterException(String errorMsg) {
super(errorMsg);
}

@Override
public Throwable fillInStackTrace() {
return this;
}
}

+ 79
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/ClusterRequest.java 查看文件

@@ -0,0 +1,79 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.request;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public class ClusterRequest<T> implements Request {

private int id;
private int type;

private T data;

public ClusterRequest() {}

public ClusterRequest(int id, int type, T data) {
this.id = id;
this.type = type;
this.data = data;
}

public ClusterRequest(int type, T data) {
this.type = type;
this.data = data;
}

@Override
public int getId() {
return id;
}

public ClusterRequest<T> setId(int id) {
this.id = id;
return this;
}

@Override
public int getType() {
return type;
}

public ClusterRequest<T> setType(int type) {
this.type = type;
return this;
}

public T getData() {
return data;
}

public ClusterRequest<T> setData(T data) {
this.data = data;
return this;
}

@Override
public String toString() {
return "ClusterRequest{" +
"id=" + id +
", type=" + type +
", data=" + data +
'}';
}
}

+ 39
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/Request.java 查看文件

@@ -0,0 +1,39 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.request;

/**
* Cluster transport request interface.
*
* @author Eric Zhao
* @since 1.4.0
*/
public interface Request {

/**
* Get request type.
*
* @return request type
*/
int getType();

/**
* Get request ID.
*
* @return unique request ID
*/
int getId();
}

+ 63
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/FlowRequestData.java 查看文件

@@ -0,0 +1,63 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.request.data;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public class FlowRequestData {

private long flowId;
private int count;
private boolean priority;

public long getFlowId() {
return flowId;
}

public FlowRequestData setFlowId(long flowId) {
this.flowId = flowId;
return this;
}

public int getCount() {
return count;
}

public FlowRequestData setCount(int count) {
this.count = count;
return this;
}

public boolean isPriority() {
return priority;
}

public FlowRequestData setPriority(boolean priority) {
this.priority = priority;
return this;
}

@Override
public String toString() {
return "FlowRequestData{" +
"flowId=" + flowId +
", count=" + count +
", priority=" + priority +
'}';
}
}

+ 65
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/ParamFlowRequestData.java 查看文件

@@ -0,0 +1,65 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.request.data;

import java.util.Collection;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public class ParamFlowRequestData {

private long flowId;
private int count;
private Collection<Object> params;

public long getFlowId() {
return flowId;
}

public ParamFlowRequestData setFlowId(long flowId) {
this.flowId = flowId;
return this;
}

public int getCount() {
return count;
}

public ParamFlowRequestData setCount(int count) {
this.count = count;
return this;
}

public Collection<Object> getParams() {
return params;
}

public ParamFlowRequestData setParams(Collection<Object> params) {
this.params = params;
return this;
}

@Override
public String toString() {
return "ParamFlowRequestData{" +
"flowId=" + flowId +
", count=" + count +
", params=" + params +
'}';
}
}

+ 87
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/ClusterResponse.java 查看文件

@@ -0,0 +1,87 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.response;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public class ClusterResponse<T> implements Response {

private int id;
private int type;
private int status;

private T data;

public ClusterResponse() {}

public ClusterResponse(int id, int type, int status, T data) {
this.id = id;
this.type = type;
this.status = status;
this.data = data;
}

@Override
public int getId() {
return id;
}

public ClusterResponse<T> setId(int id) {
this.id = id;
return this;
}

@Override
public int getType() {
return type;
}

public ClusterResponse<T> setType(int type) {
this.type = type;
return this;
}

@Override
public int getStatus() {
return status;
}

public ClusterResponse<T> setStatus(int status) {
this.status = status;
return this;
}

public T getData() {
return data;
}

public ClusterResponse<T> setData(T data) {
this.data = data;
return this;
}

@Override
public String toString() {
return "ClusterResponse{" +
"id=" + id +
", type=" + type +
", status=" + status +
", data=" + data +
'}';
}
}

+ 46
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/Response.java 查看文件

@@ -0,0 +1,46 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.response;

/**
* Cluster transport response interface.
*
* @author Eric Zhao
* @since 1.4.0
*/
public interface Response {

/**
* Get response ID.
*
* @return response ID
*/
int getId();

/**
* Get response type.
*
* @return response type
*/
int getType();

/**
* Get response status.
*
* @return response status
*/
int getStatus();
}

+ 52
- 0
sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/data/FlowTokenResponseData.java 查看文件

@@ -0,0 +1,52 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.cluster.response.data;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public class FlowTokenResponseData {

private int remainingCount;
private int waitInMs;

public int getRemainingCount() {
return remainingCount;
}

public FlowTokenResponseData setRemainingCount(int remainingCount) {
this.remainingCount = remainingCount;
return this;
}

public int getWaitInMs() {
return waitInMs;
}

public FlowTokenResponseData setWaitInMs(int waitInMs) {
this.waitInMs = waitInMs;
return this;
}

@Override
public String toString() {
return "FlowTokenResponseData{" +
"remainingCount=" + remainingCount +
", waitInMs=" + waitInMs +
'}';
}
}

+ 49
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java 查看文件

@@ -0,0 +1,49 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.util;

import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Eric Zhao
* @since 1.4.0
*/
public final class SpiLoader {

private static final Map<String, ServiceLoader> SERVICE_LOADER_MAP = new ConcurrentHashMap<String, ServiceLoader>();

public static <T> T loadFirstInstance(Class<T> clazz) {
String key = clazz.getName();
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoader.load(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}

Iterator<T> iterator = serviceLoader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
}

private SpiLoader() {}
}

Loading…
取消
儲存