首页 > 其他分享 >[nacos] Nacos <3> 应用场景及高频问题(FAQ)

[nacos] Nacos <3> 应用场景及高频问题(FAQ)

时间:2024-09-07 13:51:36浏览次数:12  
标签:FAQ Nacos nacos alibaba client NamingGrpcClientProxy naming com

场景:指定NACOS注册中心中spring cloud微服务应用的IP

spring:
  cloud:
    nacos:
      discovery:
        ip: 127.0.0.1

修改完成、并重启服务之后在nacos查看的地址如下:

场景:curl请求NACOS常用功能接口

  • 测试版本
  • nacos-client : 2.0.3
  • nacos-server : 2.1.2
  • 推荐文献

登录/获取 accessToken

# 获取 accessToken
curl -X POST '127.0.0.1:8848/nacos/v1/auth/login' -d 'username=nacos&password=nacos'

响应内容
{"accessToken":"xxx.xxx.xxxx-xxx-xxx","tokenTtl":86400000,"globalAdmin":false,"username":"read_bdp"}

获取配置

# 获取配置 | NACOS-SERVER 2.1.2 实测
curl -X GET 'https://config.xx.com/nacos/v1/cs/configs?show=all&dataId=application-xx-service.yml&group=XXX_GROUP&tenant=bdp_office&namespaceId=bdp_office&accessToken=xxxxxxx'

服务注册

# 服务注册 | NACOS-SERVER 2.1.2 实测
accessToken="xxxxxxx"
echo "https://nacos-config.xx.com/nacos/v1/ns/instance?port=8848&healthy=true&ip=11.11.11.11&weight=1.0&serviceName=nacos.test.3&encoding=GBK&namespaceId=bigdata_office&accessToken=${accessToken}"
curl -X POST "https://nacos-config.xx.com/nacos/v1/ns/instance?port=8848&healthy=true&ip=11.11.11.11&weight=1.0&serviceName=nacos.test.3&encoding=GBK&namespaceId=bdp_office&accessToken=$accessToken"
  • 关键源码
  • nacos-client : 2.0.3
  • nacos-server : 2.1.2
com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate#registerService

--> com.alibaba.nacos.client.naming.remote.NamingClientProxy#registerService

--> com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy#registerService
    @Override
    public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,
                instance);
        redoService.cacheInstanceForRedo(serviceName, groupName, instance);
        doRegisterService(serviceName, groupName, instance);
    }

--> com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy#doRegisterService
    public void doRegisterService(String serviceName, String groupName, Instance instance) throws NacosException {
        InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName,
                NamingRemoteConstants.REGISTER_INSTANCE, instance);
        requestToServer(request, Response.class); // 调用 :NamingGrpcClientProxy#requestToServer
        redoService.instanceRegistered(serviceName, groupName);
    }

--> com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy#requestToServer
    private <T extends Response> T requestToServer(AbstractNamingRequest request, Class<T> responseClass)
            throws NacosException {
        try {
            request.putAllHeader(getSecurityHeaders());
            request.putAllHeader(getSpasHeaders(
                    NamingUtils.getGroupedNameOptional(request.getServiceName(), request.getGroupName())));
            Response response =
                    requestTimeout < 0 ? rpcClient.request(request) : rpcClient.request(request, requestTimeout);
            if (ResponseCode.SUCCESS.getCode() != response.getResultCode()) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            if (responseClass.isAssignableFrom(response.getClass())) {
                return (T) response;
            }
            NAMING_LOGGER.error("Server return unexpected response '{}', expected response should be '{}'",
                    response.getClass().getName(), responseClass.getName());
        } catch (Exception e) {
            throw new NacosException(NacosException.SERVER_ERROR, "Request nacos server failed: ", e);
        }
        throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");
    }
  • 注册成功的响应:

https://nacos-config.xx.com/nacos/v1/ns/instance?port=8848&healthy=true&ip=11.11.11.11&weight=1.0&serviceName=nacos.test.3&encoding=GBK&namespaceId=bdp_office&accessToken=xxxxxxx
关键日志

[TID: N/A] [my-xxl-job-executor] [system] [2024/09/06 11:18:21.248] [INFO ] [main] [NamingGrpcClientProxy] registerService:112__||__[REGISTER-SERVICE] bdp_office registering service my-xxl-job-executor with instance Instance{instanceId='null', ip='192.168.19.181', port=9527, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName='DEFAULT', serviceName='null', metadata={management.endpoints.web.base-path=/actuator, preserved.register.source=SPRING_CLOUD}}
[TID: N/A] [my-xxl-job-executor] [system] [2024/09/06 11:18:21.260] [INFO ] [main] [NacosServiceRegistry] register:75__||__nacos registry, BDP_GROUP my-xxl-job-executor-data-distribute 192.168.19.181:9527 register finished
  • 当前用户无服务注册权限的响应: (即 可写权限)

以 NACOS SERVER 2.1.2 为例,此错误发生在 强制启用身份认证之后
{"timestamp":"2024-09-06T10:42:11.602+08:00","status":403,"error":"Forbidden","message":"authorization failed!","path":"/nacos/v1/ns/instance"}

关键日志

...
Caused by: java.lang.reflect.UndeclaredThrowableException
	at org.springframework.util.ReflectionUtils.rethrowRuntimeException(ReflectionUtils.java:147) ~[spring-core-5.2.15.RELEASE.jar!/:5.2.15.RELEASE]
	at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:82) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) ~[spring-cloud-commons-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]
	at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:78) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.7.RELEASE.
	...
