You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/11/30 05:54:50 UTC

[shardingsphere] branch master updated: instead of faq#27 add faq#29 (#13824)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 52a8136  instead of faq#27 add faq#29 (#13824)
52a8136 is described below

commit 52a8136dde77a96a463a4eb1003cdd03b6c654e9
Author: cheese8 <yi...@163.com>
AuthorDate: Tue Nov 30 13:54:05 2021 +0800

    instead of faq#27 add faq#29 (#13824)
    
    * instead of faq#27, add faq#29
    
    * instead of faq#27, add faq#29
    
    * instead of faq#27, add faq#29
    
    * instead of faq#27, add faq#29
    
    * instead of faq#27, add faq#29
---
 docs/document/content/reference/faq/_index.cn.md | 44 ++++++++++++++++++++++++
 docs/document/content/reference/faq/_index.en.md | 44 ++++++++++++++++++++++++
 2 files changed, 88 insertions(+)

diff --git a/docs/document/content/reference/faq/_index.cn.md b/docs/document/content/reference/faq/_index.cn.md
index a77e39a..561bd0f 100644
--- a/docs/document/content/reference/faq/_index.cn.md
+++ b/docs/document/content/reference/faq/_index.cn.md
@@ -334,3 +334,47 @@ ShardingSphere 中很多功能实现类的加载方式是通过 [SPI](/cn/concep
 更多关于 alias 使用方法请参考 [Proxool官网](http://proxool.sourceforge.net/configure.html)。
 
 PS:sourceforge 网站需要翻墙访问。
+
+## 29. [其他] 使用 Spring Boot 2.x 集成 ShardingSphere 时,配置文件中的属性设置不生效?
+
+回答:
+
+需要特别注意,Spring Boot 2.x 环境下配置文件的属性名称约束为仅允许小写字母、数字和短横线,即`[a-z][0-9]`和`-`。
+
+原因如下:
+
+Spring Boot 2.x 环境下,ShardingSphere 通过 Binder 来绑定配置文件,属性名称不规范(如:驼峰或下划线等)会导致属性设置不生效从而校验属性值时抛出 `NullPointerException` 异常。参考以下错误示例:
+
+下划线示例:database_inline
+```
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm-expression=ds-$->{user_id % 2}
+```
+```
+Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'database_inline': Initialization of bean failed; nested exception is java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	... 
+Caused by: java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.getAlgorithmExpression(InlineShardingAlgorithm.java:58)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.init(InlineShardingAlgorithm.java:52)
+	at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.postProcessAfterInitialization(AbstractAlgorithmProvidedBeanRegistry.java:98)
+	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
+	... 
+```
+驼峰示例:databaseInline
+```
+spring.shardingsphere.rules.sharding.sharding-algorithms.databaseInline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.databaseInline.props.algorithm-expression=ds-$->{user_id % 2}
+```
+```
+Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseInline': Initialization of bean failed; nested exception is java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	... 
+Caused by: java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.getAlgorithmExpression(InlineShardingAlgorithm.java:58)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.init(InlineShardingAlgorithm.java:52)
+	at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.postProcessAfterInitialization(AbstractAlgorithmProvidedBeanRegistry.java:98)
+	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
+	... 
+```
+从异常堆栈中分析可知: `AbstractAlgorithmProvidedBeanRegistry.registerBean` 方法调用 `PropertyUtil.containPropertyPrefix(environment, prefix)` 方法判断指定前缀 `prefix` 的配置是否存在,而 `PropertyUtil.containPropertyPrefix(environment, prefix)` 方法,在 Spring Boot 2.x 环境下使用了 Binder,不规范的属性名称(如:驼峰或下划线等)会导致属性设置不生效。
\ No newline at end of file
diff --git a/docs/document/content/reference/faq/_index.en.md b/docs/document/content/reference/faq/_index.en.md
index 4ef9ed8..0f5b1d6 100644
--- a/docs/document/content/reference/faq/_index.en.md
+++ b/docs/document/content/reference/faq/_index.en.md
@@ -341,3 +341,47 @@ The followings are core codes from ProxoolDataSource getConnection method in `Pr
 ```
 
 For more alias usages, please refer to [Proxool](http://proxool.sourceforge.net/configure.html) official website.
+
+## 29. [Other] The property settings in the configuration file do not take effect when integrating ShardingSphere with Spring Boot 2.x ?
+
+Answer:
+
+Note that the property name in the Spring Boot 2.x environment is constrained to allow only lowercase letters, numbers and short transverse lines, `[a-z][0-9]` and `-`.
+
+Reasons:
+
+In the Spring Boot 2.x environment, ShardingSphere binds the properties through Binder, and the unsatisfied property name (such as camel case or underscore.) can throw a `NullPointerException` exception when the property setting does not work to check the property value. Refer to the following error examples:
+
+Underscore case: database_inline
+```
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm-expression=ds-$->{user_id % 2}
+```
+```
+Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'database_inline': Initialization of bean failed; nested exception is java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	... 
+Caused by: java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.getAlgorithmExpression(InlineShardingAlgorithm.java:58)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.init(InlineShardingAlgorithm.java:52)
+	at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.postProcessAfterInitialization(AbstractAlgorithmProvidedBeanRegistry.java:98)
+	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
+	... 
+```
+Camel case:databaseInline
+```
+spring.shardingsphere.rules.sharding.sharding-algorithms.databaseInline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.databaseInline.props.algorithm-expression=ds-$->{user_id % 2}
+```
+```
+Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseInline': Initialization of bean failed; nested exception is java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	... 
+Caused by: java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
+	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.getAlgorithmExpression(InlineShardingAlgorithm.java:58)
+	at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.init(InlineShardingAlgorithm.java:52)
+	at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.postProcessAfterInitialization(AbstractAlgorithmProvidedBeanRegistry.java:98)
+	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
+	... 
+```
+From the exception stack, the `AbstractAlgorithmProvidedBeanRegistry.registerBean` method calls `PropertyUtil.containPropertyPrefix (environment, prefix)` , and `PropertyUtil.containPropertyPrefix (environment, prefix)` determines that the configuration of the specified prefix does not exist, while the method uses Binder in an unsatisfied property name (such as camelcase or underscore) causing property settings does not to take effect.
\ No newline at end of file