-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrate Zookeeper as configuration center with ZookeeperDynamicConf…
…igManager
- Loading branch information
Showing
7 changed files
with
301 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
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,112 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.alipay.sofa</groupId> | ||
<artifactId>sofa-rpc-config</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>sofa-rpc-config-zk</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alipay.sofa</groupId> | ||
<artifactId>sofa-rpc-log-common-tools</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alipay.sofa</groupId> | ||
<artifactId>sofa-rpc-log</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alipay.sofa</groupId> | ||
<artifactId>sofa-rpc-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.curator</groupId> | ||
<artifactId>curator-framework</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src/main/java</sourceDirectory> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>false</filtering> | ||
<includes> | ||
<include>**/**</include> | ||
</includes> | ||
</resource> | ||
</resources> | ||
<testSourceDirectory>src/test/java</testSourceDirectory> | ||
<testResources> | ||
<testResource> | ||
<directory>src/test/resources</directory> | ||
<filtering>false</filtering> | ||
<includes> | ||
<include>**/**</include> | ||
</includes> | ||
</testResource> | ||
</testResources> | ||
|
||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${maven.compiler.source}</source> | ||
<target>${maven.compiler.source}</target> | ||
<encoding>${project.build.sourceEncoding}</encoding> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<configuration> | ||
<skip>${module.install.skip}</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<configuration> | ||
<skip>${module.deploy.skip}</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<skipTests>${skipTests}</skipTests> | ||
<includes> | ||
<!-- 这里需要根据自己的需要指定要跑的单元测试 --> | ||
<include>**/*Test.java</include> | ||
</includes> | ||
<!-- 如无特殊需求,将forkMode设置为once --> | ||
<forkMode>once</forkMode> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
112 changes: 112 additions & 0 deletions
112
...config-zk/src/main/java/com/alipay/sofa/rpc/dynamic/zk/ZookeeperDynamicConfigManager.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,112 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.dynamic.zk; | ||
|
||
import com.alipay.sofa.rpc.auth.AuthRuleGroup; | ||
import com.alipay.sofa.rpc.common.utils.StringUtils; | ||
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeyHelper; | ||
import com.alipay.sofa.rpc.dynamic.DynamicConfigManager; | ||
import com.alipay.sofa.rpc.dynamic.DynamicHelper; | ||
import com.alipay.sofa.rpc.ext.Extension; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
|
||
/** | ||
* @author Narziss | ||
* @version ZookeeperDynamicConfigManager.java, v 0.1 2024年07月20日 09:23 Narziss | ||
*/ | ||
|
||
@Extension(value = "zookeeper", override = true) | ||
public class ZookeeperDynamicConfigManager extends DynamicConfigManager { | ||
|
||
private CuratorFramework zkClient; | ||
private static final String ADDRESS = "127.0.0.1:2181"; | ||
private static final String DEFAULT_NAMESPACE = "sofa-rpc"; | ||
private static final String CONFIG_NODE = "/config"; | ||
private static final String Default_GROUP = "/application"; | ||
|
||
protected ZookeeperDynamicConfigManager(String appName) { | ||
super(appName); | ||
zkClient = CuratorFrameworkFactory.builder() | ||
.connectString(ADDRESS) | ||
.retryPolicy(new ExponentialBackoffRetry(1000, 3)) | ||
.namespace(DEFAULT_NAMESPACE) | ||
.build(); | ||
zkClient.start(); | ||
} | ||
|
||
@Override | ||
public void initServiceConfiguration(String service) { | ||
//TODO not now | ||
} | ||
|
||
@Override | ||
public String getProviderServiceProperty(String service, String key) { | ||
try { | ||
String path = getPrefixPath() + DynamicConfigKeyHelper.buildProviderServiceProKey(service, key); | ||
byte[] bytes = zkClient.getData().forPath(path); | ||
return bytes != null ? new String(bytes) : DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} catch (Exception e) { | ||
return DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} | ||
} | ||
|
||
@Override | ||
public String getConsumerServiceProperty(String service, String key) { | ||
try { | ||
String path = getPrefixPath() + DynamicConfigKeyHelper.buildConsumerServiceProKey(service, key); | ||
byte[] bytes = zkClient.getData().forPath(path); | ||
return bytes != null ? new String(bytes) : DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} catch (Exception e) { | ||
return DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} | ||
} | ||
|
||
@Override | ||
public String getProviderMethodProperty(String service, String method, String key) { | ||
try { | ||
String path = getPrefixPath() + DynamicConfigKeyHelper.buildProviderMethodProKey(service, method, key); | ||
byte[] bytes = zkClient.getData().forPath(path); | ||
return bytes != null ? new String(bytes) : DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} catch (Exception e) { | ||
return DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} | ||
} | ||
|
||
@Override | ||
public String getConsumerMethodProperty(String service, String method, String key) { | ||
try { | ||
String path = getPrefixPath() + DynamicConfigKeyHelper.buildConsumerMethodProKey(service, method, key); | ||
byte[] bytes = zkClient.getData().forPath(path); | ||
return bytes != null ? new String(bytes) : DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} catch (Exception e) { | ||
return DynamicHelper.DEFAULT_DYNAMIC_VALUE; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public AuthRuleGroup getServiceAuthRule(String service) { | ||
//TODO 暂不支持 | ||
return null; | ||
} | ||
|
||
public String getPrefixPath() { | ||
return CONFIG_NODE+Default_GROUP+StringUtils.CONTEXT_SEP; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ain/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.dynamic.DynamicConfigManager
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 @@ | ||
zookeeper=com.alipay.sofa.rpc.dynamic.zk.ZookeeperDynamicConfigManager |
53 changes: 53 additions & 0 deletions
53
...ig-zk/src/test/java/com/alipay/sofa/rpc/dynamic/zk/ZookeeperDynamicConfigManagerTest.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,53 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.dynamic.zk; | ||
|
||
import com.alipay.sofa.rpc.dynamic.DynamicHelper; | ||
import com.alipay.sofa.rpc.log.Logger; | ||
import com.alipay.sofa.rpc.log.LoggerFactory; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class ZookeeperDynamicConfigManagerTest { | ||
|
||
private final static Logger logger = LoggerFactory | ||
.getLogger(ZookeeperDynamicConfigManager.class); | ||
|
||
private ZookeeperDynamicConfigManager zookeeperDynamicConfigManager = new ZookeeperDynamicConfigManager("test"); | ||
|
||
@Test | ||
public void getProviderServiceProperty() { | ||
String result = zookeeperDynamicConfigManager.getProviderServiceProperty("serviceName", "timeout"); | ||
Assert.assertEquals(DynamicHelper.DEFAULT_DYNAMIC_VALUE, result); | ||
} | ||
|
||
@Test | ||
public void getConsumerServiceProperty() { | ||
} | ||
|
||
@Test | ||
public void getProviderMethodProperty() { | ||
} | ||
|
||
@Test | ||
public void getConsumerMethodProperty() { | ||
} | ||
|
||
@Test | ||
public void getServiceAuthRule() { | ||
} | ||
} |
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,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> | ||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> | ||
|
||
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> | ||
<layout class="org.apache.log4j.PatternLayout"> | ||
<param name="ConversionPattern" value="%d %t %5p [%c:%M:%L] - %m%n"/> | ||
</layout> | ||
</appender> | ||
|
||
<root> | ||
<level value="INFO"/> | ||
<appender-ref ref="CONSOLE"/> | ||
</root> | ||
|
||
</log4j:configuration> |
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