apache / apache/shenyu-client-golang

使用Nacos注册,提示异常.Nacos部署的服务版本是2.0.3

Open
#47 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
18
Forks
13
PR merge metrics
No merged PRs in 30d

Description

代码:
package shenyu

import (
"encoding/json"
"github.com/apache/shenyu-client-golang/clients/nacos_client"
"github.com/apache/shenyu-client-golang/common/constants"
"github.com/apache/shenyu-client-golang/common/shenyu_sdk_client"
"github.com/apache/shenyu-client-golang/model"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/vo"

"net"
"pki-go-server/src/com/ifyou/pki/conf"
)

func RegisterShenYu() {
//设置nacos环境配置
ncp := &nacos_client.NacosClientParam{
IpAddr: "10.246.162.117",
Port: 18848,
NamespaceId: "d1bd133f-f415-404b-8e2c-60847ce426d4",
}

//元数据是必要的参数,这将注册到shenyu网关使用
metaData := &model.URIRegister{
Protocol: "http", //需要用户提供
AppName: "pki-go-server", //需要用户提供
ContextPath: "/godemo/api", //需要用户提供
RPCType: constants.RPCTYPE_HTTP, //需要用户提供
Host: getLocalIP(), //需要用户提供
Port: "8080", //需要用户提供
}
metaDataStringJson, _ := json.Marshal(metaData)

//初始化Nacos注册实例信息
nacosRegisterInstance := vo.RegisterInstanceParam{
Ip: "10.246.162.117", //需要用户提供
Port: 18848, //需要用户提供
ServiceName: "pki-go-server", //需要用户提供
Weight: 10, //需要用户提供
Enable: true, //需要用户提供
Healthy: true, //需要用户提供
Ephemeral: true, //需要用户提供
Metadata: map[string]string{"contextPath": "contextPath", "uriMetadata": string(metaDataStringJson)},
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
result, createResult, err := sdkClient.NewClient(ncp)
if !createResult && err != nil {
conf.LogEntry.Fatalf("Create nacos client error : %v", err)
}

nc := &nacos_client.ShenYuNacosClient{
NacosClient: result.(*naming_client.NamingClient),
}

registerResult, err := nc.RegisterServiceInstance(nacosRegisterInstance)
if !registerResult && err != nil {
conf.LogEntry.Fatalf("Register nacos Instance error : %v", err)
} else {
conf.LogEntry.Infof("Register nacos Instance success : %v", registerResult)
}

//do your logic

}

func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "unknow"
}

for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
gInnerIP := ipnet.IP.String()
return gInnerIP
}
}
}
return "unknow"
}

2024-04-07T15:03:16.118+0800 ERROR nacos_server/nacos_server.go:230 api,method:, params:<{"app":"","clusterName":"","enable":"true","ephemeral":"true","groupName":"DEFAULT_GROUP","healthy":"true","ip":"10.246.162.117","metadata":"{\"contextPath\":\"contextPath\",\"uriMetadata\":\"{\\\"protocol\\\":\\\"http\\\",\\\"appName\\\":\\\"pki-go-server\\\",\\\"contextPath\\\":\\\"/godemo/api\\\",\\\"rpcType\\\":\\\"http\\\",\\\"host\\\":\\\"192.168.137.59\\\",\\\"port\\\":\\\"8080\\\"}\"}","namespaceId":"d1bd133f-f415-404b-8e2c-60847ce426d4","port":"18848","serviceName":"DEFAULT_GROUP@@pki-go-server","weight":"10"}>, call domain error: , result:<{"timestamp":"2024-04-07T15:03:11.808+08:00","status":403,"error":"Forbidden","message":"unknown user!","path":"/nacos/v1/ns/instance"}>
FATA[0004] RegisterServiceInstance failure! ,error is :retry3times request failed,err=request return error code 403

可能的原始是否为,Nacos需要用户名密码登陆. shenyu-sdk使用的是Nacos 1V的包,导致无法注册 抛出403?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at RegisterServiceInstance and the NewClient call in the reported Go example, then inspect the Nacos client dependency and the server response in nacos_server/nacos_server.go. Reproduce registration against Nacos 2.0.3 with the shown parameters and determine whether authentication or client-version compatibility causes the 403; done means the cause is documented and registration succeeds or the supported limitation is explicit.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.