alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.21k stars 12.83k forks source link

Nacos2.0客户端注册失败 NacosException: Request nacos server failed #5215

Closed warthur1214 closed 3 years ago

warthur1214 commented 3 years ago

nacos-client 1.4.1可以正常注册。

相关组件版本: spring-cloud-starter-alibaba-nacos-discovery: 2.2.5.RELEASE spring-cloud-starter-alibaba-nacos-config: 2.2.5.RELEASE spring-cloud-starter-dubbo: 2.2.5.RELEASE nacos-client: 2.0.0

部署环境:docker-compose yaml

version: "2"
services:
  nacos:
    image: nacos/nacos-server:2.0.0
    container_name: nacos-standalone-mysql
    hostname: nacos-server
    environment:
      - PREFER_HOST_MODE=hostname
      - MODE=standalone
      - NACOS_AUTH_ENABLE=true
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_SERVICE_HOST=192.168.0.108
      - MYSQL_SERVICE_DB_NAME=nacos_devtest
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=nacos
      - MYSQL_SERVICE_PASSWORD=nacos
      - JVM_XMS=1g
      - JVM_XMX=1g
      - JVM_XMN=500m
    volumes:
      - ./standalone-logs/:/home/nacos/logs
      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9849:9849"
      - "9555:9555"
  prometheus:
    container_name: prometheus
    image: prom/prometheus:latest
    volumes:
      - ./prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
    depends_on:
      - nacos
    restart: on-failure
  grafana:
    container_name: grafana
    image: grafana/grafana:latest
    ports:
      - 3000:3000
    restart: on-failure

dubbo 配置如下:

dubbo:
  registry:
    address: nacos://${spring.cloud.nacos.discovery.server-addr}?username=${spring.cloud.nacos.username}&password=${spring.cloud.nacos.password}
    parameters:
      namespace: ${spring.cloud.nacos.discovery.namespace}
    group: dubbo
    simplified: true
    use-as-config-center: false
    register: true
    subscribe: true
  protocol:
    name: dubbo
    port: -1
  scan:
    base-packages: com.warthur.nacos.demo.domain.dubbo
  consumer:
    filter: ConsumerAuthFilter
  provider:
    filter: AuthorizationFilter
  cloud:
    subscribed-services: spring-cloud-nacos-demo

Exception:

ERROR org.apache.dubbo.registry.nacos.NacosRegistry:586 ERROR -  [DUBBO] Request nacos server failed: , dubbo version: 2.7.8, current host: 10.30.201.48
com.alibaba.nacos.api.exception.NacosException: Request nacos server failed: 
    at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:213) ~[nacos-client-2.0.0.jar:na]
    at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.subscribe(NamingGrpcClientProxy.java:172) ~[nacos-client-2.0.0.jar:na]
    at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.subscribe(NamingClientProxyDelegate.java:146) ~[nacos-client-2.0.0.jar:na]
    at com.alibaba.nacos.client.naming.NacosNamingService.subscribe(NacosNamingService.java:387) ~[nacos-client-2.0.0.jar:na]
    at com.alibaba.cloud.nacos.discovery.NacosWatch.start(NacosWatch.java:123) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
    at com.warthur.nacos.demo.MainApplication.main(MainApplication.java:17) [classes/:na]

debug info: image

when

rpcClient.request(request)

request header accessToken was gone。

已解决此问题: 我的nacos自建账号没有分配public命名空间权限,在nacos-client 1.4.1上可以正常使用。 升级nacos-client2.0后无法注册,分配public命名空间权限后,已可以正常注册。

image

jackwang-wangcb commented 3 years ago

想问下 那个端口 - "9555:9555" 是干啥用的?

warthur1214 commented 3 years ago

@jackwang-wangcb 我没关注,是按官方部署文档下载的yaml。 https://github.com/nacos-group/nacos-docker.git

KomachiSion commented 3 years ago

返回码403,说明打开了鉴权但是没有输入用户名密码,所以鉴权失败导致了注册失败。 或者是用户名密码错误

KomachiSion commented 3 years ago

可以试着debug一下,getSecurityHeader 方法,看下里面的用户名密码是不是正确的

warthur1214 commented 3 years ago

可以试着debug一下,getSecurityHeader 方法,看下里面的用户名密码是不是正确的

@KomachiSion 在 rpcClient.request(request) 之前鉴权token是存在的, 我用nacos-client 1.4.1客户端是可以正常注册。debug信息如下: image

KomachiSion commented 3 years ago

image image 我复现不出来这个问题。 我试了下 发起请求前request里是有token的,返回后可能被grpcCLient回收了,但是确实是可以注册的

KomachiSion commented 3 years ago

image

然后我故意把密码改错, 得到了403的返回。 我认为应该还是设置有问题吧。

或者你提供下你的赋权等信息。

warthur1214 commented 3 years ago

@KomachiSion 没法复现,确实不好定位问题来源。 主要是我配置信息都不变,把nacos-client版本改成1.4.1可以正常注册 我的bootstrap.yaml文件如下:

spring:
  application:
    name: spring-cloud-nacos-demo
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      username: nacos
      password: nacos
      discovery:
        server-addr: localhost:8848
        group: DEFAULT_GROUP
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        file-extension: yaml
        namespace: ${spring.cloud.nacos.discovery.namespace}
        extension-configs:
          - refresh: true
            dataId: redis.yaml
          - refresh: true
            dataId: jdbc.yaml
          - refresh: true
            dataId: logback.yaml
          - refresh: true
            dataId: mybatis.yaml
        group: ${spring.cloud.nacos.discovery.group}

调试信息截图: image image

warthur1214 commented 3 years ago

@KomachiSion 我用的是自建账号密码,当我改成nacos账号后可以注册了。 可能是我的账号权限问题,奇怪的是在客户端1.4.1上没有遇到这个问题。 我再研究下,感谢你抽时间帮我排查问题。