From 61d32e5f1a8326e5199f58ec80a388f7be2c30d3 Mon Sep 17 00:00:00 2001 From: Lei Zhiyuan Date: Thu, 11 Apr 2019 19:35:05 +0800 Subject: [PATCH] Support for sofa registry. (#166) * support for sofa registry * fix case --- .../config/SofaBootRpcConfigConstants.java | 2 + .../boot/config/SofaRegistryConfigurator.java | 47 +++++++++++++++++++ .../config/SofaRegistryConfiguratorTest.java | 44 +++++++++++++++++ .../boot/SofaBootRpcAutoConfiguration.java | 8 ++++ 4 files changed, 101 insertions(+) create mode 100644 sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfigurator.java create mode 100644 sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfiguratorTest.java diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java index 52eec38..d9fc2d7 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java @@ -62,6 +62,8 @@ public class SofaBootRpcConfigConstants { public static final String REGISTRY_PROTOCOL_CONSUL = "consul"; public static final String REGISTRY_PROTOCOL_NACOS = "nacos"; + //@since 5.5.2 + public static final String REGISTRY_PROTOCOL_SOFA = "sofa"; /* server */ public static final String RPC_PROTOCOL_BOLT = "bolt"; diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfigurator.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfigurator.java new file mode 100644 index 0000000..e8bd132 --- /dev/null +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfigurator.java @@ -0,0 +1,47 @@ +/* + * 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.boot.config; + +import com.alipay.sofa.rpc.boot.common.RegistryParseUtil; +import com.alipay.sofa.rpc.config.RegistryConfig; + +import java.util.Map; + +/** + * SOFARegistry 配置 + *

+ * 配置格式: com.alipay.sofa.rpc.registry.address=sofa://xxx:9600?k1=v1 + * + * @author LiWei + */ +public class SofaRegistryConfigurator implements RegistryConfigureProcessor { + + public SofaRegistryConfigurator() { + } + + @Override + public RegistryConfig buildFromAddress(String address) { + String sofaRegistryAddress = RegistryParseUtil.parseAddress(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA); + Map map = RegistryParseUtil.parseParam(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA); + + return new RegistryConfig().setAddress(sofaRegistryAddress) + .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA) + .setParameters(map); + } +} \ No newline at end of file diff --git a/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfiguratorTest.java b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfiguratorTest.java new file mode 100644 index 0000000..ac63a6d --- /dev/null +++ b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/SofaRegistryConfiguratorTest.java @@ -0,0 +1,44 @@ +/* + * 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.boot.config; + +import com.alipay.sofa.rpc.config.RegistryConfig; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author zhiyuan.lzy + * @version $Id: SofaRegistryConfiguratorTest.java, v 0.1 2010-03-16 17:36 zhiyuan.lzy Exp $$ + */ +public class SofaRegistryConfiguratorTest { + + @Test + public void buildFromAddress() { + String address = "sofa://127.0.0.1:9603?cluster=test"; + + SofaRegistryConfigurator sofaRegistryConfigurator = new SofaRegistryConfigurator(); + RegistryConfig registryConfig = sofaRegistryConfigurator.buildFromAddress(address); + + assertNotNull(registryConfig); + assertEquals("sofa", registryConfig.getProtocol()); + assertEquals("127.0.0.1:9603", registryConfig.getAddress()); + assertNotNull(registryConfig.getParameters()); + assertEquals("test", registryConfig.getParameter("cluster")); + } +} \ No newline at end of file diff --git a/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java b/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java index 8e18d7f..a2434a5 100644 --- a/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java +++ b/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java @@ -25,6 +25,7 @@ import com.alipay.sofa.rpc.boot.config.RegistryConfigureProcessor; import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants; import com.alipay.sofa.rpc.boot.config.SofaBootRpcProperties; +import com.alipay.sofa.rpc.boot.config.SofaRegistryConfigurator; import com.alipay.sofa.rpc.boot.config.ZookeeperConfigurator; import com.alipay.sofa.rpc.boot.container.ConsumerConfigContainer; import com.alipay.sofa.rpc.boot.container.ProviderConfigContainer; @@ -111,6 +112,11 @@ public RegistryConfigureProcessor nacosConfigurator() { return new NacosConfigurator(); } + @Bean + public RegistryConfigureProcessor sofaRegistryConfigurator() { + return new SofaRegistryConfigurator(); + } + @Bean(name = "registryConfigMap") public Map configureProcessorMap() { Map map = new HashMap(); @@ -119,6 +125,8 @@ public Map configureProcessorMap() { map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH, meshConfigurator()); map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL, consulConfigurator()); map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS, nacosConfigurator()); + map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA, sofaRegistryConfigurator()); + return map; }