diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/PolarisRegistryConfiguration.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/PolarisRegistryConfiguration.java new file mode 100644 index 000000000..4ffef95f0 --- /dev/null +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/PolarisRegistryConfiguration.java @@ -0,0 +1,38 @@ +/* + * 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.boot.autoconfigure.rpc; + +import com.alipay.sofa.boot.autoconfigure.condition.ConditionalOnSwitch; +import com.alipay.sofa.rpc.boot.config.PolarisRegistryConfigurator; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author chengming + * @version PolarisRegistryConfiguration.java, v 0.1 2024年02月27日 4:02 PM chengming + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnSwitch(value = "rpcPolarisRegistryConfiguration") +public class PolarisRegistryConfiguration { + + @Bean + @ConditionalOnMissingBean + public PolarisRegistryConfigurator polarisRegistryConfigurator() { + return new PolarisRegistryConfigurator(); + } +} diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/RegistryConfigurations.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/RegistryConfigurations.java index 64af355f2..ec6e6570d 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/RegistryConfigurations.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/RegistryConfigurations.java @@ -30,6 +30,7 @@ public static String[] registryConfigurationClass() { MulticastRegistryConfiguration.class.getName(), MeshRegistryConfiguration.class.getName(), ConsulRegistryConfiguration.class.getName(), - SofaRegistryConfiguration.class.getName() }; + SofaRegistryConfiguration.class.getName(), + PolarisRegistryConfiguration.class.getName() }; } } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/PolarisRegistryConfigurator.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/PolarisRegistryConfigurator.java new file mode 100644 index 000000000..3c735f87a --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/PolarisRegistryConfigurator.java @@ -0,0 +1,48 @@ +/* + * 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; + +/** + * @author chengming + * @version PolarisRegistryConfigurator.java, v 0.1 2024年02月27日 3:52 PM chengming + */ +public class PolarisRegistryConfigurator implements RegistryConfigureProcessor { + + public PolarisRegistryConfigurator() { + } + + @Override + public RegistryConfig buildFromAddress(String address) { + String polarisAddress = RegistryParseUtil.parseAddress(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_POLARIS); + Map map = RegistryParseUtil.parseParam(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_POLARIS); + + return new RegistryConfig().setAddress(polarisAddress).setParameters(map) + .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_POLARIS); + } + + @Override + public String registryType() { + return SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_POLARIS; + } +} diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java index 8ab776b03..bc6b66849 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java @@ -69,6 +69,8 @@ public class SofaBootRpcConfigConstants { //@since 5.5.2 public static final String REGISTRY_PROTOCOL_SOFA = "sofa"; + public static final String REGISTRY_PROTOCOL_POLARIS = "polaris"; + /* server */ public static final String RPC_PROTOCOL_BOLT = "bolt"; public static final String RPC_PROTOCOL_REST = "rest"; diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/config/PolarisRegistryConfiguratorTests.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/config/PolarisRegistryConfiguratorTests.java new file mode 100644 index 000000000..5c2504776 --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/config/PolarisRegistryConfiguratorTests.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.test.config; + +import com.alipay.sofa.rpc.boot.config.PolarisRegistryConfigurator; +import com.alipay.sofa.rpc.config.RegistryConfig; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author chengming + * @version PolarisRegistryConfiguratorTests.java, v 0.1 2024年02月27日 4:04 PM chengming + */ +public class PolarisRegistryConfiguratorTests { + + @Test + public void buildFromAddress() { + String address = "polaris://30.183.168.90:12202?cluster=test"; + + PolarisRegistryConfigurator polarisRegistryConfigurator = new PolarisRegistryConfigurator(); + RegistryConfig registryConfig = polarisRegistryConfigurator.buildFromAddress(address); + + assertThat(registryConfig).isNotNull(); + assertThat("polaris").isEqualTo(registryConfig.getProtocol()); + assertThat("30.183.168.90:12202").isEqualTo(registryConfig.getAddress()); + assertThat(registryConfig.getParameters()).isNotNull(); + assertThat("test").isEqualTo(registryConfig.getParameter("cluster")); + } +} \ No newline at end of file