forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Registry center for Nacos (apache#3150)
* check codes by checkstyle
- Loading branch information
1 parent
0656fea
commit 51dddbf
Showing
5 changed files
with
264 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
sharding-orchestration/sharding-orchestration-reg/sharding-orchestration-reg-nacos/pom.xml
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>sharding-orchestration-reg</artifactId> | ||
<version>4.0.0-RC3-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>sharding-orchestration-reg-nacos</artifactId> | ||
<name>${project.artifactId}</name> | ||
|
||
<properties> | ||
<nacos-client.verison>1.0.0</nacos-client.verison> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>sharding-orchestration-reg-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-client</artifactId> | ||
<version>${nacos-client.verison}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
149 changes: 149 additions & 0 deletions
149
.../src/main/java/org/apache/shardingsphere/orchestration/reg/nacos/NacosRegistryCenter.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,149 @@ | ||
/* | ||
* 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.shardingsphere.orchestration.reg.nacos; | ||
|
||
import com.alibaba.nacos.api.NacosFactory; | ||
import com.alibaba.nacos.api.config.ConfigService; | ||
import com.alibaba.nacos.api.config.listener.Listener; | ||
import com.alibaba.nacos.api.exception.NacosException; | ||
import com.google.common.base.Strings; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenter; | ||
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration; | ||
import org.apache.shardingsphere.orchestration.reg.listener.DataChangedEvent; | ||
import org.apache.shardingsphere.orchestration.reg.listener.DataChangedEventListener; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.concurrent.Executor; | ||
|
||
@Slf4j | ||
public class NacosRegistryCenter implements RegistryCenter { | ||
|
||
private ConfigService configService; | ||
|
||
@Getter | ||
@Setter | ||
private Properties properties = new Properties(); | ||
|
||
@Override | ||
public void init(final RegistryCenterConfiguration config) { | ||
try { | ||
configService = NacosFactory.createConfigService(config.getServerLists()); | ||
} catch (NacosException e) { | ||
log.debug("exception for: {}", e.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public String get(final String key) { | ||
return getDirectly(key); | ||
} | ||
|
||
@Override | ||
public String getDirectly(final String key) { | ||
try { | ||
String dataId = key.replace("/", "."); | ||
String group = properties.getProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP"); | ||
long timeoutMs = Long.parseLong(properties.getProperty("timeout", "3000")); | ||
return configService.getConfig(dataId, group, timeoutMs); | ||
} catch (NacosException e) { | ||
log.debug("exception for: {}", e.toString()); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isExisted(final String key) { | ||
return !Strings.isNullOrEmpty(getDirectly(key)); | ||
} | ||
|
||
@Override | ||
public List<String> getChildrenKeys(final String key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void persist(final String key, final String value) { | ||
update(key, value); | ||
} | ||
|
||
@Override | ||
public void update(final String key, final String value) { | ||
try { | ||
String dataId = key.replace("/", "."); | ||
String group = properties.getProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP"); | ||
configService.publishConfig(dataId, group, value); | ||
} catch (NacosException e) { | ||
log.debug("exception for: {}", e.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public void persistEphemeral(final String key, final String value) { | ||
|
||
} | ||
|
||
@Override | ||
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) { | ||
try { | ||
String dataId = key.replace("/", "."); | ||
String group = properties.getProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP"); | ||
configService.addListener(dataId, group, new Listener() { | ||
|
||
@Override | ||
public Executor getExecutor() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void receiveConfigInfo(final String configInfo) { | ||
dataChangedEventListener.onChange(new DataChangedEvent(key, configInfo, DataChangedEvent.ChangedType.UPDATED)); | ||
} | ||
}); | ||
} catch (NacosException e) { | ||
log.debug("exception for: {}", e.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public void initLock(final String key) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean tryLock() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void tryRelease() { | ||
|
||
} | ||
|
||
@Override | ||
public String getType() { | ||
return "nacos"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...esources/META-INF/services/org.apache.shardingsphere.orchestration.reg.api.RegistryCenter
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,18 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
org.apache.shardingsphere.orchestration.reg.nacos.NacosRegistryCenter |
50 changes: 50 additions & 0 deletions
50
.../test/java/org/apache/shardingsphere/orchestration/reg/nacos/NacosRegistryCenterTest.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,50 @@ | ||
/* | ||
* 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.shardingsphere.orchestration.reg.nacos; | ||
|
||
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenter; | ||
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.Properties; | ||
|
||
public class NacosRegistryCenterTest { | ||
|
||
private static RegistryCenter nacosRegistryCenter = new NacosRegistryCenter(); | ||
|
||
@BeforeClass | ||
public static void init() { | ||
Properties properties = new Properties(); | ||
properties.setProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP"); | ||
properties.setProperty("timeout", "3000"); | ||
RegistryCenterConfiguration configuration = new RegistryCenterConfiguration(nacosRegistryCenter.getType(), properties); | ||
configuration.setServerLists("127.0.0.1:8848"); | ||
nacosRegistryCenter.init(configuration); | ||
} | ||
|
||
@Test | ||
public void assertPersist() { | ||
nacosRegistryCenter.persist("sharding/test", "value1"); | ||
} | ||
|
||
@Test | ||
public void assertUpdate() { | ||
nacosRegistryCenter.update("sharding/test", "value2"); | ||
} | ||
} |