forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish apache#3942 : Add Service registry and discovery
- Loading branch information
1 parent
5805cb7
commit 74f1f4d
Showing
13 changed files
with
1,242 additions
and
1 deletion.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...gistry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...o-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/Page.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.