当前位置:   article > 正文

Spring Cloud Alibaba系列(5)之:nacos源码本地环境搭建_nacos 二次开发

nacos 二次开发

传送门

Spring Cloud Alibaba系列之nacos:(1)安装

Spring Cloud Alibaba系列之nacos:(2)单机模式支持mysql

Spring Cloud Alibaba系列之nacos:(3)服务注册发现

Spring Cloud Alibaba系列之nacos:(4)配置管理

为什么要搭建本地Nacos源码环境

nacos自带了一套简单的认证权限管理体系,在多数情况下已经够用了。不过有少数情况还是不太满足需求:

  • 有的公司有统一的账号管理体系,技术选型nacos之后,也要将nacos的账号体系统一纳管起,方便用户登录及运维管理
  • 在内网的情况下,对nacos自身提供的认证权限管理体系要求不高;不过如果涉及外网访问,它的安全性不太够,需要安全性更强的登录/权限组件来替代

Nacos架构

上面谈到的nacos认证权限管理体系,在nacos中的所处位置,主要为Nacos Console模块服务:

图片来源于nacos官网:基本架构及概念 

环境搭建

如果要对nacos作二次开发,必然要下载源码搭建好本地环境调试,下面就介绍一下如何搭建的相关步骤!

源码下载

版本选择

nacos这种新兴的服务注册框架,肯定是在不断更新,所以这里选取最新的版本2.2.3来进行测试:

下载地址

下载地址:Releases · alibaba/nacos · GitHub

编译构建流程

下载完成之后,导入到idea里面,里面有一个BUILDING文件内容如下:

Build Instructions for NACOS
====================================================
(1) Prerequisites
    JDK 1.8+ is required in order to compile and run Nacos.
    nacos utilizes Maven as a distribution management and packaging tool. Version 3.0.3 or later is required.
    Maven installation and configuration instructions can be found here:
    http://maven.apache.org/run-maven/index.html
(2) Run test cases
    Execute the following command in order to compile and run test cases of each components:
    $ mvn test
(3) Import projects to Eclipse IDE
    First, generate eclipse project files:
    $ mvn -U eclipse:eclipse
    Then, import to eclipse by specifying the root directory of the project via:
    [File] > [Import] > [Existing Projects into Workspace].
(4) Build distribution packages
    Execute the following command in order to build the tar.gz packages and install JAR into local repository:
   #build nacos
    $ mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U

环境依赖

从上面的BUILDING文件可以看出:

  • JDK1.8以上,配置好环境变量
  • maven3.0.3以上,配置好环境变量

编译打包

从上面的BUILDING文件第4步,可以看到编译打包的命令:

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U

那在idea里面进行执行该命令,成功结果如下:

从mvn打包命令来看,执行的release-nacos这个profile,到nacos-distribution模块查看一下:

 最后再看下release-nacos.xml,即可定位到打出来包的位置:

运行启动

进入到bin/目录,直接按单机模式,启动nacos-server,执行startup.cmd文件:

startup.cmd -m standalone

启动成功,控制台会打印访问地址:

访问nacos地址:http://192.168.1.8:8848/nacos/index.html,启动成功:

本地运行

如果要本地启动nacos,比如IDEA里面,可以直接运行com.alibaba.nacos.Nacos类:

这里配置一下启动参数:-Dnacos.standalone=true,指定成单机运行模式(默认是以集群方式启动),否则会报如下错误:

