一、簡介
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/zh-hant/n/310090.html