Comments (8)
@shumintao 有什么报错信息么。可以贴一下日志。
我用你这套代码,sdk 版本v1.0 ,
注册服务是正常的。你可以检查一下TimeoutMs: option.TimeoutMs,是否有超时。
from nacos-sdk-go.
@shumintao 有什么报错信息么。可以贴一下日志。
我用你这套代码,sdk 版本v1.0 ,
注册服务是正常的。你可以检查一下TimeoutMs: option.TimeoutMs,是否有超时。
一直提示: 注册失败: retry 3 times request failed!
- 调用
option:=client.NewOption().WithAdress("123.56.24.220").WithPort(8848)
nacosClient:=client.NewNacos(option)
if ok :=nacosClient.Register("10.4.176.26",9000);ok{
fmt.Println("注册服务成功")
}
from nacos-sdk-go.
@shumintao 有什么报错信息么。可以贴一下日志。
我用你这套代码,sdk 版本v1.0 ,
注册服务是正常的。你可以检查一下TimeoutMs: option.TimeoutMs,是否有超时。
- client.go
package client
import (
"fmt"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/vo"
)
type NacosClient struct {
Client config_client.IConfigClient
NamingClient naming_client.INamingClient
}
// New Nacos Client
func NewNacos(option *Options) *NacosClient {
clientConfig := constant.ClientConfig{
TimeoutMs: option.TimeoutMs,
ListenInterval: option.ListenInterval,
BeatInterval: option.BeatInterval,
}
serverConfigs := []constant.ServerConfig{
constant.ServerConfig{
ContextPath: "/nacos",
IpAddr: option.Address,
Port: option.Port,
},
}
// Create Config Center
client, err := clients.CreateConfigClient(map[string]interface{}{
"serverConfigs": serverConfigs,
"clientConfig": clientConfig,
})
if err != nil {
panic(err)
}
// Create Register Center
namingClient,err:=clients.CreateNamingClient(map[string]interface{}{
"serverConfigs":serverConfigs,
"clientConfig":clientConfig,
})
if err!=nil{
panic(err)
}
return &NacosClient{Client: client,NamingClient: namingClient}
}
// Publish Config
func (c *NacosClient) Publish(dataId, group, content string) bool {
ok, err := c.Client.PublishConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
Content: content,
})
if err != nil {
return false
}
return ok
}
// Get Config
func (c *NacosClient) Get(dataId, group string) string {
s, err := c.Client.GetConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
})
if err != nil {
return ""
}
return s
}
// Delete Config
func (c *NacosClient) Delete(dataId, group string) bool {
ok,err:=c.Client.DeleteConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
})
if err!=nil{
return false
}
return ok
}
// Listen Config
func (c *NacosClient) Listen(dataId, group string,OnChange func(namespace, group, dataId, data string)) error {
return c.Client.ListenConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
OnChange: OnChange,
})
}
// Register Service
func (c *NacosClient) Register(ip string,port uint64) bool {
ok,err:=c.NamingClient.RegisterInstance(vo.RegisterInstanceParam{
Ip: "123.56.24.220",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "zwt",
Enable: true,
Healthy: true,
Ephemeral: true,
})
if err!=nil{
fmt.Println("注册失败:",err)
return false
}
return ok
}
// Deregister Service
func (c *NacosClient) Deregister(ip string,port uint64) bool {
ok ,err:=c.NamingClient.DeregisterInstance(vo.DeregisterInstanceParam{
Ip: ip,
Port: port,
ServiceName: "demo.go",
Cluster: "a",
Ephemeral: true,
})
if err!=nil{
return false
}
return ok
}
// Get Service
func (c *NacosClient) GetService(name string) (model.Service,error) {
return c.NamingClient.GetService(vo.GetServiceParam{
Clusters: []string{"a"},
ServiceName: name,
})
}
// Select Health Service
func (c *NacosClient) SelectOneHealthyInstance() (*model.Instance,error) {
return c.NamingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: "demo.go",
Clusters: []string{"a"},
})
}
- option.go
package client
type Options struct {
TimeoutMs uint64
ListenInterval uint64
BeatInterval int64
Address string
Port uint64
}
func deafultOptions() *Options {
return &Options{
TimeoutMs: 10*1000,
ListenInterval: 3*1000,
BeatInterval: 5*1000,
}
}
func (option *Options) WithAdress(address string) *Options {
option.Address = address
return option
}
func (option *Options) WithPort(port uint64) *Options {
option.Port = port
return option
}
func NewOption() *Options {
return deafultOptions()
}
- main.go
func main() {
option:=client.NewOption().WithAdress("123.56.24.220").WithPort(8848)
nacosClient:=client.NewNacos(option)
if ok:=nacosClient.Publish("test","test","hello word");ok{
fmt.Println("Nacos 配置列表")
}
content:=nacosClient.Get("test","test")
fmt.Println("Nacos 配置内容:",content)
if err:=nacosClient.Listen("test","test", func(namespace, group, dataId, data string) {
fmt.Println("监听修改修改配置:",data)
});err!=nil{
panic(err)
}
if ok:=nacosClient.Delete("test","test");ok{
fmt.Println("删除配置成功")
}
if ok :=nacosClient.Register("10.4.176.26",9000);ok{
fmt.Println("注册服务成功")
}
}
- go.mod
module gitee.com/nacos.demon/nacosGo
go 1.13
require (
github.com/gin-gonic/gin v1.6.3
github.com/nacos-group/nacos-sdk-go v1.0.0
)
from nacos-sdk-go.
@shumintao retry 3 times request failed! 这个错误是重试了三次去请求Nacos serve。
你可以关注一下log error的错误日志。类似于这样格式:api<%s>,method:<%s>, params:<%s>, call domain error:<%+v> , result:<%s>
from nacos-sdk-go.
@shumintao retry 3 times request failed! 这个错误是重试了三次去请求Nacos serve。
你可以关注一下log error的错误日志。类似于这样格式:api<%s>,method:<%s>, params:<%s>, call domain error:<%+v> , result:<%s>
- Java 日志
对java真是不太懂
2020-08-20T18:39:34.782+0800 �[34mINFO�[0m udp server start, port: 55159
2020-08-20T18:39:34.783+0800 �[34mINFO�[0m register instance namespaceId:<>,serviceName:<DEFAULT_GROUP@@demo.go> with instance:<{"valid":false,"marked":false,"instanceId":"","port":8848,"ip":"123.56.24.220","weight":10,"metadata":{},"clusterName":"zwt","serviceName":"","enabled":true,"healthy":true,"ephemeral":true}>
2020-08-20T18:39:34.805+0800 �[31mERROR�[0m api</v1/ns/instance>,method:<POST>, params:<{"clusterName":"zwt","enable":"true","ephemeral":"true","groupName":"DEFAULT_GROUP","healthy":"true","ip":"123.56.24.220","metadata":"{}","namespaceId":"","port":"8848","serviceName":"DEFAULT_GROUP@@demo.go","weight":"10"}>, call domain error:<request return error code 405> , result:<{"timestamp":"2020-08-20T10:39:34.802+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/nacos/v1/ns/instance"}>
2020-08-20T18:39:34.820+0800 �[31mERROR�[0m api</v1/ns/instance>,method:<POST>, params:<{"clusterName":"zwt","enable":"true","ephemeral":"true","groupName":"DEFAULT_GROUP","healthy":"true","ip":"123.56.24.220","metadata":"{}","namespaceId":"","port":"8848","serviceName":"DEFAULT_GROUP@@demo.go","weight":"10"}>, call domain error:<request return error code 405> , result:<{"timestamp":"2020-08-20T10:39:34.815+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/nacos/v1/ns/instance"}>
2020-08-20T18:39:34.834+0800 �[31mERROR�[0m api</v1/ns/instance>,method:<POST>, params:<{"clusterName":"zwt","enable":"true","ephemeral":"true","groupName":"DEFAULT_GROUP","healthy":"true","ip":"123.56.24.220","metadata":"{}","namespaceId":"","port":"8848","serviceName":"DEFAULT_GROUP@@demo.go","weight":"10"}>, call domain error:<request return error code 405> , result:<{"timestamp":"2020-08-20T10:39:34.831+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/nacos/v1/ns/instance"}>
- 显示错误
from nacos-sdk-go.
@shumintao
// Register Service
func (c *NacosClient) Register(ip string,port uint64) bool {
ok,err:=c.NamingClient.RegisterInstance(vo.RegisterInstanceParam{
Ip: "123.56.24.220",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "zwt",
Enable: true,
Healthy: true,
Ephemeral: true,
})
if err!=nil{
fmt.Println("注册失败:",err)
return false
}
return ok
}
nacos-server 用的什么版本?
首先 vo.RegisterInstanceParam 的ip 和 port 是注册实例的ip和端口,不应该和nacos server ip和端口一致。
两种方式可以验证一下 你的nacos-server是否有问题。
1:
你可以使用open-api ,尝试将vo.RegisterInstanceParam的内容注册到你的nacos中。看看是否还有相同的问题。
参考注册实例:https://nacos.io/zh-cn/docs/open-api.html
2:option:=client.NewOption().WithAdress("123.56.24.220").WithPort(8848) 换成
option:=client.NewOption().WithAdress("console.nacos.io").WithPort(80)
from nacos-sdk-go.
@shumintao
// Register Service func (c *NacosClient) Register(ip string,port uint64) bool { ok,err:=c.NamingClient.RegisterInstance(vo.RegisterInstanceParam{ Ip: "123.56.24.220", Port: 8848, ServiceName: "demo.go", Weight: 10, ClusterName: "zwt", Enable: true, Healthy: true, Ephemeral: true, }) if err!=nil{ fmt.Println("注册失败:",err) return false } return ok }
nacos-server 用的什么版本?
首先 vo.RegisterInstanceParam 的ip 和 port 是注册实例的ip和端口,不应该和nacos server ip和端口一致。两种方式可以验证一下 你的nacos-server是否有问题。
1:
你可以使用open-api ,尝试将vo.RegisterInstanceParam的内容注册到你的nacos中。看看是否还有相同的问题。
参考注册实例:https://nacos.io/zh-cn/docs/open-api.html2:option:=client.NewOption().WithAdress("123.56.24.220").WithPort(8848) 换成
option:=client.NewOption().WithAdress("console.nacos.io").WithPort(80)
-
nacos-server 版本
-
docker-compose.yaml
version: "3"
services:
nacos:
image: paderlol/nacos:latest
container_name: nacos-standalone
environment:
- MODE=standalone
volumes:
- ./logs:/home/nacos/logs
ports:
- "8848:8848"
- 运行
docker-compose up -d
from nacos-sdk-go.
@shumintao
// Register Service func (c *NacosClient) Register(ip string,port uint64) bool { ok,err:=c.NamingClient.RegisterInstance(vo.RegisterInstanceParam{ Ip: "123.56.24.220", Port: 8848, ServiceName: "demo.go", Weight: 10, ClusterName: "zwt", Enable: true, Healthy: true, Ephemeral: true, }) if err!=nil{ fmt.Println("注册失败:",err) return false } return ok }
nacos-server 用的什么版本?
首先 vo.RegisterInstanceParam 的ip 和 port 是注册实例的ip和端口,不应该和nacos server ip和端口一致。两种方式可以验证一下 你的nacos-server是否有问题。
1:
你可以使用open-api ,尝试将vo.RegisterInstanceParam的内容注册到你的nacos中。看看是否还有相同的问题。
参考注册实例:https://nacos.io/zh-cn/docs/open-api.html2:option:=client.NewOption().WithAdress("123.56.24.220").WithPort(8848) 换成
option:=client.NewOption().WithAdress("console.nacos.io").WithPort(80)
非常感谢
找到原因了
nacos-serve 的问题
我使用官方镜像目前可以注册服务了。
使用方法如下:
git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
from nacos-sdk-go.
Related Issues (20)
- cache not working
- 重启nacos服务端后编辑配置,客户端ListenConfig收不到消息 HOT 2
- constant.ClientConfig.updateCacheWhenEmpty配置项不起作用
- [version compatibility] success field of nacos server's response is empty HOT 2
- 运行出错 panic: runtime error: invalid memory address or nil pointer dereference HOT 5
- 当我注销一个实例后再次查询服务是查询的缓存然后再查询的实例,应该如何避免这个问题? HOT 2
- Ephemeral=true时,服务注册失败 HOT 2
- [BUG] Config listener doesn't execute when querying result is config doesn't exist
- 启动项目出现log日志 HOT 1
- Is there a way to manage Nacos Service via sdk? HOT 8
- 我在使用nacos作为配置中心的时候 为什么有时候能读取到配置 有时候读取的是空串 错误是nil HOT 2
- [BUG] naming proxy uses config metrics to record metrics. HOT 1
- 初始化nacos失败 问题 HOT 1
- ⚠️服务注册失败没有任何提示 HOT 1
- 不能连接nacos客户端 HOT 4
- Nacos 的 naming http client 的 PushReceiver 是否应该对 ReadFromUDP 方法增加 ctx 超时 HOT 2
- The local cache file create error. HOT 1
- Bugs for getConfig HOT 2
- sdk 中 查询实例相关方法 SelectInstances 没有命名空间参数吗 ? HOT 2
- 注册服务时无法连接nacos HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nacos-sdk-go.