Search in sources :

Example 1 with ServiceKey

use of com.tencent.polaris.api.pojo.ServiceKey in project polaris-java by polarismesh.

the class CircuitBreakerTest method before.

@Before
public void before() {
    try {
        namingServer = NamingServer.startNamingServer(10081);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_CIRCUIT_BREAKER);
    InstanceParameter parameter = new InstanceParameter();
    parameter.setHealthy(true);
    parameter.setIsolated(false);
    parameter.setWeight(100);
    namingServer.getNamingService().batchAddInstances(serviceKey, 10010, MAX_COUNT, parameter);
}
Also used : InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) IOException(java.io.IOException) Before(org.junit.Before)

Example 2 with ServiceKey

use of com.tencent.polaris.api.pojo.ServiceKey in project polaris-java by polarismesh.

the class MessagePersistHandler method fileNameToServiceKey.

private static ServiceEventKey fileNameToServiceKey(String fileName) {
    fileName = fileName.substring(0, fileName.length() - CACHE_SUFFIX.length());
    String[] pieces = fileName.split("#");
    try {
        String namespace = URLDecoder.decode(pieces[1], "UTF-8");
        String service = URLDecoder.decode(pieces[2], "UTF-8");
        String eventTypeStr = URLDecoder.decode(pieces[3], "UTF-8");
        ServiceEventKey.EventType eventType = ServiceEventKey.EventType.valueOf(eventTypeStr.toUpperCase());
        return new ServiceEventKey(new ServiceKey(namespace, service), eventType);
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError("UTF-8 is unknown");
    // or 'throw new AssertionError("Impossible things are happening today. " +
    // "Consider buying a lottery ticket!!");'
    }
}
Also used : ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey)

Example 3 with ServiceKey

use of com.tencent.polaris.api.pojo.ServiceKey in project polaris-java by polarismesh.

the class LocalTest method before.

@Before
public void before() {
    try {
        namingServer = NamingServer.startNamingServer(10081);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    ServiceKey serviceKey = new ServiceKey(Consts.NAMESPACE_TEST, Consts.LOCAL_LIMIT_SERVICE);
    namingServer.getNamingService().addService(serviceKey);
    Builder rateLimitBuilder = RateLimit.newBuilder();
    Rule.Builder ruleBuilder1 = Rule.newBuilder();
    ruleBuilder1.setType(Type.LOCAL);
    ruleBuilder1.setPriority(UInt32Value.newBuilder().setValue(1).build());
    ruleBuilder1.setAction(StringValue.newBuilder().setValue("reject").build());
    ruleBuilder1.setAmountMode(AmountMode.GLOBAL_TOTAL);
    ruleBuilder1.addAmounts(Amount.newBuilder().setMaxAmount(UInt32Value.newBuilder().setValue(9998).build()).setValidDuration(Duration.newBuilder().setSeconds(1).build()));
    ruleBuilder1.setRevision(StringValue.newBuilder().setValue("11111").build());
    rateLimitBuilder.addRules(ruleBuilder1.build());
    Rule.Builder ruleBuilder2 = Rule.newBuilder();
    ruleBuilder2.setType(Type.LOCAL);
    ruleBuilder2.setPriority(UInt32Value.newBuilder().setValue(0).build());
    ruleBuilder2.setAction(StringValue.newBuilder().setValue("reject").build());
    ruleBuilder2.setAmountMode(AmountMode.GLOBAL_TOTAL);
    ruleBuilder2.putLabels(Consts.LABEL_METHOD, MatchString.newBuilder().setType(MatchStringType.EXACT).setValue(StringValue.newBuilder().setValue(Consts.METHOD_CASH).build()).build());
    ruleBuilder2.addAmounts(Amount.newBuilder().setMaxAmount(UInt32Value.newBuilder().setValue(1998).build()).setValidDuration(Duration.newBuilder().setSeconds(1).build()));
    ruleBuilder2.setRevision(StringValue.newBuilder().setValue("22222").build());
    rateLimitBuilder.addRules(ruleBuilder2.build());
    Rule.Builder ruleBuilder3 = Rule.newBuilder();
    ruleBuilder3.setType(Type.LOCAL);
    ruleBuilder3.setPriority(UInt32Value.newBuilder().setValue(0).build());
    ruleBuilder3.setAction(StringValue.newBuilder().setValue("reject").build());
    ruleBuilder3.setAmountMode(AmountMode.GLOBAL_TOTAL);
    ruleBuilder3.putLabels(Consts.LABEL_METHOD, MatchString.newBuilder().setType(MatchStringType.EXACT).setValue(StringValue.newBuilder().setValue(Consts.METHOD_PAY).build()).build());
    ruleBuilder3.addAmounts(Amount.newBuilder().setMaxAmount(UInt32Value.newBuilder().setValue(998).build()).setValidDuration(Duration.newBuilder().setSeconds(1).build()));
    ruleBuilder3.setRevision(StringValue.newBuilder().setValue("33333").build());
    rateLimitBuilder.addRules(ruleBuilder3.build());
    rateLimitBuilder.setRevision(StringValue.newBuilder().setValue("xxxxxxx").build());
    namingServer.getNamingService().setRateLimit(serviceKey, rateLimitBuilder.build());
}
Also used : Builder(com.tencent.polaris.client.pb.RateLimitProto.RateLimit.Builder) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) IOException(java.io.IOException) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) Before(org.junit.Before)

