Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nacos service discovery support #3447

Merged
merged 23 commits into from
Jan 3, 2021
Merged

Conversation

klboke
Copy link
Contributor

@klboke klboke commented Dec 19, 2020

新增nacos服务发现支持,使用方法如下:
启用外部的nacos服务注册中心分两步走,configService和adminService的操作一样,如下:

  • 1、修改VM options,激活nacosDiscovery的profile,如:
-Dapollo_profile=github,auth,nacosDiscovery
  • 2、前往application-github.properties配置nacos连接地址,如:
#nacos discovery
nacos.discovery.server-addr=127.0.0.1:8848

也可采用VM options的方式配置,如:

-Dnacos.discovery.server-addr=127.0.0.1:8848

@klboke
Copy link
Contributor Author

klboke commented Dec 19, 2020

#3444

@nobodyiam
Copy link
Member

nobodyiam commented Dec 21, 2020

I checked out this branch, start the application but failed...
It looks like the nacos.discovery.enabled is default to true, which activates nacos discovery by default...

java.io.IOException: Error while requesting: http:///nacos/serverlist'. Server returned: 500
	at com.alibaba.nacos.client.naming.net.NamingProxy.getServerListFromEndpoint(NamingProxy.java:142)
	at com.alibaba.nacos.client.naming.net.NamingProxy.refreshSrvIfNeed(NamingProxy.java:175)
	at com.alibaba.nacos.client.naming.net.NamingProxy.access$000(NamingProxy.java:60)
	at com.alibaba.nacos.client.naming.net.NamingProxy$2.run(NamingProxy.java:118)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
java.io.IOException: Error while requesting: http:///nacos/serverlist'. Server returned: 500
	at com.alibaba.nacos.client.naming.net.NamingProxy.getServerListFromEndpoint(NamingProxy.java:142)
	at com.alibaba.nacos.client.naming.net.NamingProxy.refreshSrvIfNeed(NamingProxy.java:175)
	at com.alibaba.nacos.client.naming.net.NamingProxy.initRefreshTask(NamingProxy.java:130)
	at com.alibaba.nacos.client.naming.net.NamingProxy.<init>(NamingProxy.java:100)
	at com.alibaba.nacos.client.naming.NacosNamingService.init(NacosNamingService.java:96)
	at com.alibaba.nacos.client.naming.NacosNamingService.<init>(NacosNamingService.java:85)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:45)
	at com.alibaba.nacos.api.NacosFactory.createNamingService(NacosFactory.java:76)

Then I configured nacos.discovery.enabled=false, but it still failed to start. It looks to me the spring property sources are somehow wrong after nacos dependency is added.

Caused by: java.sql.SQLSyntaxErrorException: Table 'apolloconfigdb.server_config' doesn't exist
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1005)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
	... 121 common frames omitted

@klboke
Copy link
Contributor Author

klboke commented Dec 21, 2020

I checked out this branch, start the application but failed...
It looks like the nacos.discovery.enabled is default to true, which activates nacos discovery by default...

java.io.IOException: Error while requesting: http:///nacos/serverlist'. Server returned: 500
	at com.alibaba.nacos.client.naming.net.NamingProxy.getServerListFromEndpoint(NamingProxy.java:142)
	at com.alibaba.nacos.client.naming.net.NamingProxy.refreshSrvIfNeed(NamingProxy.java:175)
	at com.alibaba.nacos.client.naming.net.NamingProxy.access$000(NamingProxy.java:60)
	at com.alibaba.nacos.client.naming.net.NamingProxy$2.run(NamingProxy.java:118)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
java.io.IOException: Error while requesting: http:///nacos/serverlist'. Server returned: 500
	at com.alibaba.nacos.client.naming.net.NamingProxy.getServerListFromEndpoint(NamingProxy.java:142)
	at com.alibaba.nacos.client.naming.net.NamingProxy.refreshSrvIfNeed(NamingProxy.java:175)
	at com.alibaba.nacos.client.naming.net.NamingProxy.initRefreshTask(NamingProxy.java:130)
	at com.alibaba.nacos.client.naming.net.NamingProxy.<init>(NamingProxy.java:100)
	at com.alibaba.nacos.client.naming.NacosNamingService.init(NacosNamingService.java:96)
	at com.alibaba.nacos.client.naming.NacosNamingService.<init>(NacosNamingService.java:85)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:45)
	at com.alibaba.nacos.api.NacosFactory.createNamingService(NacosFactory.java:76)

Then I configured nacos.discovery.enabled=false, but it still failed to start. It looks to me the spring property sources are somehow wrong after nacos dependency is added.

Caused by: java.sql.SQLSyntaxErrorException: Table 'apolloconfigdb.server_config' doesn't exist
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1005)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
	... 121 common frames omitted

