org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds2-sentinel-nacos-datasource': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1827) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1265) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.registerBean(SentinelDataSourceHandler.java:203) ~[spring-cloud-starter-alibaba-sentinel-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.lambda$afterSingletonsInstantiated$0(SentinelDataSourceHandler.java:93) ~[spring-cloud-starter-alibaba-sentinel-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at java.util.TreeMap.forEach(TreeMap.java:1001) ~[na:1.8.0_65]
at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.afterSingletonsInstantiated(SentinelDataSourceHandler.java:80) ~[spring-cloud-starter-alibaba-sentinel-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:914) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at com.lsp.sentinel.Sentinel_8401.main(Sentinel_8401.java:12) ~[classes/:na]
Caused by: java.lang.NullPointerException: null
at java.util.Hashtable.put(Hashtable.java:459) ~[na:1.8.0_65]
at java.util.Properties.setProperty(Properties.java:166) ~[na:1.8.0_65]
at com.alibaba.cloud.sentinel.datasource.factorybean.NacosDataSourceFactoryBean.getObject(NacosDataSourceFactoryBean.java:70) ~[spring-cloud-alibaba-sentinel-datasource-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at com.alibaba.cloud.sentinel.datasource.factorybean.NacosDataSourceFactoryBean.getObject(NacosDataSourceFactoryBean.java:34) ~[spring-cloud-alibaba-sentinel-datasource-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:169) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 20 common frames omitted
在采用sentinel持久化到nacos的时候,发现了报错信息,但是配置文件的信息和nacos上编写的json配置是对应的
spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
#说明我们微服务名称是哪一个
spring.cloud.sentinel.datasource.ds2.nacos.data-id=cloudalibaba-sentinel-service-8401
#默认分组
spring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUP
#json串
spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
#规则类型
spring.cloud.sentinel.datasource.ds2.nacos.rule-type=flow
但是就是报错在创建bean的时候报空了,但是配置文件以及连接什么的既然都没有问题,初步判断了下nacos和sentinel没有达到连接,有了这个想法就开始往这个想法里找错。
最终在发现是以前埋的一个坑,在刚开始下载nacos的时候配置文件修改了登录认证的操作,然后从此不管在nacos提供服务还是消费都需要带上username和password,这个也不例外。
最终修改配置文件
spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
#说明我们微服务名称是哪一个
spring.cloud.sentinel.datasource.ds2.nacos.data-id=cloudalibaba-sentinel-service-8401
#默认分组
spring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds2.nacos.username=nacos
spring.cloud.sentinel.datasource.ds2.nacos.password=nacos
#json串
spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
#规则类型
spring.cloud.sentinel.datasource.ds2.nacos.rule-type=flow
加上用户名和密码重新启动项目,发现就启动ok