Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Curator Service Discovery and Spring Cloud ZooKeeper (#2749)
Related: #2673 Motivation: It will be nice if we support Curator Service Discovery and Spring Cloud Zookeeper. Modifications: - Add `ZookeeperRegistrationSpec` and `ZookeeperDiscoverySpec` to specify whether use legacy format or Curator compatible format. - (Breaking) You should specify `ZookeeperRegistrationSpec` When creating `ZookeeperUpdatingListener`. - (Breaking) You should specify `ZookeeperDiscoverySpec` When creating `ZookeeperEndpointGroup`. - (Breaking) - `NodeValueCodec` is gone. - You now have to use `ZookeeperRegistrationSpec` and `ZookeeperDiscoverySpec` to encode and decode. - `ZooKeeperEndpointGroupBuilder.codec(...)` and `ZooKeeperUpatingListenerBuilder.codec(...)` are gone as well. Result: - You can now use Armeria client and server with Curator Service Discovery.
- Loading branch information
Showing
28 changed files
with
1,353 additions
and
485 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -1,26 +1,11 @@ | ||
final def DROPWIZARD_VERSION = '3.2.6' | ||
final def SNAPPY_VERSION = '1.1.7.5' | ||
|
||
dependencies { | ||
// Curator | ||
api 'org.apache.curator:curator-recipes' | ||
api 'org.apache.curator:curator-x-discovery' | ||
|
||
// ZooKeeper | ||
api 'org.apache.zookeeper:zookeeper' | ||
|
||
implementation("io.dropwizard.metrics:metrics-core") { | ||
version { | ||
// Will fail the build if the override doesn't work | ||
strictly DROPWIZARD_VERSION | ||
} | ||
} | ||
implementation("org.xerial.snappy:snappy-java") { | ||
version { | ||
// Will fail the build if the override doesn't work | ||
strictly SNAPPY_VERSION | ||
} | ||
} | ||
|
||
// ZooKeeper JUnit | ||
testImplementation 'org.dmonix.junit:zookeeper-junit' | ||
} |
55 changes: 55 additions & 0 deletions
55
zookeeper/src/main/java/com/linecorp/armeria/client/zookeeper/CuratorDiscoverySpec.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,55 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.armeria.client.zookeeper; | ||
|
||
import java.util.function.Function; | ||
|
||
import org.apache.curator.x.discovery.ServiceInstance; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
import com.linecorp.armeria.client.Endpoint; | ||
import com.linecorp.armeria.internal.common.zookeeper.CuratorXNodeValueCodec; | ||
|
||
final class CuratorDiscoverySpec implements ZookeeperDiscoverySpec { | ||
|
||
private final String path; | ||
private final Function<? super ServiceInstance<?>, Endpoint> converter; | ||
|
||
CuratorDiscoverySpec( | ||
String serviceName, Function<? super ServiceInstance<?>, Endpoint> converter) { | ||
path = '/' + serviceName; | ||
this.converter = converter; | ||
} | ||
|
||
@Override | ||
public String path() { | ||
return path; | ||
} | ||
|
||
@Override | ||
public Endpoint decode(byte[] data) { | ||
return converter.apply(CuratorXNodeValueCodec.INSTANCE.decode(data)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("path", path) | ||
.add("converter", converter) | ||
.toString(); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...eper/src/main/java/com/linecorp/armeria/client/zookeeper/CuratorDiscoverySpecBuilder.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,110 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.armeria.client.zookeeper; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static com.linecorp.armeria.internal.common.zookeeper.ZookeeperPathUtil.validatePath; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Function; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.apache.curator.x.discovery.ServiceInstance; | ||
|
||
import com.linecorp.armeria.client.Endpoint; | ||
|
||
/** | ||
* Builds a {@link ZookeeperDiscoverySpec} for | ||
* <a href="https://curator.apache.org/curator-x-discovery/index.html">Curator Service Discovery</a>. | ||
*/ | ||
public final class CuratorDiscoverySpecBuilder { | ||
|
||
private final String serviceName; | ||
@Nullable | ||
private String instanceId; | ||
@Nullable | ||
private Boolean useSsl; | ||
@Nullable | ||
private Function<? super ServiceInstance<?>, Endpoint> converter; | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
CuratorDiscoverySpecBuilder(String serviceName) { | ||
this.serviceName = validatePath(serviceName, "serviceName"); | ||
} | ||
|
||
/** | ||
* Sets the specified instance ID. If this is set, the {@link ZooKeeperEndpointGroup} will only connect to | ||
* the instance. | ||
*/ | ||
public CuratorDiscoverySpecBuilder instanceId(String instanceId) { | ||
checkState(converter == null, "converter() and instanceId() are mutually exclusive."); | ||
this.instanceId = requireNonNull(instanceId, "instanceId"); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets whether to connect an {@link Endpoint} using {@code sslPort} of {@link ServiceInstance}. | ||
*/ | ||
public CuratorDiscoverySpecBuilder useSsl(boolean useSsl) { | ||
checkState(converter == null, "converter() and useSsl() are mutually exclusive."); | ||
this.useSsl = useSsl; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the specified converter to convert a {@link ServiceInstance} into an {@link Endpoint}. | ||
* If you don't want to connect to the service, you can simply return {@code null} in the converter. | ||
*/ | ||
public CuratorDiscoverySpecBuilder converter( | ||
Function<? super ServiceInstance<?>, Endpoint> converter) { | ||
checkState(instanceId == null, "converter() and instanceId() are mutually exclusive."); | ||
checkState(useSsl == null, "converter() and useSsl() are mutually exclusive."); | ||
this.converter = requireNonNull(converter, "converter"); | ||
return this; | ||
} | ||
|
||
private Function<? super ServiceInstance<?>, Endpoint> converter() { | ||
if (converter != null) { | ||
return converter; | ||
} | ||
return instance -> { | ||
if (!instance.isEnabled()) { | ||
return null; | ||
} | ||
if (instanceId != null && !instanceId.equals(instance.getId())) { | ||
return null; | ||
} | ||
if (useSsl != null && useSsl && instance.getSslPort() != null) { | ||
return Endpoint.of(instance.getAddress(), instance.getSslPort()); | ||
} | ||
|
||
if (instance.getPort() != null) { | ||
return Endpoint.of(instance.getAddress(), instance.getPort()); | ||
} | ||
return Endpoint.of(instance.getAddress()); | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a newly-created {@link ZookeeperDiscoverySpec} based on the properties set so far. | ||
*/ | ||
public ZookeeperDiscoverySpec build() { | ||
return new CuratorDiscoverySpec(serviceName, converter()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...per/src/main/java/com/linecorp/armeria/client/zookeeper/LegacyZookeeperDiscoverySpec.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,36 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.armeria.client.zookeeper; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.linecorp.armeria.client.Endpoint; | ||
import com.linecorp.armeria.internal.common.zookeeper.LegacyNodeValueCodec; | ||
|
||
enum LegacyZookeeperDiscoverySpec implements ZookeeperDiscoverySpec { | ||
INSTANCE; | ||
|
||
@Nullable | ||
@Override | ||
public String path() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Endpoint decode(byte[] zNodeValue) { | ||
return LegacyNodeValueCodec.INSTANCE.decode(zNodeValue); | ||
} | ||
} |
Oops, something went wrong.