Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public interface ServiceCombConstants {
String SERVICECOMB_ENV = "SERVICECOMB_ENV";

String DEFAULT_SERVICECOMB_ENV = "";

String DEVELOPMENT_SERVICECOMB_ENV = "development";

String PRODUCTION_SERVICECOMB_ENV = "production";
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.netflix.config.DynamicPropertyFactory;

public final class RegistryUtils {
Expand Down Expand Up @@ -221,4 +223,8 @@ public static MicroserviceInstances findServiceInstances(String appId, String se
String versionRule, String revision) {
return serviceRegistry.findServiceInstances(appId, serviceName, versionRule, revision);
}

public static String calcSchemaSummary(String schemaContent) {
return Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class MicroserviceInstance {

private HealthCheck healthCheck;

/**
* Will be abandoned, use {@link Microservice#environment} instead
*/
@Deprecated
private String environment;

private String stage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.ws.rs.core.Response.Status;

import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
Expand All @@ -40,6 +42,7 @@
import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
import org.apache.servicecomb.serviceregistry.client.http.Holder;
import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
import org.apache.servicecomb.serviceregistry.version.Version;
import org.apache.servicecomb.serviceregistry.version.VersionRule;
Expand Down Expand Up @@ -342,7 +345,7 @@ public String getSchema(String microserviceId, String schemaId) {
}

@Override
public List<GetSchemaResponse> getSchemas(String microserviceId) {
public Holder<List<GetSchemaResponse>> getSchemas(String microserviceId) {
Microservice microservice = microserviceIdMap.get(microserviceId);
if (microservice == null) {
throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
Expand All @@ -355,7 +358,9 @@ public List<GetSchemaResponse> getSchemas(String microserviceId) {
schema.setSummary(Hashing.sha256().newHasher().putString(val, Charsets.UTF_8).hash().toString());
schemas.add(schema);
});
return schemas;
Holder<List<GetSchemaResponse>> resultHolder = new Holder<>();
resultHolder.setStatusCode(Status.OK.getStatusCode()).setValue(schemas);
return resultHolder;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
import org.apache.servicecomb.serviceregistry.client.http.Holder;
import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;

public interface ServiceRegistryClient {
Expand Down Expand Up @@ -82,7 +83,7 @@ public interface ServiceRegistryClient {
*
* 批量获取schemas内容
*/
List<GetSchemaResponse> getSchemas(String microserviceId);
Holder<List<GetSchemaResponse>> getSchemas(String microserviceId);

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.servicecomb.serviceregistry.client.http;

/**
* To carry the rest response information.
* @param <T> Type of response body
*/
public class Holder<T> {
T value;

int statusCode;

Throwable throwable;

public T getValue() {
return value;
}

public Holder<T> setValue(T value) {
this.value = value;
return this;
}

public int getStatusCode() {
return statusCode;
}

public Holder<T> setStatusCode(int statusCode) {
this.statusCode = statusCode;
return this;
}

public Throwable getThrowable() {
return throwable;
}

public Holder<T> setThrowable(Throwable throwable) {
this.throwable = throwable;
return this;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Holder{");
sb.append("value=").append(value);
sb.append(", statusCode=").append(statusCode);
sb.append(", throwable=").append(throwable);
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import java.util.concurrent.CountDownLatch;

import javax.ws.rs.core.Response.Status;
import javax.xml.ws.Holder;

import org.apache.servicecomb.foundation.common.net.IpPort;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.serviceregistry.api.Const;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
Expand Down Expand Up @@ -60,8 +60,6 @@
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;

import io.netty.handler.codec.http.HttpStatusClass;
import io.vertx.core.Handler;
Expand Down Expand Up @@ -108,13 +106,19 @@ protected <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, C
}
return;
}
holder.setStatusCode(response.statusCode());
response.bodyHandler(
bodyBuffer -> {
if (cls.getName().equals(HttpClientResponse.class.getName())) {
holder.value = (T) response;
countDownLatch.countDown();
return;
}
if (cls.equals(String.class)) {
holder.setValue((T) bodyBuffer.toString());
countDownLatch.countDown();
return;
}

// no need to support 304 in this place
if (!HttpStatusClass.SUCCESS.equals(HttpStatusClass.valueOf(response.statusCode()))) {
Expand All @@ -131,6 +135,7 @@ protected <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, C
holder.value =
JsonUtils.readValue(bodyBuffer.getBytes(), cls);
} catch (Exception e) {
holder.setStatusCode(0).setThrowable(e);
LOGGER.warn("read value failed and response message is {}",
bodyBuffer.toString());
}
Expand Down Expand Up @@ -291,7 +296,7 @@ public boolean registerSchema(String microserviceId, String schemaId, String sch
try {
CreateSchemaRequest request = new CreateSchemaRequest();
request.setSchema(schemaContent);
request.setSummary(Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString());
request.setSummary(RegistryUtils.calcSchemaSummary(schemaContent));
byte[] body = JsonUtils.writeValueAsBytes(request);

CountDownLatch countDownLatch = new CountDownLatch(1);
Expand Down Expand Up @@ -355,9 +360,10 @@ public String getSchema(String microserviceId, String schemaId) {
}

@Override
public List<GetSchemaResponse> getSchemas(String microserviceId) {
public Holder<List<GetSchemaResponse>> getSchemas(String microserviceId) {
Holder<GetSchemasResponse> holder = new Holder<>();
IpPort ipPort = ipPortManager.getAvailableAddress();
Holder<List<GetSchemaResponse>> resultHolder = new Holder<>();

CountDownLatch countDownLatch = new CountDownLatch(1);
// default not return schema content, just return summary!
Expand All @@ -372,11 +378,15 @@ public List<GetSchemaResponse> getSchemas(String microserviceId) {
microserviceId,
e);
}
resultHolder.setStatusCode(holder.getStatusCode()).setThrowable(holder.getThrowable());
if (holder.value != null) {
return holder.value.getSchema() != null ? holder.value.getSchema() : holder.value.getSchemas();
return resultHolder.setValue(
holder.value.getSchema() != null ?
holder.value.getSchema() :
holder.value.getSchemas());
}

return null;
return resultHolder;
}

@Override
Expand Down
Loading