...

...
Caused by: com.alibaba.nacos.api.exception.NacosException: Request nacos server failed:        
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:279) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doRegisterService(NamingGrpcClientProxy.java:129) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:115) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:95) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:145) ~[nacos-client-2.0.3.jar!/:?]
  ...
...

Caused by: com.alibaba.nacos.api.exception.NacosException: authorization failed!        
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:271) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doRegisterService(NamingGrpcClientProxy.java:129) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:115) ~[nacos-client-2.0.3.jar!/:?]        
  at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:95) ~[nacos-client-2.0.3.jar!/:?]
  ...
...

获取注册的服务列表

protocol="https"
endpoint="config.xxxx.com:443"
username=nacos
password=xxxxxx

curl -X GET "$protocol://$endpoint/nacos/v1/ns/service/list?namespaceId=bigdata_office&pageNo=1&pageSize=5&accessToken=$accessToken"

response

{"count":0,"doms":[]}

U 关键源码分析

com.alibaba.nacos.client.naming.core.ServerListManager

com.alibaba.nacos.common.remote.client.RpcClient

  • com.alibaba.nacos.common.remote.client.RpcClient#resolveServerInfo

X 参考文献

  • Nacos

标签:FAQ,Nacos,nacos,alibaba,client,NamingGrpcClientProxy,naming,com
From: https://www.cnblogs.com/johnnyzen/p/18401612

相关文章

  • [nacos] Nacos <1> 架构与原理
    1NACOS架构与原理架构推荐文献https://nacos.io/en-us/docs/architecture.html基本架构逻辑架构服务管理:实现服务CRUD、域名CRUD、服务健康检查、服务权重管理等。配置管理:实现配置的CRUD、版本管理、灰度管理、监控管理、推送轨迹、聚合数据等。元数据管理:提......
  • Nacos 认证绕过漏洞(CVE-2021-29441)
    相关介绍Nacos是阿里巴巴推出来的一个新开源项目,是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。致力于帮助发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,可以快速实现动态服务发现、服务配置、服务元数据及流量管理。小知识:Nacos初始用户......
  • debian11 申通 无感考勤 mysql postgresql nacos集群
     echo"nameserver114.114.114.114nameserver8.8.8.8">/etc/resolv.conf echo"debhttps://mirrors.aliyun.com/debian/bullseyemainnon-freecontribdeb-srchttps://mirrors.aliyun.com/debian/bullseyemainnon-freecontribdebhttps://......
  • 【SpringBoot】使用Nacos服务注册发现与配置管理
    前提:需要提前部署好nacos服务,这里可以参考我的文章:Windows下Nacos安装与配置0.版本信息SpringBoot3.2.8SpringCloud2023.0.1SpringCloudalibaba2023.0.1.0nacos2.3.2本地安装的nacos2.3.0       SpringBoot、SpringCloud、SpringCloudalibaba的版本对应可......
  • 使用 Nacos 实现动态路由
    Hello,大家好,我是V哥。最近写到使用Nacos实现动态路由的问题,整理了一下思路和案例,分享给大家。使用Nacos实现SpringCloudGateway的动态路由,主要涉及到以下几个步骤:添加依赖:在SpringCloudGateway应用的pom.xml文件中添加Nacos相关依赖。配置Nacos:在......
  • 直接复制nacos库,最后编辑-保存报错
    更改nacos的yml文件,保存后报错:caused:PreparedStatementCallback;SQL[INSERTINTOhis_config_info(id,data_id,group_id,tenant_id,app_name,content,md5,src_ip,src_user,gmt_modified,op_type)VALUES(?,?,?,?,?,?,?,?,?,?,?)];Field'nid'doesn&......
  • Goby 漏洞发布|Nacos Jraft 服务文件读取漏洞【已复现】
    漏洞名称:NacosJraft服务文件读取漏洞EnglishName:NacosJraftServicesFileReadVulnerabilityCVSScore:5.0漏洞描述:NACOS是阿里巴巴推出来的一个新开源项目,是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。致力于帮助发现、配置和管理微服务。Nac......
  • SpringBoot把本地的对象封装成为Nacos的配置对象
    你需要有个NacosNacos建立你的配置文件--建议yml文件编写你的yml配置platform:transaction:properties:notifyHost:"http://10.130.1.18:${server.port.cztech-service-gateway}"smsTemplate:"TEM_0029"#订单默认过期时间--分钟defau......
  • centos 7部署nacos 2.4.1版本单点方式
    文章目录Nacos:微服务架构中的服务发现与配置管理利器官方网址引言Nacos简介Nacos的核心功能1.服务发现和服务健康监测2.动态配置服务3.服务及其元数据管理Nacos的工作原理Nacos的集群部署与高可用性Nacos的使用场景如何使用Nacos1.安装Nacos2.服务注册与发现3.配......
  • Docker安装Nacos(docker2.0.4)
    一、准备工作1.打开目录cd/usr/local/docker/2.创建nacos文件夹mkdirnacos3.打开目录cdnacos/4.创建文件夹configmkdirconfig5.打开文件夹cdconfig/6.初始换数据库在nacos需要连接的数据库当中,创建数据库nacos_config字符集类型为utf8mb4在数据库nacos_con......