Nacos源码启动

一、下载源码

为保证速度,国内推荐使用gitee:https://gitee.com/mirrors/Nacos.git

 二、导入IDE中

参考之前文章配置国内Maven私服,快速更新工程。

Nacos源码启动_第1张图片

 三、启动过程,各种问题

找到启动入口:

先直接启动测试下:

 

Caused by: java.net.UnknownHostException: jmenv.tbsite.net
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
	at java.base/java.net.Socket.connect(Socket.java:633)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:178)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:531)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:636)
	at java.base/sun.net.www.http.HttpClient.(HttpClient.java:279)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:384)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:406)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1309)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1057)
	at com.alibaba.nacos.common.http.client.request.JdkHttpClientRequest.execute(JdkHttpClientRequest.java:114)
	at com.alibaba.nacos.common.http.client.NacosRestTemplate.execute(NacosRestTemplate.java:482)
	at com.alibaba.nacos.common.http.client.NacosRestTemplate.get(NacosRestTemplate.java:72)
	at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.syncFromAddressUrl(AddressServerMemberLookup.java:175)
	at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:143)

报错:解析不了这个域名jmenv.tbsite.net。AddressServerMemberLookup这些关键类看着是以集群形式启动了。先配置成单机启动:

-Dnacos.standalone=true

在以下位置配置:

Nacos源码启动_第2张图片

注意是nacos. 我看到其他文档此处介绍有错误。

 接下来打开application.properties中DB配置:

 执行初始化sql脚本:

Nacos源码启动_第3张图片

 初始化后的表如下:

Nacos源码启动_第4张图片

 给配置中心的表比较多。

启动报错如下:

Caused by: java.lang.IllegalArgumentException: The specified key byte array is 0 bits which is not secure enough for any JWT HMAC-SHA algorithm.  The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with HMAC-SHA algorithms MUST have a size >= 256 bits (the key size must be greater than or equal to the hash output size).  See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.
	at com.alibaba.nacos.plugin.auth.impl.jwt.NacosJwtParser.(NacosJwtParser.java:56)
	at com.alibaba.nacos.plugin.auth.impl.token.impl.JwtTokenManager.processProperties(JwtTokenManager.java:71)
	... 111 common frames omitted

跟到代码里面发现有个key设置:

Nacos源码启动_第5张图片

 属性是:public static final String TOKEN_SECRET_KEY = "nacos.core.auth.plugin.nacos.token.secret.key";

在application.properties中找到这个配置项,默认是""。设置一个大于256bit的值试试。

 此时再运行,报一个类找不到了,全是com.alibaba.nacos.consistency包下面的类,看了下源码里面确实没有?懵不懵!!

原因是com.alibaba.nacos.consistency.entity下面类是由protobuf在编译时自动生成。在eclipse下执行如下动作即可:

Nacos源码启动_第6张图片

 还有另外个项目也需要执行Maven generate,nacos-istio。但再次不信,执行过程中报找不到nacos其他模块的依赖。一种解决办法是先注释掉,再执行生成,执行完后取消注释,成功了!

最后再次运行启动:

2023-02-23 20:49:21.827 INFO 8284 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8848 (http) with context path '/nacos'

2023-02-23 20:49:21.848 INFO 8284 --- [ main] c.a.n.c.l.StartingApplicationListener : Nacos started successfully in stand alone mode. use embedded storage

2023-02-23 20:49:47.535 INFO 8284 --- [nio-8848-exec-5] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'

2023-02-23 20:49:47.538 INFO 8284 --- [nio-8848-exec-5] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms

访问http://127.0.0.1:8848/nacos/

成功进入控制台。

你可能感兴趣的:(nacos,nacos,源码启动)