From 6d58917fa6dadf4e8b38da6303e00e14e4a0f770 Mon Sep 17 00:00:00 2001 From: HuGuirong Date: Thu, 23 Dec 2021 12:29:18 +0800 Subject: [PATCH] NacosDataSourceProperties support for contextPath configuration items Closes gh-2327 --- .../config/NacosDataSourceProperties.java | 10 ++++++++++ .../factorybean/NacosDataSourceFactoryBean.java | 13 +++++++++++++ .../datasource/NacosDataSourceFactoryBeanTests.java | 4 ++++ .../datasource/NacosDataSourcePropertiesTests.java | 2 ++ 4 files changed, 29 insertions(+) diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java index 08790ac4b..d52c9c3b2 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java @@ -32,6 +32,8 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties { private String serverAddr; + private String contextPath; + private String username; private String password; @@ -71,6 +73,14 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties { this.serverAddr = serverAddr; } + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + } + public String getUsername() { return username; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java index e8bf42e4c..d04dd1f95 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java @@ -35,6 +35,8 @@ public class NacosDataSourceFactoryBean implements FactoryBean private String serverAddr; + private String contextPath; + private String username; private String password; @@ -63,6 +65,9 @@ public class NacosDataSourceFactoryBean implements FactoryBean properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint); } + if (!StringUtils.isEmpty(this.contextPath)) { + properties.setProperty(PropertyKeyConst.CONTEXT_PATH, this.contextPath); + } if (!StringUtils.isEmpty(this.accessKey)) { properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey); } @@ -94,6 +99,14 @@ public class NacosDataSourceFactoryBean implements FactoryBean this.serverAddr = serverAddr; } + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + } + public String getUsername() { return username; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourceFactoryBeanTests.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourceFactoryBeanTests.java index ca364f30e..cb4b826e3 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourceFactoryBeanTests.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourceFactoryBeanTests.java @@ -39,6 +39,8 @@ public class NacosDataSourceFactoryBeanTests { private String serverAddr = "localhost:8848"; + private String contextPath = "/my-nacos"; + private String accessKey = "ak"; private String secretKey = "sk"; @@ -56,6 +58,7 @@ public class NacosDataSourceFactoryBeanTests { factoryBean.setDataId(dataId); factoryBean.setGroupId(groupId); factoryBean.setServerAddr(serverAddr); + factoryBean.setContextPath(contextPath); factoryBean.setConverter(converter); NacosDataSource nacosDataSource = mock(NacosDataSource.class); @@ -69,6 +72,7 @@ public class NacosDataSourceFactoryBeanTests { assertThat(factoryBean.getDataId()).isEqualTo(dataId); assertThat(factoryBean.getGroupId()).isEqualTo(groupId); assertThat(factoryBean.getServerAddr()).isEqualTo(serverAddr); + assertThat(factoryBean.getContextPath()).isEqualTo(contextPath); } @Test diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourcePropertiesTests.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourcePropertiesTests.java index f897d6ed8..cde739d06 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourcePropertiesTests.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourcePropertiesTests.java @@ -31,11 +31,13 @@ public class NacosDataSourcePropertiesTests { public void testNacosWithAddr() { NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties(); nacosDataSourceProperties.setServerAddr("127.0.0.1:8848"); + nacosDataSourceProperties.setContextPath("/my-nacos"); nacosDataSourceProperties.setRuleType(RuleType.FLOW); nacosDataSourceProperties.setDataId("sentinel"); nacosDataSourceProperties.setGroupId("custom-group"); nacosDataSourceProperties.setDataType("xml"); + assertThat(nacosDataSourceProperties.getContextPath()).isEqualTo("/my-nacos"); assertThat(nacosDataSourceProperties.getGroupId()).isEqualTo("custom-group"); assertThat(nacosDataSourceProperties.getDataId()).isEqualTo("sentinel"); assertThat(nacosDataSourceProperties.getDataType()).isEqualTo("xml");