Search in sources :

Example 1 with CircuitBreaker

use of com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker in project polaris-java by polarismesh.

the class NamingService method buildServiceResponse.

private ResponseProto.DiscoverResponse buildServiceResponse(int code, String info, DiscoverRequest req) {
    ResponseProto.DiscoverResponse.Builder builder = ResponseProto.DiscoverResponse.newBuilder();
    builder.setCode(UInt32Value.newBuilder().setValue(code).build());
    CircuitBreakerProto.CircuitBreaker circuitBreaker;
    RateLimitProto.RateLimit rateLimit;
    RoutingProto.Routing routing;
    List<ServiceProto.Instance> instances;
    ServiceProto.Service service = req.getService();
    ServiceKey serviceKey = new ServiceKey(service.getNamespace().getValue(), service.getName().getValue());
    switch(req.getType()) {
        case UNKNOWN:
            break;
        case INSTANCE:
            instances = services.get(serviceKey);
            if (CollectionUtils.isNotEmpty(instances)) {
                builder.addAllInstances(instances);
            }
            builder.setType(DiscoverResponseType.INSTANCE);
            break;
        case CLUSTER:
            break;
        case ROUTING:
            routing = serviceRoutings.get(serviceKey);
            if (null != routing) {
                builder.setRouting(routing);
            }
            builder.setType(DiscoverResponseType.ROUTING);
            break;
        case CIRCUIT_BREAKER:
            circuitBreaker = serviceCircuitBreakers.get(serviceKey);
            if (null != circuitBreaker) {
                builder.setCircuitBreaker(circuitBreaker);
            }
            builder.setType(DiscoverResponseType.CIRCUIT_BREAKER);
            break;
        case RATE_LIMIT:
            rateLimit = serviceRateLimits.get(serviceKey);
            if (null != rateLimit) {
                builder.setRateLimit(rateLimit);
            }
            builder.setType(DiscoverResponseType.RATE_LIMIT);
            break;
        case SERVICES:
            Set<ServiceKey> keys = services.keySet();
            Map<String, ServiceProto.Service> tmp = new HashMap<>();
            String namespace = req.getService().getNamespace().getValue();
            System.out.println("get service param : " + namespace);
            keys.removeIf(serviceKey1 -> {
                if (StringUtils.isBlank(namespace)) {
                    return false;
                }
                return !Objects.equals(namespace, serviceKey1.getNamespace());
            });
            keys.forEach(key -> {
                tmp.put(key.getNamespace() + "##" + key.getService(), ServiceProto.Service.newBuilder().setNamespace(StringValue.newBuilder().setValue(key.getNamespace()).build()).setName(StringValue.newBuilder().setValue(key.getService()).build()).build());
            });
            final int[] index = { 0 };
            tmp.forEach((s, svc) -> {
                builder.addServices(index[0], svc);
                index[0]++;
            });
            builder.setType(DiscoverResponseType.SERVICES);
            break;
        case UNRECOGNIZED:
            break;
        default:
            break;
    }
    if (StringUtils.isNotBlank(info)) {
        builder.setInfo(StringValue.newBuilder().setValue(info).build());
    }
    builder.setService(service);
    return builder.build();
}
Also used : Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) RateLimitProto(com.tencent.polaris.client.pb.RateLimitProto) RoutingProto(com.tencent.polaris.client.pb.RoutingProto) RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) CircuitBreaker(com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker) CircuitBreakerProto(com.tencent.polaris.client.pb.CircuitBreakerProto) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Example 2 with CircuitBreaker

use of com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker in project polaris-java by polarismesh.

the class CircuitBreakCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    CircuitBreaker circuitBreaker = discoverResponse.getCircuitBreaker();
    String revision = "";
    if (null != circuitBreaker) {
        revision = circuitBreaker.getRevision().getValue();
    }
    return new ServiceRuleByProto(circuitBreaker, revision, isCacheLoaded, getTargetEventType());
}
Also used : CircuitBreaker(com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) ServiceRuleByProto(com.tencent.polaris.client.pojo.ServiceRuleByProto)

Aggregations

CircuitBreaker (com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 CircuitBreakerProto (com.tencent.polaris.client.pb.CircuitBreakerProto)1 RateLimitProto (com.tencent.polaris.client.pb.RateLimitProto)1 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)1 DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)1 RoutingProto (com.tencent.polaris.client.pb.RoutingProto)1 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)1 Instance (com.tencent.polaris.client.pb.ServiceProto.Instance)1 ServiceRuleByProto (com.tencent.polaris.client.pojo.ServiceRuleByProto)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1