I overlooked that nacos is turned on by default, but you can start the project after disabling it. nacos-discovery-spring-boot-starter only includes spring integration of service discovery part, it has nothing to do with config. If you still cannot get up after disabling it, is this an environmental issue?

@codecov-io
Copy link

codecov-io commented Dec 21, 2020

Codecov Report

Merging #3447 (da6b767) into master (ad63e14) will decrease coverage by 0.02%.
The diff coverage is 85.71%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3447      +/-   ##
============================================
- Coverage     51.41%   51.39%   -0.03%     
- Complexity     2316     2318       +2     
============================================
  Files           441      442       +1     
  Lines         13823    13843      +20     
  Branches       1408     1408              
============================================
+ Hits           7107     7114       +7     
- Misses         6226     6237      +11     
- Partials        490      492       +2     
Impacted Files Coverage Δ Complexity Δ
...o/metaservice/service/DefaultDiscoveryService.java 100.00% <ø> (ø) 6.00 <0.00> (ø)
...llo/metaservice/service/NacosDiscoveryService.java 85.00% <85.00%> (ø) 6.00 <6.00> (?)
...llo/metaservice/controller/HomePageController.java 100.00% <100.00%> (ø) 2.00 <1.00> (?)
...work/apollo/biz/message/DatabaseMessageSender.java 56.25% <0.00%> (-8.34%) 6.00% <0.00%> (-2.00%)
...rk/apollo/spring/property/SpringValueRegistry.java 83.33% <0.00%> (-5.56%) 10.00% <0.00%> (-1.00%)
...ework/apollo/internals/RemoteConfigRepository.java 86.25% <0.00%> (-3.13%) 24.00% <0.00%> (-1.00%)
.../framework/apollo/spring/property/SpringValue.java 87.71% <0.00%> (-1.76%) 20.00% <0.00%> (-1.00%)
.../apollo/internals/RemoteConfigLongPollService.java 78.31% <0.00%> (+1.20%) 29.00% <0.00%> (+1.00%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 843cd94...da6b767. Read the comment docs.

@Anilople Anilople added feature Categorizes issue as related to a new feature. area/metaserver apollo meta server labels Dec 22, 2020
@nobodyiam
Copy link
Member

@klboke

ApolloApplication starts with the above errors...

I did some debugging and found nacos-client has an application.properties which overrides apollo-common/src/main/resources/application.properties

image

@klboke
Copy link
Contributor Author

klboke commented Dec 26, 2020

It turns out that the problem is here. I have always used separate ConfigService and AdminService to start services, so we see different phenomena. I will continue to see the solution

@nobodyiam
Copy link
Member

nobodyiam commented Dec 26, 2020

After I activated the nacosDiscovery profile, I got this error...

java.lang.NullPointerException: null
	at com.ctrip.framework.apollo.metaservice.service.NacosDiscoveryService.getServiceInstances(NacosDiscoveryService.java:37)
	at com.ctrip.framework.apollo.metaservice.controller.ServiceController.getConfigService(ServiceController.java:35)

@klboke
Copy link
Contributor Author

klboke commented Dec 26, 2020

@nobodyiam The reason for the problem is because you are running in ApolloApplication, I have not tested it in this mode

@klboke
Copy link
Contributor Author

klboke commented Dec 29, 2020

@nobodyiam 通过maven的profile隔离了nacos-discovery-spring-boot-starter的依赖,默认只有nacos-api的依赖解决编译问题,nacos-api.1.4.0版本已经将application.properties文件挪走了,不影响ApolloApplication的启动。但是nacosDis'covery的spring-profile有缺陷,不支持ApolloApplication模式启动,原因是未配置@componentscan扫描,ApolloApplication模式下没法启用自动配置。验证时只能通过ConfigServiceApplication和AdminServiceApplication单独启动。
nacos只作为备选注册服务发现方案
The dependency of nacos-discovery-spring-boot-starter is isolated through the maven profile. By default, only the dependency of nacos-api solves the compilation problem. The nacos-api.1.4.0 version has moved the application.properties file and does not affect ApolloApplication Start. However, the spring-profile of nacosDis'covery is defective and does not support ApolloApplication mode startup. The reason is that @componentscan scanning is not configured, and automatic configuration cannot be enabled in ApolloApplication mode. The verification can only be started separately through ConfigServiceApplication and AdminServiceApplication. nacos is only an alternative registration service discovery solution

Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works by using maven profile, with the following suggestions.

Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nobodyiam nobodyiam merged commit 41519f4 into apolloconfig:master Jan 3, 2021
@nobodyiam nobodyiam added this to the 1.8.0 milestone Feb 11, 2021
@klboke klboke deleted the nacos branch May 19, 2021 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/metaserver apollo meta server feature Categorizes issue as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants