一、简介
NacosLinux是阿里巴巴公司开源的一款基于Go语言开发的分布式服务发现和配置管理系统,具有如下特点:
1、强一致性:保证注册中心、配置管理与服务发现的数据都是实时同步的。
2、故障转移:具有集群模式和多数据中心支持,保证高可用。
3、易于扩展:支持自定义扩展,适用于大规模数据中心。
4、生态丰富:支持多种编程语言和框架。NacosLinux可以作为微服务应用的注册中心、配置中心和服务发现。NacosLinux提供了完全免费的全功能开源版本。
二、注册中心
注册中心主要用于存储微服务的实例信息,服务的名称、 IP、端口、健康状态等。NacosLinux使用了Paxos算法来保证在不同节点间的一致性,解决了传统注册中心在网络抖动、节点丢失等场景下的问题。在NacosLinux中,注册中心的数据包括大多数信息被本地写入到磁盘后才算是真正注册到注册中心。开源社区的客户端SDK已经支持了Eureka、Consul、Zookeeper等服务。例如,下面是向NacosLinux注册一个服务实例的代码:
client, err := naming.New(&naming.Config{
// NacosLinux addresses.
// If you use a standalone version of NacosLinux, only one address is required here.
// If you are using the cluster version of NacosLinux, you can fill in all the nodes in the cluster.
// Multiple addresses can be separated by commas.
// For example: []string{"http://192.168.100.1:8848", "http://192.168.100.2:8848"}
// NacosLinux地址。
// 如果使用NacosLinux的单机版本,这里只需要填写一个地址即可。
// 如果使用NacosLinux的集群版本,则可以填写集群中所有节点。
// 多个地址之间用英文逗号隔开。
// 例如:[]string{"http://192.168.100.1:8848", "http://192.168.100.2:8848"}
Endpoint: "192.168.0.100:8808",
// Your namespace ID, the unified management of multiple services under the same account,
// and the namespace ID is used to distinguish different environments, such as dev/test/prod
// 您的命名空间编号,同一账号下多个服务的统一管理,
// 命名空间编号用于区分不同的环境,如dev/test/prod
NamespaceId: "5a6b507c-68fe-4d4a-bcd1-4a18f66a4d0c",
// The information of the service you want to register with NacosLinux.
// 您要向NacosLinux注册的服务的信息。
ServiceName: "example",
// The IP of the instance, which is automatically obtained by the SDK and you do not need to manually fill it in.
// 实例的IP,由SDK自动获取,您不需要手动填写。
// 如果您的机器上有多个网卡或IP,请确保获取到的IP是您期望向NacosLinux注册的IP。
// 如果无法自动获取,则会报错。
IpAddr: "",
// The port number the service is listening on.
// 服务监听的端口号。
Port: 8888,
})
// Register the service instance with NacosLinux.
// 向NacosLinux注册服务实例。
err = client.RegisterInstance(vo.RegisterInstanceParam{
// The metadata of the service instance.
// 服务实例的元数据。
Metadata: map[string]string{
"weight": "1",
},
// The ID of the service instance. This ID is unique within the scope of the service name.
// 服务实例的编号,该编号在服务名下唯一。
InstanceId: "example-01",
})
三、配置中心
配置中心主要用于管理微服务的各种配置,例如服务的超时时间、日志级别等。NacosLinux支持动态修改配置,在Mary和这一些途径,可以实时更新到配置中心,同时推送到相应的微服务上,可以做到微服务的无感知更新。下面是读取配置的示例代码,可以看出极其简单:
client, err := config.NewConfigClient(map[string]interface{}{
// NacosLinux addresses.
// If you use a standalone version of NacosLinux, only one address is required here.
// If you are using the cluster version of NacosLinux, you can fill in all the nodes in the cluster.
// Multiple addresses can be separated by commas.
// For example: "192.168.100.1:8848,192.168.100.2:8848"
// NacosLinux地址。
// 如果使用NacosLinux的单机版本,这里只需要填写一个地址即可。
// 如果使用NacosLinux的集群版本,则可以填写集群中所有节点。
// 多个地址之间用英文逗号隔开。
// 例如:"192.168.100.1:8848,192.168.100.2:8848"
"serverConfigs": []string{
"http://console.demo.nacos.io:80",
},
})
// Get the value of the configuration item.
// 获取配置项的值。
fmt.Println(client.GetConfig(vo.ConfigParam{
DataId: "example",
Group: "DEFAULT_GROUP",
}))
四、服务发现
服务发现主要用于查询可用的微服务实例列表,可以通过查询接口获得对应服务的所有可用实例。例如下面是使用NacosLinux查询一个服务的所有实例的代码:
client, err := naming.New(&naming.Config{
// NacosLinux addresses.
// If you use a standalone version of NacosLinux, only one address is required here.
// If you are using the cluster version of NacosLinux, you can fill in all the nodes in the cluster.
// Multiple addresses can be separated by commas.
// For example: []string{"http://192.168.100.1:8848", "http://192.168.100.2:8848"}
// NacosLinux地址。
// 如果使用NacosLinux的单机版本,这里只需要填写一个地址即可。
// 如果使用NacosLinux的集群版本,则可以填写集群中所有节点。
// 多个地址之间用英文逗号隔开。
// 例如:[]string{"http://192.168.100.1:8848", "http://192.168.100.2:8848"}
Endpoint: "192.168.0.100:8808",
// Your namespace ID, the unified management of multiple services under the same account,
// and the namespace ID is used to distinguish different environments, such as dev/test/prod
// 您的命名空间编号,同一账号下多个服务的统一管理,
// 命名空间编号用于区分不同的环境,如dev/test/prod
NamespaceId: "5a6b507c-68fe-4d4a-bcd1-4a18f66a4d0c",
})
// Query the instances of the service.
// 查询服务的实例列表。
instances, err := client.GetAllInstances(vo.GetAllInstancesParam{
// The name of the service. You must provide the name of the service you want to query.
// 服务的名称。您必须提供要查询的服务名称。
ServiceName: "example",
// The namespace ID, which is used to distinguish different environments under the same account,
// such as dev/test/prod.
// 命名空间编号,用于区分同一账号下不同的环境,如dev/test/prod。
NamespaceId: "5a6b507c-68fe-4d4a-bcd1-4a18f66a4d0c",
})
五、结语
以上就是对NacosLinux这款微服务应用的注册中心、配置中心和服务发现进行介绍。NacosLinux的设计与实现完全开源,并提供了完善的开发者API文档和SDK。使用NacosLinux,可以大大简化微服务的开发和部署过程,提高开发效率和运行效率。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/310090.html
微信扫一扫
支付宝扫一扫