Example 4 with ServiceKey

use of com.tencent.polaris.api.pojo.ServiceKey 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 5 with ServiceKey

use of com.tencent.polaris.api.pojo.ServiceKey in project polaris-java by polarismesh.

the class NamingService method registerInstance.

@Override
public void registerInstance(ServiceProto.Instance request, StreamObserver<ResponseProto.Response> responseObserver) {
    ServiceKey serviceKey = new ServiceKey(request.getNamespace().getValue(), request.getService().getValue());
    if (!services.containsKey(serviceKey)) {
        responseObserver.onNext(buildResponse(ServerCodes.NOT_FOUND_RESOURCE, String.format("service %s not found", serviceKey), request));
        responseObserver.onCompleted();
        return;
    }
    List<ServiceProto.Instance> instances = services.get(serviceKey);
    if (CollectionUtils.isNotEmpty(instances)) {
        for (ServiceProto.Instance instance : instances) {
            if (instance.getHost().getValue().equals(request.getHost().getValue()) && instance.getPort().getValue() == request.getPort().getValue()) {
                responseObserver.onNext(buildResponse(ServerCodes.EXISTED_RESOURCE, String.format("instance %s:%d exists", request.getHost().getValue(), request.getPort().getValue()), instance));
                responseObserver.onCompleted();
                return;
            }
        }
    }
    ServiceProto.Instance.Builder builder = ServiceProto.Instance.newBuilder();
    builder.mergeFrom(request);
    String instId = UUID.randomUUID().toString();
    builder.setId(StringValue.newBuilder().setValue(instId).build());
    ServiceProto.Instance nextInstance = builder.build();
    instances.add(nextInstance);
    ResponseProto.Response.Builder response = ResponseProto.Response.newBuilder();
    response.setCode(UInt32Value.newBuilder().setValue(ServerCodes.EXECUTE_SUCCESS).build());
    response.setInstance(nextInstance);
    responseObserver.onNext(response.build());
    responseObserver.onCompleted();
}
Also used : Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Aggregations

ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)52 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)11 Test (org.junit.Test)8 PolarisException (com.tencent.polaris.api.exception.PolarisException)7 InstanceParameter (com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter)7 RetriableException (com.tencent.polaris.api.exception.RetriableException)6 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)6 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)6 IOException (java.io.IOException)6 Server (com.netflix.loadbalancer.Server)5 Instance (com.tencent.polaris.api.pojo.Instance)5 Before (org.junit.Before)5 BeforeClass (org.junit.BeforeClass)5 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)4 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)4 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)4 Node (com.tencent.polaris.client.pojo.Node)4 HashSet (java.util.HashSet)4 Configuration (com.tencent.polaris.api.config.Configuration)3