本篇分析一下Nacos同步数据原理
1、先配置一下环境
soul:
sync:
nacos:
url: localhost:8848
namespace: 1c10d748-af86-43b9-8265-75f487d20c6c
# acm:
# enabled: false
# endpoint: acm.aliyun.com
# namespace:
# accessKey:
# secretKey:
soul-admin/pom.xml,这里默认配置是有的
<dependency>
<groupId>com.alibaba.nacosgroupId>
<artifactId>nacos-clientartifactId>
<version>${nacos-client.version}version>
dependency>
soul :
sync:
nacos:
url: localhost:8848
namespace: 1c10d748-af86-43b9-8265-75f487d20c6c
# acm:
# enabled: false
# endpoint: acm.aliyun.com
# namespace:
# accessKey:
# secretKey:
soul-bootstrap/pom.xml,下面的配置默认是没有的,需要手动添加
<dependency>
<groupId>org.dromaragroupId>
<artifactId>soul-spring-boot-starter-sync-data-nacosartifactId>
<version>${project.version}version>
dependency>
1、启动 nacos
2、启动 soul-admin
3、启动 soul-bootstrap
2、上面看着挺顺利,这个过程遇到了坑,soul-bootstrap 启动不起来报空指针异常,下面详细记录一下。
首先soul-admin启动后不会主动向nacos同步网关数据,需要手动同步,官网这一点没有提到。这个问题绊了我好久,最后是看到了群里其他同学遇到了同样的问题,参考了他们的文章才解决,下面记录一下解决过程。
1)soul-bootstrap 启动的时候遇到了如下的错误,NullPointerException。
soul-bootstrap 启动的时候会去,nacos获取网关数据,看到下面的断点,拿到的是空数据。
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-25 16:49:06.052 ERROR 5273 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nacosSyncDataService' defined in class path resource [org/dromara/soul/springboot/starter/sync/data/nacos/NacosSyncDataConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.dromara.soul.sync.data.api.SyncDataService]: Factory method 'nacosSyncDataService' threw exception; nested exception is java.lang.NullPointerException
......
at org.dromara.soul.bootstrap.SoulBootstrapApplication.main(SoulBootstrapApplication.java:37) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.dromara.soul.sync.data.api.SyncDataService]: Factory method 'nacosSyncDataService' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 19 common frames omitted
Caused by: java.lang.NullPointerException: null
at org.dromara.soul.sync.data.nacos.handler.NacosCacheHandler.updateMetaDataMap(NacosCacheHandler.java:128) ~[classes/:na]
at org.dromara.soul.sync.data.nacos.handler.NacosCacheHandler.watcherData(NacosCacheHandler.java:167) ~[classes/:na]
at org.dromara.soul.sync.data.nacos.NacosSyncDataService.start(NacosSyncDataService.java:59) ~[classes/:na]
at org.dromara.soul.sync.data.nacos.NacosSyncDataService.(NacosSyncDataService.java:49) ~[classes/:na]
at org.dromara.soul.springboot.starter.sync.data.nacos.NacosSyncDataConfiguration.nacosSyncDataService(NacosSyncDataConfiguration.java:66) ~[classes/:na]
at org.dromara.soul.springboot.starter.sync.data.nacos.NacosSyncDataConfiguration$$EnhancerBySpringCGLIB$$cce084b7.CGLIB$nacosSyncDataService$0() ~[classes/:na]
at org.dromara.soul.springboot.starter.sync.data.nacos.NacosSyncDataConfiguration$$EnhancerBySpringCGLIB$$cce084b7$$FastClassBySpringCGLIB$$3830e886.invoke() ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.dromara.soul.springboot.starter.sync.data.nacos.NacosSyncDataConfiguration$$EnhancerBySpringCGLIB$$cce084b7.nacosSyncDataService() ~[classes/:na]
......
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 20 common frames omitted
2)到nacos去看一下是否有网关的数据,根据配置的 “namespace: 1c10d748-af86-43b9-8265-75f487d20c6c” 结果是什么都没有。
3、尝试去soul-admin手动同步,nacos也看不到数据,必须需要手动创建命名空间“1c10d748-af86-43b9-8265-75f487d20c6c”,如下图。
4、去soul-admin 手动同步数据后,就看到了nacos上有了网关的配置信息,这时候soul-bootstrap 还是启动不起来,因为这里还缺少元数据信息。元数据只有 dubbo、springcloud 服务有数据,http是没有元数据的,所以还得去启动一下dubbo服务。然后在soul-admin同步一下元数据。
soul-admin 点击同步数据,将元数据会同步到nacos
soul-admin 点击同步数据,将认证数据会同步到nacos
这时候 nacos 已经看到了全部的网关数据
5、再去启动soul-bootstrap,终于启动成功
2021-01-25 17:56:54.798 INFO 10051 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[monitor] [org.dromara.soul.plugin.monitor.MonitorPlugin]
2021-01-25 17:56:54.798 INFO 10051 --- [ main] o.d.s.w.configuration.SoulConfiguration : load plugin:[response] [org.dromara.soul.plugin.httpclient.response.WebClientResponsePlugin]
2021-01-25 17:56:54.990 INFO 10051 --- [ main] d.s.s.s.s.d.n.NacosSyncDataConfiguration : you use nacos sync soul data.......
2021-01-25 17:56:58.890 INFO 10051 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-01-25 17:56:59.758 INFO 10051 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 9195
2021-01-25 17:56:59.764 INFO 10051 --- [ main] o.d.s.b.SoulBootstrapApplication : Started SoulBootstrapApplication in 8.401 seconds (JVM running for 9.95)
6、总结:
配置下来感觉使用nacos同步数据不是很友好,配置过程遇到了很多坑,首先soul-admin 不会主动同步网关数据到nacos,需要手动同步。soul-bootstrap必须依赖所有的网关配置数据 soul.plugin、soul.selector、soul.selector、soul.meta、soul.auth,缺一不可。如果网关只代理http服务(无元数据),soul-bootstrap是启动不起来的。官网这一块没有做详细说明,对小白不是很友好。