Skip to content

Commit

Permalink
Polish apache#3942 : Add Service registry and discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 7, 2019
1 parent 5805cb7 commit 74f1f4d
Show file tree
Hide file tree
Showing 13 changed files with 1,242 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.dubbo.registry.client;

import java.io.Serializable;
import java.util.List;

/**
* The default implementation of {@link Page}
*
* @since 2.7.2
*/
public class DefaultPage<T> implements Page<T>, Serializable {

private static final long serialVersionUID = 1099331838954070419L;

private final int requestOffset;

private final int requestSize;

private int totalSize;

private List<T> data;

public DefaultPage(int requestOffset, int requestSize) {
this.requestOffset = requestOffset;
this.requestSize = requestSize;
}

@Override
public int getRequestOffset() {
return requestOffset;
}

@Override
public int getRequestSize() {
return requestSize;
}

@Override
public int getTotalSize() {
return totalSize;
}

public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}

@Override
public List<T> getData() {
return data;
}

public void setData(List<T> data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.dubbo.registry.client;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* The default implementation of {@link ServiceInstance}.
*
* @since 2.7.2
*/
public class DefaultServiceInstance implements ServiceInstance {

private final String serviceName;

private final String host;

private final int port;

private boolean enabled = true;

private boolean healthy = true;

private Map<String, String> metadata = new HashMap<>();

public DefaultServiceInstance(String serviceName, String host, int port) {
this.serviceName = serviceName;
this.host = host;
this.port = port;
}

@Override
public String getServiceName() {
return serviceName;
}

@Override
public String getHost() {
return host;
}

@Override
public int getPort() {
return port;
}

@Override
public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
public boolean isHealthy() {
return healthy;
}

public void setHealthy(boolean healthy) {
this.healthy = healthy;
}

@Override
public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DefaultServiceInstance)) return false;
DefaultServiceInstance that = (DefaultServiceInstance) o;
return getPort() == that.getPort() &&
Objects.equals(getServiceName(), that.getServiceName()) &&
Objects.equals(getHost(), that.getHost()) &&
Objects.equals(getMetadata(), that.getMetadata());
}

@Override
public int hashCode() {
return Objects.hash(getServiceName(), getHost(), getPort(), getMetadata());
}

@Override
public String toString() {
return "DefaultServiceInstance{" +
"serviceName='" + serviceName + '\'' +
", host='" + host + '\'' +
", port=" + port +
", enabled=" + enabled +
", healthy=" + healthy +
", metadata=" + metadata +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.dubbo.registry.client;

import java.util.List;

/**
* The model class of pagination
*
* @since 2.7.2
*/
public interface Page<T> {

/**
* Gets the offset of request
*
* @return positive integer
*/
int getRequestOffset();

/**
* Gets the size of request for query
*
* @return positive integer
*/
int getRequestSize();

/**
* Returns the total amount of elements.
*
* @return the total amount of elements
*/
int getTotalSize();

/**
* The data of current page
*
* @return non-null {@link List}
*/
List<T> getData();

/**
* The size of {@link #getData() data}
*
* @return positive integer
*/
default int getDataSize() {
return getData().size();
}

/**
* Returns whether the page has data at all.
*
* @return
*/
default boolean hasData() {
return getDataSize() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
/**
* The model class of an instance of a service, which is used for service registration and discovery.
* <p>
* The inspiration comes from <a href="https://spring.io/projects/spring-cloud-commons">Spring Cloud Commons</a>.
*
* @since 2.7.2
*/
Expand Down Expand Up @@ -75,4 +74,14 @@ default boolean isHealthy() {
*/
Map<String, String> getMetadata();

/**
* @return the hash code of current instance.
*/
int hashCode();

/**
* @param another another {@link ServiceInstance}
* @return if equals , return <code>true</code>, or <code>false</code>
*/
boolean equals(Object another);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.dubbo.registry.client;

/**
* The common interface to register and deregister for a service registry
*
* @since 2.7.2
*/
public interface ServiceRegistry {

/**
* Registers an instance of {@link ServiceInstance}.
*
* @param serviceInstance an instance of {@link ServiceInstance} to be registered
* @return If success, return <code>true</code>, or <code>false</code>
*/
boolean register(ServiceInstance serviceInstance);

/**
* Deregisters an instance of {@link ServiceInstance}.
*
* @param serviceInstance an instance of {@link ServiceInstance} to be deregistered
* @return If success, return <code>true</code>, or <code>false</code>
*/
void deregister(ServiceInstance serviceInstance);

/**
* Closes the ServiceRegistry. This is a lifecycle method.
*/
void close();
}
Loading

0 comments on commit 74f1f4d

Please sign in to comment.