2023-09-24 10:14:06.439 ERROR 4328 --- [           main] c.a.n.c.l.StartingApplicationListener    : Startup errors : 

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:423)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
    at com.alibaba.nacos.Nacos.main(Nacos.java:35)
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:142)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:104)
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:479)
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:211)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:182)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160)
    ... 8 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'distroFilterRegistration' defined in class path resource [com/alibaba/nacos/naming/web/NamingConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method 'distroFilterRegistration' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distroFilter': Unsatisfied dependency expressed through field 'distroMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distroMapper' defined in file [E:\work\code\nacos-2.2.3\naming\target\classes\com\alibaba\nacos\naming\core\DistroMapper.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverMemberManager' defined in file [E:\work\code\nacos-2.2.3\core\target\classes\com\alibaba\nacos\core\cluster\ServerMemberManager.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:212)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:203)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:97)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:86)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:260)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:234)
    at org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:53)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5211)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1383)
    at java.util.concurrent.FutureTask.run

$capture(FutureTask.java:266)atjava.util.concurrent.FutureTask.run(FutureTask.java)atorg.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)atjava.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)atorg.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)atorg.apache.catalina.core.StandardHost.startInternal(StandardHost.java:835)atorg.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)atorg.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393)atorg.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1383)atjava.util.concurrent.FutureTask.run
$capture(FutureTask.java:266)
    at java.util.concurrent.FutureTask.run(FutureTask.java)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:265)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:430)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:486)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:123)
    ... 13 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method 'distroFilterRegistration' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distroFilter': Unsatisfied dependency expressed through field 'distroMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distroMapper' defined in file [E:\work\code\nacos-2.2.3\naming\target\classes\com\alibaba\nacos\naming\core\DistroMapper.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverMemberManager' defined in file [E:\work\code\nacos-2.2.3\core\target\classes\com\alibaba\nacos\core\cluster\ServerMemberManager.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    ... 55 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distroFilter': Unsatisfied dependency expressed through field 'distroMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distroMapper' defined in file [E:\work\code\nacos-2.2.3\naming\target\classes\com\alibaba\nacos\naming\core\DistroMapper.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverMemberManager' defined in file [E:\work\code\nacos-2.2.3\core\target\classes\com\alibaba\nacos\core\cluster\ServerMemberManager.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:362)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:334)
    at com.alibaba.nacos.naming.web.NamingConfig
EnhancerBySpringCGLIB
c229cd2c.distroFilter(<generated>)
    at com.alibaba.nacos.naming.web.NamingConfig.distroFilterRegistration(NamingConfig.java:44)
    at com.alibaba.nacos.naming.web.NamingConfig
EnhancerBySpringCGLIB
c229cd2c.CGLIB$distroFilterRegistration$5(<generated>)
    at com.alibaba.nacos.naming.web.NamingConfig
EnhancerBySpringCGLIB
c229cd2c
FastClassBySpringCGLIB
d2979c50.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at com.alibaba.nacos.naming.web.NamingConfig
EnhancerBySpringCGLIB
c229cd2c.distroFilterRegistration(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 56 common frames omitted

配置环境变量

配置完重新启动:

服务端如何开启鉴权

  • Nacos是一个内部微服务组件,需要在可信的内部网络中运行,不可暴露在公网环境,防止带来安全风险。
  • Nacos提供简单的鉴权实现,为防止业务错用的弱鉴权体系,不是防止恶意攻击的强鉴权体系。

在2.2.3版本中,nacos默认关闭了认证权限功能,需要通过配置手动开启,具体配置可以参考:服务端如何开启鉴权

修改配置

按照官方文档配置启动,默认是不需要登录的,这样会导致配置中心对外直接暴露。而启用鉴权之后,需要在使用用户名和密码登录之后,才能正常使用nacos。

开启鉴权之后,application.properties中的配置信息为:

  1. nacos.core.auth.system.type=nacos
  2. nacos.core.auth.enabled=true
  3. nacos.core.auth.server.identity.key=SecretKey012345678901234567890123456789012345678901234567890123456789
  4. nacos.core.auth.server.identity.value=SecretKey012345678901234567890123456789012345678901234567890123456789
  5. ### 2.1.0 版本后
  6. nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

配置完成启动,出现下面的界面表示配置成功,默认账号/密码:nacos/nacos

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/414779
推荐阅读
相关标签
  

闽ICP备14008679号